16

I am learning C++ and have got a question that I cannot find the answer to.

What is the difference between a char constant (using single quotes) and a string constant (with double quotes)?

All my search results related to char arrays vs std::string. I am after the difference between 'a' and "a".

Would there be a difference in doing the following:

cout << "a";
cout << 'a';
Niall
  • 30,036
  • 10
  • 99
  • 142
james
  • 477
  • 2
  • 5
  • 10

6 Answers6

37

'a' is a character literal. It's of type char, with the value 97 on most systems (the ASCII/Latin-1/Unicode encoding for the letter a).

"a" is a string literal. It's of type const char[2], and refers to an array of 2 chars with values 'a' and '\0'. In most, but not all, contexts, a reference to "a" will be implicitly converted to a pointer to the first character of the string.

Both

cout << 'a';

and

cout << "a";

happen to produce the same output, but for different reasons. The first prints a single character value. The second successively prints all the characters of the string (except for the terminating '\0') -- which happens to be the single character 'a'.

String literals can be arbitrarily long, such as "abcdefg". Character literals almost always contain just a single character. (You can have multicharacter literals, such as 'ab', but their values are implementation-defined and they're very rarely useful.)

(In C, which you didn't ask about, 'a' is of type int, and "a" is of type char[2] (no const)).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Thanks, is the string literal the same as the type included with #include ? – james Jul 01 '12 at 02:31
  • @james, No, the latter is a class that encapsulates the former, making it much easier to use. – chris Jul 01 '12 at 02:33
  • 2
    @james: No, a string literal is of type `const char[]`, but `` defines a type `std::string`. C++ inherited string literals from C; `std::string` is specific to C++. – Keith Thompson Jul 01 '12 at 02:33
  • 3
    @chris: Except that a `std::string` isn't terminated by a `'\0'` null character. – Keith Thompson Jul 01 '12 at 02:36
  • 2
    I do think it's worth noting that this is an area where C differs from C++ in a way many people don't expect. As you (entirely correctly) said, in C++ a character literal like `'a'` has type `char` -- but in C, it has type `int`. – Jerry Coffin Jul 01 '12 at 03:28
4

"a" is an array of characters that just happens to only contain one character, or two if you count the \0 at the end. 'a' is one character. They're not the same thing. For example:

#include <stdio.h>

void test(char c) {
    printf("Got character %c\n", c);
}

void test(const char* c) {
    printf("Got string %s\n", c);
}

int main() {
    test('c');
    test("c");
}

This will use two different overloads; see http://codepad.org/okl0UcCN for a demo.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1

Single quotes are used to surround character literals. Double quotes are used to surround string (character array) literals.

Many interfaces, such as cout <<, accept either.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

A single quoted 'a' is a literal of type char. A double quoted "a" is a null-terminated string literal of chars.

ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
0
  • "a" -> it denotes it is a string. in c++ string is a collection of characters array.
  • so string is terminated by delimiter '\0' it indicates the end of string.
  • so its size would be 2 because 1-byte for "a" and 1-byte for '\0'

where in case of 'a'-> it is single character. so its size would be 1-byte.

char str[]="a"; 

or

char *ptr = "c";

for  'c' -> char c = 'a';

or we can write as well

char c = 97;
Manolo
  • 24,020
  • 20
  • 85
  • 130
-1

'a' - 1) Character constant 2) size - 1 Character 3) Not a collection of Character array

"a" -1) String literals 2) size - 2 Character 3) collection of Character array

ganga
  • 1
  • 1