Alex Rivera | Logout

What is the difference between 'a' (with single quotes) and "a" (with double quotes)?

Asked 2012-07-01T02:25:03.107
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';
Edit
Report

2 Answers

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.

answered 2012-07-01T02:28:43.163
0

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

answered 2012-07-01T02:32:18.363

Your Answer