网络知识 娱乐 【C语言深度解剖】关键字if&&else&&bool类型

【C语言深度解剖】关键字if&&else&&bool类型

大家好我是沐曦希💕

文章目录

  • 🎇if else 组合
    • 🔭什么是语句
    • 🎆什么是表达式
    • 🎡基本语法
    • 🎉if语句实现猜凶手
  • 🚗bool 变量与"零值"进行比较
    • 💫深入理解C 中 bool
    • 🎆bool 值与0比较
  • 💥写在最后


在这里插入图片描述

🎇if else 组合

🔭什么是语句

C语言中由一个分号;隔开的就是一条语句。

例如:

printf("hahan");
int a = 10;
int b = 20;
int c = a + b; 

🎆什么是表达式

C语言中,用各种操作符把变量连起来,形成有意义的式子,就叫做表达式。
操作符:+,-,*,/,%,>,<,=,==…

int a = 10;
int b = 20;
if(a!=b);//a!=b就是一个表达式

🎡基本语法

语法结构:
//1
if(表达式)
  语句;
//2
if(表达式)
  语句1;
else
  语句2;
//3.多分支
if(表达式1)
  语句1;
else if(表达式2)
  语句2;
else
  语句3;
//4.嵌套
if(表达式1)
{
  语句1;
  if(表示式x)
  {
    语句x;
  }
  else
  {
    语句y;
  }
}
else if(表达式2)
{
  语句2;
}
else
{
  语句3;
}

C语言中0为假,非0为真。
if语句执行,先执行完成表达式的值,得到逻辑结果,在进行判定

#include
int main()
{
	int a = 1;
	int b = 0;
	int c = -1;
	if (a)
	{
		printf("hahan");
	}
	if (b)
	{
		printf("hehen");
	}
	if (c)
	{
		printf("hello worldn");
	}
	return 0;
}

在这里插入图片描述
if语句中:
1.先执行()中的表达式,得到真假结果(true,false,逻辑结果)
2.条件判定功能
3.进行分支功能

🎉if语句实现猜凶手

日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个嫌疑犯的一个。
以下为4个嫌疑犯的供词:
A说:不是我。
B说:是C。
C说:是D。
D说:C在胡说
已知3个人说了真话,1个人说的是假话。
现在请根据这些信息,写一个程序来确定到底谁是凶手

#include
int main()
{
	int A = 0;
	int B = 0;
	int C = 0;
	int D = 0;
	for (A = 0; A <= 1; A++)
	{
		for (B = 0; B <= 1; B++)
		{
			for (C = 0; C <= 1; C++)
			{
				for (D = 0; D <= 1; D++)
				{
					if ((A+B+C+D==1)&&((A == 0) + (C == 1) + (D == 1) + (D == 0) == 3))
					{
						if (A == 1)
							printf("凶手是An");
						else if (B == 1)
							printf("凶手是Bn");
						else if (C == 1)
							printf("凶手是Cn");
						else if (D == 1)
							printf("凶手是Dn");
					}
				}
			}
		}
	}
	return 0;
}

在这里插入图片描述
代码二

#include
int main()
{
   int killer = 0;
   //分别假设凶手是a,b,c,d,看谁是凶手时满足3个人说了真话,一个人说了假话
   for (killer = 'a'; killer <= 'd'; killer++)
   {
    if ((killer != 'a') + (killer == 'c') + (killer == 'd') + (killer != 'd') == 3)
     printf("凶手是:%c", killer);
   }
   return 0;
}

🚗bool 变量与"零值"进行比较

bool x = true/flase;

💫深入理解C 中 bool

C语言有没有bool类型?
c99之前,主要是c90是没有的,目前大部分书,都是认为没有的。因为书,一般都要落后于行业。
但是c99引入了_Bool类型(_Bool就是一个类型,不过在新增头文件stdbool.h中,被重新用宏写成了bool,为了保证C/C++兼容性)。

//测试代码1
#include
#include
int main()
{

	bool ret = false;
	ret = true;
	printf("%dn", sizeof(ret)); //vs2013,vs2019和 Linux中都是1
	return 0;
}

在这里插入图片描述
查看源码:

// stdbool.h
//
//      Copyright (c) Microsoft Corporation. All rights reserved.
//
// The C Standard Library  header.
//
#ifndef _STDBOOL
#define _STDBOOL

#define __bool_true_false_are_defined 1

#ifndef __cplusplus

#define bool  _Bool//c99中是一个关键字哦,后续可以使用bool
#define false 0    //假
#define true  1    //真

#endif /* __cplusplus */

#endif /* _STDBOOL */

/*
* Copyright (c) 1992-2010 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
V5.30:0009 */

理论上,表示真假,需要一个bit就够了,还是要取决于编译器的理解。
代码怎么写:因为目前编译器对C99特性支持的并不全面,我们后面依旧默认使用C90的认识去编码即可,使用int表示真假。
给别人介绍:c99之前,主要是c90是没有的,目前大部分书,都是认为没有的。

另一种bool类型

//测试代码2
#include
#include
int main()
{
//在vs2013中,光标选中BOOL,单击右键,可以看到转到定义,就能看到BOOL是什么
	BOOL x = TRUE;//typedef int BOOl
	x = FALSE;
	printf("%dn", sizeof(x));//输出结果是4,因为在源代码中,是这么定义的:typedef int BOOL;
	return 0;
}
//由微软自行定的标准,只能在微软的软件下使用,不推荐,因为可移植性较差。

在这里插入图片描述

/****************************************************************************
*                                                                           *
* minwindef.h -- Basic Windows Type Definitions for minwin partition        *
*                                                                           *
* Copyright (c) Microsoft Corporation. All rights reserved.                 *
*                                                                           *
****************************************************************************/
typedef int                 BOOL;

这都是Microsoft自己搞的一套BOOL值。
强烈不推荐,因为好的习惯是:一定要保证代码的跨平台性,微软定义的专属类型,其他平台不支持。

在Linux中(centos 7),测试代码1,是可以编过的(因为是标准啊),但是测试代码2就过不了。

[whb@VM-0-3-centos code]$ cat test.c //具有跨平台性
#include 
#include 
int main()
{
   bool ret = false;
   return 0;
}
[whb@VM-0-3-centos code]$ gcc test.c //直接通过
[whb@VM-0-3-centos code]$ vim test.c
[whb@VM-0-3-centos code]$ cat test.c
#include 
#include 
int main()
{
   BOOL ret = FALSE;
   return 0;
}
[whb@VM-0-3-centos code]$ gcc test.c //直接报错
test.c: In function ‘main’:
test.c:5:5: error: unknown type name ‘BOOL’
    BOOL ret = FALSE;
    ^
test.c:5:16: error: ‘FALSE’ undeclared (first use in this function)
    BOOL ret = FALSE;
               ^
test.c:5:16: note: each undeclared identifier is reported only once for each function it appears in

总结:

  1. 优先使用c90,就是我们之前以及后面一直用的方式
  2. 使用bool时,推荐c99标准。

🎆bool 值与0比较

#include 
#include 
int main()
{
	int pass = 0; //0表示假,C90,我们习惯用int表示bool
	//bool pass = false; //C99
	if (pass == 0) { //理论上可行,但此时的pass是应该被当做bool看待的,==用来进行整数比较,不推荐
	//TODO
	}
	if (pass == false) { //不推荐,尽管在C99中也可行
	//TODO
	}
	if (pass) { //推荐
	//TODO
	}
	//理论上可行,但此时的pass是应该被当做bool看待的,==用来进行整数比较,不推荐
	//另外,非0为真,但是非0有多个,这里也不一定是完全正确的
	if (pass != 1) {
		//TODO
	}
	if (pass != true) { //不推荐,尽管在C99中也可行
	//TODO
	}
	if (!pass) { //推荐
	//TODO
	}
	return 0;
}
//bool类型,直接判定,不用操作符进行和特定值比较。

💥写在最后

你的❤️点赞是我创作的动力的源泉
你的✨收藏是我奋斗的方向
你的🙌关注是对我最大的支持
你的✏️评论是我前进的明灯
创作不易,希望大佬你支持一下小沐吧😘