Does the ASCII value '\ 0' match the ASCII value of 0?

I found out that the ASCII value '\ 0' is 0 and the ASCII value 0 is 0x30, but when I try to print their ASCII value using printf, I get the same result:

printf("\'\\0\' : %d\n", '\0'); printf("\'\\0\' in hex : %x\n", '\0'); printf("0 : %d\n", 0); printf("0 in hex: %x\n", 0); 

exit:

 '\0' : 0 '\0' in hex : 0 0 : 0 0 in hex: 0 

why?

+4
source share
7 answers

The ASCII character '0' is different from the number 0 . You print the integer 0 in the second pair of printf instead of '0' .

Try the following:

 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 do not need to hide ' inside strings. That is, "'" excellent, and you do not need to write "\'"

+7
source

You confuse 0, '\ 0' and '0'.

The first two of them are one and the same; they just represent an int with a value of 0.

'0', however, is different and represents an int with the value of the character '0', which is 48 .

+4
source

Yes, the character literal '\0' has a value of 0 , and it does not depend on the character set.

(K & R2, 2.3) "The character constant '\ 0' represents a character with a value of zero, a zero character. '\ 0' is often written instead of 0 to emphasize the characteristic nature of some expression, but the numerical value is 0."

+3
source

The ASCII value for '\ 0' is indeed 0. But '\ 0' is different from "0" (note the backslash, which is the escape character).

0
source
 printf("0 : %d\n", '0'); 

Type in this way, you get 48.

0
source

Your last line should be

 printf("0 in hex: %x\n", '0'); 
0
source

All of these answers do not seem to match the dot \0 ...

Try the following:

 printf("This line\0 might not completely appear"); 

This is the reason \0

So you can define a string with a terminating null character ...

 #define TEST_STRING "Test" 

very different from

 #define TEST_STRING "Test\0" 

the latter is useful in some cases.

0
source

All Articles