(一)
先动手编写一个程序:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
#include <stdio.h>
int main()
{
if(1)
{
printf("The condition is true!\\n");
}
return 0;
}
|
运行结果:
The condition is true!
再把1依次改为,2,5,100,-10,发现运行结果完全一样。
再改成if(0),此时发现没有运行结果,说明printf()语句没被执行。
C语言把判断语句中的任何非0或非空的值当作真。所以if(1), if(2), if(5), if(100), if(-10)的效果是一样的。
(二)
再编写一个程序:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#include <stdio.h>
int main()
{
int a = 100;
if(a > 0)
{
printf("The condition value is %d\\n", (a > 0));
}
return 0;
}
|
运行结果:
The condition value is 1
分析:
a = 100,a > 0成立 ,所以if( a > 0)等价于if(1)。
在C语言中,判断语句是有值的,要么为1,要么为0。比如本程序中a > 0的值就是1。
(三)
最后编写一个程序:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#include <stdio.h>
int main()
{
char c1 = '\\0';
if(c1)
{
printf("The condition is true!\\n");
}
else
{
printf("The condition is false!\\n");
}
char c2 = ' ';
if(c2)
{
printf("The condition is true!\\n");
}
else
{
printf("The condition is false!\\n");
}
char c3 = 'A';
if(c3)
{
printf("The condition is true!\\n");
}
else
{
printf("The condition is false!\\n");
}
return 0;
}
|
运行结果:
?
|
1
2
3
|
The condition is false!
The condition is true!
The condition is true!
|
说明:C语言中用'\\0'来表示空字符。空格' ‘也是一个字符,这从if(c2)条件为真就可以看出来。
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 49
-
2025-05-25 50
-
2025-05-25 35
-
2025-06-04 82
-
2025-05-25 49
热门评论

