c语言中 if 0,c语言中 if(x) 、if(0) 、if(1)

解释

if 语句里面包含真和非真,但是如果我们没有写清楚真和非真的话,会如何呢?

if(x)

相当于

if(x != 0)

如果是指针的话,相当于

if(x != NULL)

if(1)

相当于

if(1 != 0)

还有

if(0)

相当于

if(0 != 0)

举个例子

#include

int main()

{

int x = 2;

printf("x value:%d\n",x);

printf("code:'x==2'\nvalue:%d\n" ,x==2);

printf("code:'x!=2'\nvalue:%d\n" ,x!=2);

printf("code:'x=3'\nvalue:%d\n" ,x=3);

printf("code:'x-1'\nvalue:%d\n" ,x-1);

printf("code:'!x'\nvalue:%d\n" ,!x);

printf("code:'!1'\nvalue:%d\n" ,!1);

printf("code:'!0'\nvalue:%d\n" ,!0);

printf("code:'x'\nvalue:%d\n" ,x);

printf("\n");

if(0)

{

printf("if(0) is running!\n");

}

if(1)

{

printf("if(1) is running!\n");

}

if(x)

{

printf("if(x) is running!\n");

}

if(x = 0)

{

printf("if(x = 0) is running!\n");

}

if(x = 1)

{

printf("if(x = 1) is running!\n");

}

return 0;

}

输出结果

x value:2

code:'x==2'

value:1

code:'x!=2'

value:0

code:'x=3'

value:3

code:'x-1'

value:2

code:'!x'

value:0

code:'!1'

value:0

code:'!0'

value:1

code:'x'

value:3

if(1) is running!

if(x) is running!

if(x = 1) is running!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值