[英]'\0' evaluates false, “\0” evaluates true
灵感来自K&R第5.5节中描述的程序:
void strcpy(char *s, char *t)
{
while(*s++ = *t++);
}
C程序
if ('\0') { printf("\'\\0\' -> true \n"); }
else { printf("\'\\0\' -> false\n"); }
if ("\0") { printf("\"\\0\" -> true \n"); }
else { printf("\"\\0\" -> false\n"); }
版画
'\0' -> false
"\0" -> true
为什么'\\0'
和"\\0"
在C中的评价方式不同?
clang版本3.8.0
回想一下C字符串文字是如何工作的 - "\\0"
是一个包含两个零字节的字符数组(你要求的那个,最后是隐含的字节)。 当评估if
测试时,它会衰减成指向其第一个字符的指针。 该指针不是NULL,因此当用作条件时它被认为是真的。
'\\0'
是数字零,相当于0
。 它是一个零的整数,因此当用作条件时它被认为是假的。
首先,你需要记住在C中,
NULL
为false且非NULL
为true。 正如其他人所说, '\\0'
与整数文字0
相同,因此是假的(参见上面的第一个要点知道原因)。
"\\0"
是一个包含两个\\0
字符的字符串文字(一个是您明确添加的,另一个是隐式的,将由编译器添加)。 字符串文字将存储在只读存储器中的某处。 当您使用"\\0"
,它将转换为指向其第一个元素 † 的指针 。 这通常被称为“ 阵列衰减 ”。 (这就是像char* str = "string";
类的东西的原因)。
因此, 您正在有效地检查字符串文字的第一个字符的地址 。 由于字符串文字的地址始终为非NULL
,因此if将始终为true(请参阅上面的第二个要点以了解原因)。
† :数组的“衰减”并不总是发生。 请参见异常数组不衰减成指针?
'\\0'
是一个数字: 0
,因此它被评估为false( 0
= false, !0
= true)。
但是"\\0"
是一个指向只读部分的指针,其中存储了实际的字符串,指针不是NULL
因为它是真的。
首先,看两个条件, '\\0'
是整数类型的常量,表示空字符C,它与0
相同。 "\\0"
是一个字符串文字,其中包含2个字节,指定的字节和隐式添加的空终止符字节。 作为字符串文字,指针不能为NULL
。
其次,在C中,对于if
语句的条件,所有非零都被评估为true
,零被评估为false
。
根据这条规则,很明显'\\0'
为false
, "\\0"
为true
。
首先,请注意False的十六进制值为0x00
,True为除0x00之外的任何其他值。
"\\0"
是一个字符串,末尾是Null Terminator '\\0'
。 所以它是一个字符指针,指向一个2字节的数组: ['\\0', '\\0']
。 在这个数组中,第一个是字符,另一个是空终止符。
在编译(没有优化)之后,该字符指针被临时分配给存储器中指向这两个字节的第一个字节的地址。 例如,该地址可以是十六进制的0x18A6
。 因此编译器(大多数)实际上将这两个值写入内存。 因为字符串实际上是该字符串的第一个字节的地址,所以我们的表达式被解释为0x18A6 != false
。 所以,很清楚0x18A6 != 0x00
是真的。
'\\0'
只是十六进制的0x00
。 0x00 != 0x00
为False。
这个答案是针对具有16位寻址的8位数据架构而编写的。 我希望有所帮助。
'\\ 0'是一个空字符,其值为0 。 它用于终止一串字符。 所以它被认为是假的。
“\\ 0”是空或空 字符串 。 字符串中唯一的字符是空字符,它终止字符串。所以它被认为是真的。
我们可以在C的两个不同概念中清楚上述问题
1.在C中 if(条件)的工作 if(条件)
在C语言中,如果条件适用于0(零)和非零基础。
如果给定条件的结果为零,则C认为给定条件为假。
如果给定条件的结果为非零,则C认为给定条件为真。
2. C中字符和字符串文字的差异
在C中,字符串文字是用双引号(“”)括起来的字符串,而字符文字是用单引号('')括起来的字符,最小长度是一个字符,最大长度是两个字符。
另外重要的一点是,在C,如果我们把“\\ 0”(空)为int(整数),那么我们将得到0(零),虽然我们不能将“\\ 0”或明或暗地诠释。 因为“\\ 0”是字符串,而“\\ 0”是字符。
并且根据字符串IF条件工作逻辑,如果condition返回0或false,则表示condition为false; 如果条件返回非零,则表示条件为真。
因此,根据第1点和第2点,我们最终可以得出结论
if('\\ 0')printf(“\\'\\ 0 \\'!= false \\ n”); //条件变为假
if(“\\ 0”)printf(“\\”\\ 0 \\“!= false \\ n”); //条件变为真
'\\ 0'是一个等于数字零的字符。 “\\ 0”是一个字符串,我们通常在字符串的末尾添加'\\ 0'。 不要在条件语句中使用'\\ 0'或“\\ 0”,因为它非常混乱。
建议使用以下用法:
if (array[0] != 0)
{
}
if (p != 0)
{
}
if (p != NULL)
{
}
看看这个例子..
#include <stdio.h>
int main()
{
printf( "string value\n" );
//the integer zero
printf( "0.........%d\n" , 0 );
//the char zero, but chars are very small ints, so it is also an int
//it just has some special syntax and conventions to allow it to seem
//like a character, it's actual value is 48, this is based on the
//ASCII standard, which you can look up on Wikipedia
printf( "'0'.......%d\n" , '0' );
//because it is an integer, you can add it together,
//'0'+'0' is the same as 48+48 , so it's value is 96
printf( "'0'+'0'...%d\n" , '0'+'0' );
//the null terminator, this indicates that it is the end of the string
//this is one of the conventions strings use, as a string is just an array
//of characters (in C, at least), it uses this value to know where the array
//ends, that way you don't have to lug around another variable to track
//how long your string is. The actual integer value of '\0' is zero.
printf( "'\\0'......%d\n" , '\0' );
//as stated, a string is just an array of characters, and arrays are tracked
//by the memory location of their first index. This means that a string is
//actually a pointer to the memory address that stores the first element of
//the string. We should get some large number, a memory address
printf( "\"0\".......%d\n" , "0" );
//a string is just an array of characters, so lets access the character
//in position zero of the array. it should be the character zero, which
//has an integer value of 48
printf( "\"0\"[0]....%d\n" , "0"[0] );
//and the same thing for the empty string
printf( "\"\\0\"[0]...%d\n" , "\0"[0] ); //equal to '\0'
//we also said a string is just a pointer, so we should be able to access
//the value it is pointing to (the first index of the array of characters)
//by using pointers
printf( "*\"0\"......%d\n" , *"0" );
return 0;
}
简单的事情是ATLEAST 0(int)和0.0(float或double)在C中具有FALSE值。
'\\ 0'是整数0。
“\\ 0”是一个字符数组。 在数组中INSIDE有多少个字符或那些字符是什么并不重要。
因此,'\\ 0'的计算结果为0,如77-77,计算结果为0. 0为假。
int x; x = '\\0'; printf("X has a value : %d");
输出:
x的值为:0
和代码:
if(0){printf("true");}
else{printf("false");}
输出:
假
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.