+ 2

Is "a" a char?

I have this question, while I was learning c++. And the right answer is "no". Why?

4th Sep 2017, 4:12 PM
OWNER
OWNER - avatar
5 odpowiedzi
+ 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.
4th Sep 2017, 4:41 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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...
4th Sep 2017, 4:16 PM
ghostwalker13
ghostwalker13 - avatar
+ 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...
6th Sep 2017, 5:31 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
0
yes but you need it in single quotations
4th Sep 2017, 11:18 PM
HJ🐓TDM
0
Simply no. It is a string.
5th Sep 2017, 8:09 AM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar