I learned the ASCII value of '\0' is 0, and the ASCII value of 0 is 0x30, but when I try to print their ASCII value using printf, I get the same output:
printf("\'\\0\' : %d\n", '\0');
printf("\'\\0\' in hex : %x\n", '\0');
printf("0 : %d\n", 0);
printf("0 in hex: %x\n", 0);
output:
'\0' : 0
'\0' in hex : 0
0 : 0
0 in hex: 0
why?
The ASCII character '0' is different than the number 0. You are printing the integer 0 in your second pair of printfs instead of '0'.
Try this:
printf("'\\0' : %d\n", '\0');
printf("'\\0' in hex : %x\n", '\0');
printf("'0' : %d\n", '0');
printf("'0' in hex: %x\n", '0');
Also, you don't need to escape ' inside strings. That is, "'" is fine and you don't need to write "\'"
You confuse 0, '\0', and '0'.
The first two of these are the same thing; they just represent an int with value 0.
'0', however, is different, and represents an int with the value of the '0' character, which is 48.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With