C语言中 39 0 39 ==0的值为,c-'\ 0'计算为false,“ \ 0”计算为tru

c-'\ 0'计算为false,“ \ 0”计算为tru

受到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

10个解决方案

112 votes

回想一下字符串文字在C中的工作原理-'\0'是一个包含两个零字节(您要的字节,末尾是隐式的字节)的字符数组。 在对0测试进行评估时,它会衰减为指向其第一个字符的指针。 该指针不是NULL,因此在用作条件时将其视为true。

'\0'是零,仅相当于0。它是一个整数,为零,因此用作条件时被视为false。

user253751 answered 2019-12-25T22:34:38Z

34 votes

首先,请记住在C语言中,

零为假,非零为真。

对于指针类型,NULL为false,非\0为true。

正如其他人所说,NULL与整数文字\0相同,因此为假(请参阅上面的第一个要点,以了解原因)。

NULL是一个字符串文字,其中包含两个\0字符(一个已显式添加,另一个已隐式包含,将由编译器添加)。 字符串文字将存储在只读存储器中的某个位置。 当您使用NULL时,它将转换为指向其第一个元素的指针†。 这通常称为“阵列衰减”。 (这就是char* str = "string";之类的东西起作用的原因)。

因此,您正在有效地检查字符串文字的第一个字符的地址。 由于字符串文字的地址始终为非NULL,因此if始终为true(请参见上面的第二个要点以了解原因)。

†:阵列的这种“衰减”并不总是发生。 看到数组不衰减为指针的异常?

Spikatrix answered 2019-12-25T22:35:26Z

15 votes

"\0"是一个数字:NULL,因此将其评估为false(0 = false,!0 = true)。

但是"\0"是指向存储实际字符串的只读节的指针,因此不是真正的指针NULL。

FedeWar answered 2019-12-25T22:35:51Z

3 votes

首先,查看两个条件,'\0'是整数类型的常量,表示空字符C,与false相同。"\0"是字符串文字,其中包含2个字节,一个指定的字节和一个空终止符字节 隐式添加。 作为字符串文字,指针不能为true。

其次,在C中,对于'\0'语句的条件,所有非零值都评估为false,零值则评估为"\0"。

根据此规则,很明显'\0'是false,而"\0"被评估为true。

fluter answered 2019-12-25T22:36:20Z

1 votes

首先,请注意,False的十六进制值为'\0',True的值为0x00以外的任何其他值。

'\0'是一个字符串,末尾有一个字符和空终止符0x00。 因此,它是一个字符指针,指向2个字节的数组:0x00 != 0x00。在该数组中,第一个是字符,另一个是空终止符。

编译后(未经优化),该字符指针被临时分配给存储器中指向这两个字节中第一个字节的地址。 该地址可以是例如十六进制的'\0'。 因此,编译器(大多数都是编译器)实际上将这两个值写入内存。 由于字符串实际上是该字符串的第一个字节的地址,因此我们的表达式将解释为0x00。 因此,很明显0x00 != 0x00是真实的。

'\0'就是0x00(十六进制)。 0x00 != 0x00是错误的。

该答案是针对具有16位寻址的8位数据体系结构编写的。 希望对您有所帮助。

Bora answered 2019-12-25T22:37:00Z

0 votes

'\ 0'是一个空字符,其值为0。它用于终止字符串。 因此,它被认为是错误的。

“ \ 0”是空字符串或空字符串。 字符串中唯一的字符是终止字符串的空字符,因此认为是真的。

msc answered 2019-12-25T22:37:24Z

0 votes

我们可以用两种不同的C概念来解决上述问题

C语言中if(condition)的工作

C中字符和字符串文字的区别

1. if(条件)在C语言中的工作如果(条件)

在C语言中,如果条件适用于0(零)和非零基数。

如果给定条件的结果为零,则C认为给定条件为假。

如果给定条件的结果为非零,则C认为给定条件为真。

2. C语言中字符和字符串文字的区别

在C中,字符串文字是用双引号(“”)括起来的字符,而字符文字是在单引号(“)中括起来的字符,最小长度是一个字符,最大长度是两个字符。

另一个重要的一点是,在C语言中,如果将“ \ 0”(空)转换为int(整数),则将获得0(零),而我们无法将“ \ 0”隐式或显式转换为int。 因为“ \ 0”是字符串,而“ \ 0”是字符。

并根据字符串IF条件工作逻辑,如果条件返回0或false,则表示条件为false; 如果条件返回非零,则表示条件为真。

因此,根据点1和点2我们最终可以得出结论

如果('\ 0')printf(“ \'\ 0 \'!= false \ n”); //条件变为假

如果(“ \ 0”)printf(“ \” \ 0 \“!= false \ n”); //条件变为真

Ravi answered 2019-12-25T22:38:43Z

0 votes

'\ 0'是一个等于数字零的字符。 “ \ 0”是一个字符串,我们通常在字符串的末尾添加“ \ 0”。 不要在条件语句中使用“ \ 0”或“ \ 0”,因为这很容易混淆。

建议以下用法:

if (array[0] != 0)

{

}

if (p != 0)

{

}

if (p != NULL)

{

}

G.Mather answered 2019-12-25T22:39:08Z

0 votes

通过示例查看。

#include

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;

}

Krishna answered 2019-12-25T22:39:28Z

0 votes

简单的事情是ATLEAST 0(整数)和0.0(浮点数或双精度数)在C中的值为FALSE。

'\ 0'是整数0。

“ \ 0”是一个字符数组。 在数组中插入多少个字符或这些字符是什么都没关系。

因此,'\ 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");}

输出:

Bhartendu Kumar answered 2019-12-25T22:40:22Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值