[c++] Single quotes vs. double quotes in C or C++

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

This question is related to c++ c syntax char string-literals

The answer is


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 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' };

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'


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


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.


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

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


  1. single quote is for character;
  2. double quote is for string.

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’

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.


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

In Java, it is also the same.


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to syntax

What is the 'open' keyword in Swift? Check if returned value is not null and if so assign it, in one line, with one method call Inline for loop What does %>% function mean in R? R - " missing value where TRUE/FALSE needed " Printing variables in Python 3.4 How to replace multiple patterns at once with sed? What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript? How can I fix MySQL error #1064? What do >> and << mean in Python?

Examples related to char

How can I convert a char to int in Java? C# - How to convert string to char? How to take character input in java Char Comparison in C Convert Char to String in C cannot convert 'std::basic_string<char>' to 'const char*' for argument '1' to 'int system(const char*)' How to get the real and total length of char * (char array)? Why is conversion from string constant to 'char*' valid in C but invalid in C++ char *array and char array[] C++ - How to append a char to char*?

Examples related to string-literals

String in function parameter How can I get double quotes into a string literal? What is the backslash character (\\)? How to use Macro argument as string literal? What does the symbol \0 mean in a string-literal? Single quotes vs. double quotes in C or C++ python: SyntaxError: EOL while scanning string literal Difference between string object and string literal Windows path in Python C++ multiline string literal