I could not identify how the following program outputs 6 and -250.
#include<stdio.h>
int main()
{
unsigned char p=-250;
printf("%d",p);
unsigned int p1=-250;
printf("%d",p1);
return 0;
}
Being an unsigned integer it has to output only the positive values.How does the p value outputs 6? Please help me understand.
printfis not typesafe. It prints whatever you ask it to, and%dsays “signed integer”. It is your responsibility to provide a varibale of matching type. Since theunsigned charis only 8 bits wide, the literal -250 wraps around to +6, which remains +6 when interpreted as a signed integer. Note thatcharandshort int(and their signed/unsigned counterparts) all get promoted toint-types when passed via variadic arguments.