If you are an American, using quotation marks could hardly be simpler: Use double quotation marks at all times unless quoting something within a quotation, when you use single.
Sometimes referred to as an apostrophe, a single quote is a punctuation symbol found on the United States QWERTY keyboard next to the Enter key.
To place quotation marks in a string in your code In Visual C# and Visual C++, insert the escape sequence \" as an embedded quotation mark.
In C and in C++ single quotes identify a single character, while double quotes create a string literal. 'a'
is a single a character literal, while "a"
is a string literal containing an 'a'
and a null terminator (that is a 2 char array).
In C++ the type of a character literal is char
, but note that in C, the type of a character literal is int
, that is sizeof 'a'
is 4 in an architecture where ints are 32bit (and CHAR_BIT is 8), while sizeof(char)
is 1 everywhere.
Some compilers also implement an extension, that allows multi-character constants. The C99 standard says:
6.4.4.4p10: "The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined."
This could look like this, for instance:
const uint32_t png_ihdr = 'IHDR';
The resulting constant (in GCC, which implements this) has the value you get by taking each character and shifting it up, so that 'I' ends up in the most significant bits of the 32-bit value. Obviously, you shouldn't rely on this if you are writing platform independent code.
Single quotes are characters (char
), double quotes are null-terminated strings (char *
).
char c = 'x';
char *s = "Hello World";
'x'
is an integer, representing the numerical value of the
letter x in the machine’s character set"x"
is an array of characters, two characters long,
consisting of ‘x’
followed by ‘\0’
I was poking around stuff like: int cc = 'cc'; It happens that it's basically a byte-wise copy to an integer. Hence the way to look at it is that 'cc' which is basically 2 c's are copied to lower 2 bytes of the integer cc. If you are looking for a trivia, then
printf("%d %d", 'c', 'cc'); would give:
99 25443
that's because 25443 = 99 + 256*99
So 'cc' is a multi-character constant and not a string.
Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With