signedness

Type conversion - unsigned to signed int/char

妖精的绣舞 提交于 2019-11-26 01:49:09
问题 I tried the to execute the below program: #include <stdio.h> int main() { signed char a = -5; unsigned char b = -5; int c = -5; unsigned int d = -5; if (a == b) printf(\"\\r\\n char is SAME!!!\"); else printf(\"\\r\\n char is DIFF!!!\"); if (c == d) printf(\"\\r\\n int is SAME!!!\"); else printf(\"\\r\\n int is DIFF!!!\"); return 0; } For this program, I am getting the output: char is DIFF!!! int is SAME!!! Why are we getting different outputs for both? Should the output be as below ? char is

Type conversion - unsigned to signed int/char

試著忘記壹切 提交于 2019-11-26 00:56:02
I tried the to execute the below program: #include <stdio.h> int main() { signed char a = -5; unsigned char b = -5; int c = -5; unsigned int d = -5; if (a == b) printf("\r\n char is SAME!!!"); else printf("\r\n char is DIFF!!!"); if (c == d) printf("\r\n int is SAME!!!"); else printf("\r\n int is DIFF!!!"); return 0; } For this program, I am getting the output: char is DIFF!!! int is SAME!!! Why are we getting different outputs for both? Should the output be as below ? char is SAME!!! int is SAME!!! A codepad link . This is because of the various implicit type conversion rules in C. There are