#include<stdio.h>
#include<conio.h>
main()
{
int i=369;
printf("%c",i);
getch();
}
O/p
===
q
Here , getting 'q' as a output .
My doubt is the ascii character limit is 255 . But why it is giving 'q' as output ?
#include<stdio.h>
#include<conio.h>
main()
{
int i=369;
printf("%c",i);
getch();
}
O/p
===
q
Here , getting 'q' as a output .
My doubt is the ascii character limit is 255 . But why it is giving 'q' as output ?
The "%c"
in printf()
takes the int
parameter 369
and converts it to an unsigned char
which has the value 369 & 255
or 113
. Character code 113 corresponds to 'q'
on a system using ASCII encoding. Thus 'q'
is printed.
C11dr §7.21.6.1 8 c conversion specifier
"If no l length modifier is present, the int argument is converted to an unsigned char, and the resulting character is written."
[Edit]
Typical C systems have an 8-bit char
which allows for 256 combinations, hence the above & 255
(Some systems have other char
sizes). Typical C systems assign values 0 to 127 to the ASCII character set - which is only defined for codes 0 to 127. The text that may print out with values outside that range varies.
ascii's limit is 255, however 369=0000 0001 0111 0001, while q's ascii code is 0111 0001, see the last 8 bits? You got it!