Single quotes vs. double quotes in C or C++
Asked Answered
U

15

270

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

Unrefined answered 10/9, 2010 at 9:43 Comment(0)
G
357

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.

Greet answered 10/9, 2010 at 9:45 Comment(2)
in C, the type of a character literal is int. Hey more on this?Decorticate
For newbies including me: "...while double quotes create a string literal." ->Here, "string literal" is not a type of std::string. Rather, the type is const char[N], if there is no prefix(such as L, u8, etc.) (ref). Actually std::string kind of "hide" the null terminator even if it holds the null terminator in its underlying array.Surprint
T
54

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.

Tatman answered 10/9, 2010 at 11:0 Comment(0)
V
34

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

char c = 'x';
char *s = "Hello World";
Vulcan answered 10/9, 2010 at 9:45 Comment(4)
"hello world" is a const char *.Gaullist
@Vulcan when do you put a * infront of a variable eg *s as above?Chez
@Chez You use a * when the variable is a pointer type. In this case, 's' points to an array of characters.Vulcan
@Gaullist In C++, yes, but in C, no. In C, it's a char *, not a const char *.Koetke
A
25
  • '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’
Alyciaalyda answered 18/5, 2014 at 14:1 Comment(0)
D
15

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

Druci answered 20/5, 2012 at 21:29 Comment(4)
I didn't get this: 25443 = 99 + 256*99 Why 256*99?Bibb
@Bibb just because ascii table consists of 256 characters and if you assign a char to an integer variable it is implicitly converted to an integer value that corresponds to its char value. 'c' is 99th charachter in ascii table. Besides multichar constants is interpreted as integers.Isosteric
@SevbanBayır thanks for your time and explanation. This is called integral promotion isn't it?Bibb
@Bibb Not exactly. Integral Promotion is that if an int can't efford to represent the value that assigned to itself then this integer is implicitly converted to an unsigned int but here, there is a different situation. that's my thought...Isosteric
H
11

Single quotes are for a single character. Double quotes are for a string (array of characters). You can use single quotes to build up a string one character at a time, if you like.

char myChar     = 'A';
char myString[] = "Hello Mum";
char myOtherString[] = { 'H','e','l','l','o','\0' };
Hugohugon answered 10/9, 2010 at 9:46 Comment(0)
H
11
  1. single quote is for character;
  2. double quote is for string.
Huai answered 10/9, 2010 at 10:26 Comment(0)
L
8

In C, single-quotes such as 'a' indicate character constants whereas "a" is an array of characters, always terminated with the \0 character

Lemons answered 10/9, 2010 at 9:46 Comment(0)
A
7

Double quotes are for string literals, e.g.:

char str[] = "Hello world";

Single quotes are for single character literals, e.g.:

char c = 'x';

EDIT As David stated in another answer, the type of a character literal is int.

Ats answered 10/9, 2010 at 9:46 Comment(5)
thanks . means character is 1 byte with no null character '/0'at the end .. string contains null character at the end .Unrefined
@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.Ats
in that situation, str isn't a string (at least, not a C-style string, which is defined to be a NTBS).Formalin
@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.Ats
@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.Dredger
N
6

A single quote is used for character, while double quotes are used for strings.

For example...

 printf("%c \n",'a');
 printf("%s","Hello World");

Output

a  
Hello World

If you used these in vice versa case and used a single quote for string and double quotes for a character, this will be the result:

  printf("%c \n","a");
  printf("%s",'Hello World');

output :

For the first line. You will get a garbage value or unexpected value or you may get an output like this:

While for the second statement, you will see nothing. One more thing, if you have more statements after this, they will also give you no result.

Note: PHP language gives you the flexibility to use single and double-quotes easily.

Narah answered 11/8, 2015 at 13:57 Comment(0)
S
3

Use single quote with single char as:

char ch = 'a';

here 'a' is a char constant and is equal to the ASCII value of char a.

Use double quote with strings as:

char str[] = "foo";

here "foo" is a string literal.

Its okay to use "a" but its not okay to use 'foo'

Sortilege answered 10/9, 2010 at 9:50 Comment(0)
S
3

Single quotes are denoting a char, double denote a string.

In Java, it is also the same.

Siobhan answered 5/8, 2014 at 1:7 Comment(1)
This doesn't really add any value to the question, since this information has already been encompassed in the other answers.Chemaram
A
2

While I'm sure this doesn't answer what the original asker asked, in case you end up here looking for single quote in literal integers like I have...

C++14 added the ability to add single quotes (') in the middle of number literals to add some visual grouping to the numbers.

constexpr int oneBillion = 1'000'000'000;
constexpr int binary = 0b1010'0101;
constexpr int hex = 0x12'34'5678;
constexpr double pi = 3.1415926535'8979323846'2643383279'5028841971'6939937510;
Abandoned answered 29/9, 2021 at 20:9 Comment(0)
P
1

In C & C++ single quotes is known as a character ('a') whereas double quotes is know as a string ("Hello"). The difference is that a character can store anything but only one alphabet/number etc. A string can store anything. But also remember that there is a difference between '1' and 1. If you type cout<<'1'<<endl<<1; The output would be the same, but not in this case:

cout<<int('1')<<endl<<int(1);

This time the first line would be 48. As when you convert a character to an int it converts to its ascii and the ascii for '1' is 48. Same, if you do:

string s="Hi";
s+=48; //This will add "1" to the string
s+="1"; This will also add "1" to the string
Passim answered 5/11, 2020 at 16:47 Comment(0)
S
1

different way to declare a char / string

char char_simple = 'a'; // bytes 1 : -128 to 127 or 0 to 255
signed char char_signed = 'a'; // bytes 1: -128 to 127
unsigned char char_u = 'a';  // bytes 2: 0 to 255

// double quote is for string.
char string_simple[] = "myString";
char string_simple_2[] = {'m', 'S', 't', 'r', 'i', 'n', 'g'};
char string_fixed_size[8] = "myString";
char *string_pointer = "myString"; 
char string_poionter_2 = *"myString";

printf("char = %ld\n", sizeof(char_simple));
printf("char_signed = %ld\n", sizeof(char_signed));
printf("char_u = %ld\n", sizeof(char_u));

printf("string_simple[] = %ld\n", sizeof(string_simple));
printf("string_simple_2[] = %ld\n", sizeof(string_simple_2));
printf("string_fixed_size[8] = %ld\n", sizeof(string_fixed_size));
printf("*string_pointer = %ld\n", sizeof(string_pointer));
printf("string_poionter_2 = %ld\n", sizeof(string_poionter_2));

Socioeconomic answered 23/1, 2023 at 18:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.