+ 1

Why output is 9 ? Explain please

#include <stdio.h> int main() { char c = 255; c=c+10; printf("%d",c); return 0; }

7th Mar 2022, 6:35 PM
saurav
saurav - avatar
5 Antworten
+ 5
As you used %d so c will be converted into number which will be -1 so c = -1 + 10 = 9 If you want to see then print this: cout << (int) c; // this will give -1
7th Mar 2022, 6:37 PM
A͢J
A͢J - avatar
+ 2
Because char is 8bit and can hold a maximum of 255 (1111 1111)binary so adding 1 to it caused it to over flow like (1 0000 0000)b and the most significant bit is trimmed so adding the remaining 9 it becomes 0+9 =9
8th Mar 2022, 11:46 PM
Z3ro
0
A͢J Why -1 ?
7th Mar 2022, 6:38 PM
saurav
saurav - avatar
0
saurav learn about ASCII value and type conversion, you'll get your answer.
7th Mar 2022, 6:47 PM
Kashyap Kumar
Kashyap Kumar - avatar