What will be the output of
cout<<'aa';
and why?
Note: within single quotes only two characters
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!
Learn more about our help with Assignments:
C++