I learned the ASCII value of '
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:
我学习了“\0”的ASCII值为0,而ASCII值为0的值是0x30,但是当我尝试用printf打印它们的ASCII值时,我得到了相同的输出:
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?
为什么?
7 个解决方案
#1
7
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'.
ASCII字符'0'与数字0不同。您在第二对printfs中打印整数0而不是“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 "\'"
而且,你不需要逃离“内部字符串”。也就是说,"'"很好,你不需要写"\ "
#2
5
You confuse 0, '\0', and '0'.
你把0和0混淆了。
The first two of these are the same thing; they just represent an int with value 0.
前两个是一样的;它们只是表示一个值为0的整数。
'0', however, is different, and represents an int with the value of the '0' character, which is 48.
但是,“0”是不同的,表示一个带有“0”字符的值,即48。
#3
3
Yes, the character literal'\0' has value 0 and this is independent of the character set.
是的,字符文字'\0'有值0,这是独立于字符集的。
(K&R2, 2.3) "The character constant '\0' represents the character with value zero, the null character. '\0' is often written instead of 0 to emphasize the character nature of some expression, but the numeric value is just 0."
(K&R2,2.3)字符常数'\0'表示值为零的字符,空字符。“\0”通常是写而不是0来强调某些表达式的性质,但数值是0。
#4
0
The ASCII value for '\0' is indeed 0. But '\0' is different from '0' (notice the backslash, which is the escape symbol).
“\0”的ASCII值确实为0。但是“\0”不同于“0”(注意反斜杠,也就是escape符号)。
#5
0
printf("0 : %d\n", '0');
Print like this, you will get 48.
像这样打印,你会得到48。
#6
0
Your last line should be
最后一行应该是。
printf("0 in hex: %x\n", '0');
#7
0
All of these answers seem to miss the point of \0 ...
所有这些答案似乎都忽略了这一点……
Try this:
试试这个:
printf("This line\0 might not completely appear");
That's the reason for \0
这就是\0的原因。
So you can define a NULL terminated string ...
你可以定义一个空终止字符串…
#define TEST_STRING "Test"
is quite different from
非常不同于
#define TEST_STRING "Test\0"
the latter being useful in certain cases.
后者在某些情况下是有用的。
' 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:I learned the ASCII value of '
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:
我学习了“\0”的ASCII值为0,而ASCII值为0的值是0x30,但是当我尝试用printf打印它们的ASCII值时,我得到了相同的输出:
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?
为什么?
7 个解决方案
#1
7
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'.
ASCII字符'0'与数字0不同。您在第二对printfs中打印整数0而不是“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 "\'"
而且,你不需要逃离“内部字符串”。也就是说,"'"很好,你不需要写"\ "
#2
5
You confuse 0, '\0', and '0'.
你把0和0混淆了。
The first two of these are the same thing; they just represent an int with value 0.
前两个是一样的;它们只是表示一个值为0的整数。
'0', however, is different, and represents an int with the value of the '0' character, which is 48.
但是,“0”是不同的,表示一个带有“0”字符的值,即48。
#3
3
Yes, the character literal'\0' has value 0 and this is independent of the character set.
是的,字符文字'\0'有值0,这是独立于字符集的。
(K&R2, 2.3) "The character constant '\0' represents the character with value zero, the null character. '\0' is often written instead of 0 to emphasize the character nature of some expression, but the numeric value is just 0."
(K&R2,2.3)字符常数'\0'表示值为零的字符,空字符。“\0”通常是写而不是0来强调某些表达式的性质,但数值是0。
#4
0
The ASCII value for '\0' is indeed 0. But '\0' is different from '0' (notice the backslash, which is the escape symbol).
“\0”的ASCII值确实为0。但是“\0”不同于“0”(注意反斜杠,也就是escape符号)。
#5
0
printf("0 : %d\n", '0');
Print like this, you will get 48.
像这样打印,你会得到48。
#6
0
Your last line should be
最后一行应该是。
printf("0 in hex: %x\n", '0');
#7
0
All of these answers seem to miss the point of \0 ...
所有这些答案似乎都忽略了这一点……
Try this:
试试这个:
printf("This line\0 might not completely appear");
That's the reason for \0
这就是\0的原因。
So you can define a NULL terminated string ...
你可以定义一个空终止字符串…
#define TEST_STRING "Test"
is quite different from
非常不同于
#define TEST_STRING "Test\0"
the latter being useful in certain cases.
后者在某些情况下是有用的。
' is 0, and the