How does 'ab' was converted to 24930 when stored in char?
#include<stdio.h>
int main(){
char c = 'ab';
c = toupper(c);
printf("%c", c);
return 0;
}
GCC compiler warning : Overflow in conversion from 'int' to 'char' changes value from '24930' to '98'
Output : B
If possible please explain how char handled the multiple characters here.
From the C Standard (6.4.4.4 Character constants)
10 An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.
So this character constant 'ab' is stored as an object of the type int
. Then it is assigned to ab object of the type char
like in this declaration
char c = 'ab';
then the least significant byte of the object of the type int
is used to initialize the object c
. It seems in your case the character 'b'
was stored in this least significant byte and was assigned to the object c
.