Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The ASCII value of '\0' is same as ASCII value of 0?

Tags:

c

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?

like image 321
Victor S Avatar asked Jul 02 '12 13:07

Victor S


2 Answers

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 "\'"

like image 180
Shahbaz Avatar answered Oct 07 '22 01:10

Shahbaz


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.

like image 34
houbysoft Avatar answered Oct 06 '22 23:10

houbysoft