简体   繁体   English

'\ 0'计算为false,“\ 0”计算为true

[英]'\0' evaluates false, “\0” evaluates true

Inspired by a program described in K&R section 5.5: 灵感来自K&R第5.5节中描述的程序:

void strcpy(char *s, char *t)
{
    while(*s++ = *t++);
}

C program C程序

if ('\0') { printf("\'\\0\' -> true \n"); }
else      { printf("\'\\0\' -> false\n"); }

if ("\0") { printf("\"\\0\" -> true \n"); }
else      { printf("\"\\0\" -> false\n"); }

prints 版画

'\0' -> false
"\0" -> true

Why do '\\0' and "\\0" evaluate differently in C? 为什么'\\0'"\\0"在C中的评价方式不同?

clang version 3.8.0 clang版本3.8.0

Recall how string literals work in C - "\\0" is a character array containing two zero bytes (the one you asked for, and the implicit one at the end). 回想一下C字符串文字是如何工作的 - "\\0"是一个包含两个零字节的字符数组(你要求的那个,最后是隐含的字节)。 When evaluated for the if test, it decays into a pointer to its first character. 当评估if测试时,它会衰减成指向其第一个字符的指针。 This pointer is not NULL, so it's considered true when used as a condition. 该指针不是NULL,因此当用作条件时它被认为是真的。

'\\0' is the number zero, equivalent to just 0 . '\\0'是数字零,相当于0 It's an integer which is zero, so it's considered false when used as a condition. 它是一个零的整数,因此当用作条件时它被认为是假的。

First of all, you need to keep in mind that in C, 首先,你需要记住在C中,

  • Zero is false and non-zero is true. 零为假,非零为真。
  • For pointer types, NULL is false and non- NULL is true. 对于指针类型, NULL为false且非NULL为true。

'\\0' , as others have said, is the same as the integer literal 0 and hence is false (See first bullet point above to know why). 正如其他人所说, '\\0'与整数文字0相同,因此是假的(参见上面的第一个要点知道原因)。

"\\0" is a string literal that contains two \\0 characters (One which you have explicitly added and the other, which is implicit and will be added by the compiler). "\\0"是一个包含两个\\0字符的字符串文字(一个是您明确添加的,另一个是隐式的,将由编译器添加)。 The string literal will be stored somewhere in read-only memory. 字符串文字将存储在只读存储器中的某处。 When you use "\\0" , it gets converted to a pointer to its first element . 当您使用"\\0" ,它将转换为指向其第一个元素 的指针 This is commonly referred to as " array decay ". 这通常被称为“ 阵列衰减 ”。 (This is the reason why stuff like char* str = "string"; works). (这就是像char* str = "string";类的东西的原因)。

So, you are effectively checking the address of the first character of the string literal . 因此, 您正在有效地检查字符串文字的第一个字符的地址 Since the address of the string literal will always be non- NULL , the if will always be true (See second bullet point above to know why). 由于字符串文字的地址始终为非NULL ,因此if将始终为true(请参阅上面的第二个要点以了解原因)。


: This "decay" of arrays does not always happen. :数组的“衰减”并不总是发生。 See Exception to array not decaying into a pointer? 请参见异常数组不衰减成指针?

'\\0' is a number: 0 , so it is evaluated as false ( 0 = false, !0 = true). '\\0'是一个数字: 0 ,因此它被评估为false( 0 = false, !0 = true)。

But "\\0" is a pointer to a read-only section where the actual string is stored, the pointer is not NULL ergo it's true. 但是"\\0"是一个指向只读部分的指针,其中存储了实际的字符串,指针不是NULL因为它是真的。

First, looking at the two conditions, '\\0' is a constant of type integer, which denotes the null character C, which is the same as 0 . 首先,看两个条件, '\\0'是整数类型的常量,表示空字符C,它与0相同。 While "\\0" is a string literal, which contains 2 bytes, the one specified and the null terminator byte implicitly added. "\\0"是一个字符串文字,其中包含2个字节,指定的字节和隐式添加的空终止符字节。 Being a string literal, the pointer cannot be NULL . 作为字符串文字,指针不能为NULL

Second, in C, for the condition of if statement, everything non-zero is evaluated as true , and zero is evaluated as false . 其次,在C中,对于if语句的条件,所有非零都被评估为true ,零被评估为false

According to this rule, it will be clear that '\\0' is false , and "\\0" evaluated as true . 根据这条规则,很明显'\\0'false"\\0"true

First of all, please note that the hexadecimal value of False is 0x00 and True is any other value than 0x00. 首先,请注意False的十六进制值为0x00 ,True为除0x00之外的任何其他值。

"\\0" is a string with a character and Null Terminator '\\0' at the end. "\\0"是一个字符串,末尾是Null Terminator '\\0' So it is a character pointer, pointing to an array of 2 bytes: ['\\0', '\\0'] . 所以它是一个字符指针,指向一个2字节的数组: ['\\0', '\\0'] In this array, the first one is the character and the other one is the null terminator. 在这个数组中,第一个是字符,另一个是空终止符。

After compiling (without optimizing), this character pointer is temporarily assigned to an address in the memory pointing to the first byte of these two bytes. 在编译(没有优化)之后,该字符指针被临时分配给存储器中指向这两个字节的第一个字节的地址。 This address might be, for example, 0x18A6 in hexadecimal. 例如,该地址可以是十六进制的0x18A6 So the compiler (most of them) actually writes these two values to the memory. 因此编译器(大多数)实际上将这两个值写入内存。 Because a string is actually the address of the first byte of that string, our expression is interpreted as 0x18A6 != false . 因为字符串实际上是该字符串的第一个字节的地址,所以我们的表达式被解释为0x18A6 != false So, it is clear 0x18A6 != 0x00 is True. 所以,很清楚0x18A6 != 0x00是真的。

'\\0' is simply 0x00 in hexadecimal. '\\0'只是十六进制的0x00 0x00 != 0x00 is False. 0x00 != 0x00为False。

This answer is written for 8-bit data architecture with 16-bit addressing. 这个答案是针对具有16位寻址的8位数据架构而编写的。 I hope that helps. 我希望有所帮助。

'\\0' is a null character which has the value of 0 . '\\ 0'是一个字符,其值为0 It is used to terminate a string of characters. 它用于终止一串字符。 So it's consider false. 所以它被认为是假的。

"\\0" is a null or empty string . “\\ 0” 字符串 The only character in the string is the null character which terminates the string.So it's consider true. 字符串中唯一的字符是空字符,它终止字符串。所以它被认为是真的。

We can clear above problem in two different concept of C 我们可以在C的两个不同概念中清楚上述问题

  1. Working of if( condition ) in C 在C中使用if(条件)
  2. Difference of Character & String Literals in C C语言中字符和字符串文字的差异

1. Working of if( condition ) in C if( condition ) 1.在C中 if(条件)的工作 if(条件)

In C Language, if condition works on 0(Zero) and Non-Zero base. 在C语言中,如果条件适用于0(零)和非零基础。

If the result of the given condition is Zero, then C consider that given condition is false. 如果给定条件的结果为零,则C认为给定条件为假。

If the result of the given condition is Non-Zero then C consider that given condition is true. 如果给定条件的结果为非零,则C认为给定条件为真。

2. Difference of Character & String Literals in C 2. C中字符和字符串文字的差异

In C, String literals are those which enclosed in Double quotation marks (""), while Character literals are those which enclosed in Single quotation marks ('') and minimum length is one character and max length is two character. 在C中,字符串文字是用双引号(“”)括起来的字符串,而字符文字是用单引号('')括起来的字符,最小长度是一个字符,最大长度是两个字符。

Another important point is that in C, if we convert '\\0' (null) to int (Integer), then we will get 0(Zero), while we cannot convert "\\0" to int implicitly or explicitly. 另外重要的一点是,在C,如果我们把“\\ 0”(空)为int(整数),那么我们将得到0(零),虽然我们不能将“\\ 0”或明或暗地诠释。 Because "\\0" is string while '\\0' is character. 因为“\\ 0”是字符串,而“\\ 0”是字符。

And according to the string IF condition working logic, if condition returns 0 or false, it means condition is false; 并且根据字符串IF条件工作逻辑,如果condition返回0或false,则表示condition为false; in case of condition returns non-zero, it means condition is true. 如果条件返回非零,则表示条件为真。

So, according to point 1 and 2 finally we can conclude that 因此,根据第1点和第2点,我们最终可以得出结论

if ('\\0') printf("\\'\\0\\' != false\\n"); if('\\ 0')printf(“\\'\\ 0 \\'!= false \\ n”); //condition becomes false //条件变为假

if ("\\0") printf("\\"\\0\\" != false\\n"); if(“\\ 0”)printf(“\\”\\ 0 \\“!= false \\ n”); //condition becomes true //条件变为真

'\\0' is a char that is equal to number zero. '\\ 0'是一个等于数字零的字符。 "\\0" is a string and we usually add '\\0' at the end of a string. “\\ 0”是一个字符串,我们通常在字符串的末尾添加'\\ 0'。 Don't use '\\0' or "\\0" in a conditional statements because it's quite confusing. 不要在条件语句中使用'\\ 0'或“\\ 0”,因为它非常混乱。

The following usage is suggested: 建议使用以下用法:

if (array[0] != 0)
{

}

if (p != 0)
{

}

if (p != NULL)
{

}

Check out this with examples.. 看看这个例子..

#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; 
}

The simple thing is ATLEAST 0 (int) and 0.0 (float or double) have FALSE value in C. 简单的事情是ATLEAST 0(int)和0.0(float或double)在C中具有FALSE值。

'\\0' is integer 0. '\\ 0'是整数0。

"\\0" is an array of characters. “\\ 0”是一个字符数组。 It does not matter that INSIDE the array how many Characters are there or what are those characters. 在数组中INSIDE有多少个字符或那些字符是什么并不重要。

So, '\\0' evaluates to 0 like 77-77 evaluates to 0. And 0 is false. 因此,'\\ 0'的计算结果为0,如77-77,计算结果为0. 0为假。

int x; x = '\\0'; printf("X has a value : %d"); Output: 输出:


x has a value : 0 x的值为:0

And the code: 和代码:

if(0){printf("true");}

else{printf("false");}

Output: 输出:


false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM