62 568
Assignments Done
99%
Successfully Done
In June 2018

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. &#039;something&#039;, is actually a different way to write an integer literal.

Usually you put only a single character between single quotes. Like &#039;R&#039; or &#039;s&#039; or &#039;4&#039;. 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 &#039;aa&#039; 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&#039;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!

Comments

Assignment Expert
01.10.12, 14: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!

Vineet
29.09.12, 18:51

Thank you very much for the explanation.

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions