Answer to Question #15393 in C++ for Vineet

Question #15393
What will be the output of cout<<'aa'; and why? Note: within single quotes only two characters
Expert's answer
Something between single quotes (as opposed to double quotes), i.e. 'something', is actually a different way to write an integer literal.

Usually you put only a single character between single quotes. Like 'R' or 's' or '4'. Those are the same as the values 82, 115 and 52, respectively. I.e. their ASCII values.

But integers can be bigger than 255. So you can keep adding chars between the single quotes. And those additional chars fill up the more significant bytes. So 'aa' are two bytes, each the value 97 (ASCII code for lowercase a), and the more significant byte is multiplied by 256.

So you get 97 * 256 + 97, which is - you may have guessed it - 24929.

But you shouldn't use more than one char between single quotes in your programs, because they might not be portable. The reason is endianness. I.e. how different architectures order their bytes (least significant byte first or most significant byte first).

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


Assignment Expert
01.10.12, 15:20

You're welcome. We are glad to be helpful.
If you really liked our service please press like-button beside answer field. Thank you!

29.09.12, 19:51

Thank you very much for the explanation.

Leave a comment

Ask Your question

New on Blog