+ 2
Is "a" a char?
I have this question, while I was learning c++. And the right answer is "no". Why?
5 Antworten
+ 13
Double quotes contain strings.
Single quotes contain characters.
"a" is a string literal.
'a' is a character.
That is the original query of the question.
+ 2
Did it literately display to you:
"Is 'a' a char?" The obvious answer is no. Why?
Because it hasn't been defined as a character.
C++ has a character prefix to use, simply char, i believe.
I haven't reviewed on C++ in a while, so you may have to check back yourself...
+ 2
If you check the following in a compiler :
cout<< sizeof("a"); // Output - 2
While if you do :
cout<< sizeof('a'); // Output - 1
The reason that these differences are seen are just because "a" is a string, and stores a succeeded by a \0 terminating character (If one uses C-strings) (\0 occupies 1 byte as well). 'a' on the other hand is just a single character, and thus just occupies 1 byte.
If you have used the std::string class, the statement - sizeof("a") - will return an even larger value, thanks to the string class which dynamically allocates more memory for us than we need to prevent leaks...
0
yes but you need it in single quotations
0
Simply no. It is a string.