Single quotes vs. double quotes in C or C++

179,074

Solution 1

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.

Solution 2

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.

Solution 3

Single quotes are characters (char), double quotes are null-terminated strings (char *).

char c = 'x';
char *s = "Hello World";

Solution 4

  • '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’

Solution 5

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

Share:
179,074

Related videos on Youtube

Vishwanath Dalvi
Author by

Vishwanath Dalvi

SOreadytohelp about me box is kept "", intentionally.

Updated on October 08, 2021

Comments

  • Vishwanath Dalvi
    Vishwanath Dalvi over 2 years

    When should I use single quotes and double quotes in C or C++ programming?

  • Vishwanath Dalvi
    Vishwanath Dalvi over 13 years
    thanks . means character is 1 byte with no null character '/0'at the end .. string contains null character at the end .
  • Oliver Charlesworth
    Oliver Charlesworth over 13 years
    @mr_eclair: A string literal always contains an implicit null terminator, but be careful. You could write something like char str[] = {'H','e','l','l','o'};, and str would not have a null terminator.
  • Steve Jessop
    Steve Jessop over 13 years
    in that situation, str isn't a string (at least, not a C-style string, which is defined to be a NTBS).
  • Oliver Charlesworth
    Oliver Charlesworth over 13 years
    @Steve: Understood. My point to @mr_eclair was that not everything that's a char[] (which people often thing of as "strings") is null-terminated.
  • eeeeaaii
    eeeeaaii over 11 years
    Thanks, I just saw this in some code and my response can only be described as "WTF". I have been using GCC on and off for 15 years and have never once run into this until now.
  • Wolf
    Wolf almost 10 years
    +1 besides magic headers, often used for command line evaluation.
  • Wolf
    Wolf almost 10 years
    @OliCharlesworth this is - fortunately - not the full truth: these are two string literals separated by a comment: "hello" /*seamlessly connected to*/ "world". And this can make sense for commented multi-line messages.
  • Mike Precup
    Mike Precup almost 10 years
    This doesn't really add any value to the question, since this information has already been encompassed in the other answers.
  • OldSchool
    OldSchool about 9 years
    "hello world" is a const char *.
  • CodeGuru
    CodeGuru over 8 years
    @Eiko when do you put a * infront of a variable eg *s as above?
  • Eiko
    Eiko over 8 years
    @CodeGuru You use a * when the variable is a pointer type. In this case, 's' points to an array of characters.
  • uzay95
    uzay95 over 3 years
    I didn't get this: 25443 = 99 + 256*99 Why 256*99?
  • Jack Murrow
    Jack Murrow over 3 years
    @OldSchool In C++, yes, but in C, no. In C, it's a char *, not a const char *.