当前位置:网站首页>[deep anatomy of C language] keywords if & else & bool type
[deep anatomy of C language] keywords if & else & bool type
2022-06-22 16:33:00 【Muxixi】
Hello, I'm Mu Xixi
List of articles

if else Combine
What is a sentence
C There is a semicolon in language ; Separated by a statement .
for example :
printf("haha\n");
int a = 10;
int b = 20;
int c = a + b;
What is an expression
C In language , Connect variables with various operators , Form a meaningful formula , It's called an expression .
The operator :+,-,*,/,%,>,<,=,==…
int a = 10;
int b = 20;
if(a!=b);//a!=b It's an expression
Basic grammar
Grammatical structure :
//1
if( expression )
sentence ;
//2
if( expression )
sentence 1;
else
sentence 2;
//3. Multiple branches
if( expression 1)
sentence 1;
else if( expression 2)
sentence 2;
else
sentence 3;
//4. nesting
if( expression 1)
{
sentence 1;
if( The expression x)
{
sentence x;
}
else
{
sentence y;
}
}
else if( expression 2)
{
sentence 2;
}
else
{
sentence 3;
}
C In language 0 For false , Not 0 It's true .
if Statement execution , First execute the value of the completion expression , Get logical results , In making a judgment
#include<stdio.h>
int main()
{
int a = 1;
int b = 0;
int c = -1;
if (a)
{
printf("haha\n");
}
if (b)
{
printf("hehe\n");
}
if (c)
{
printf("hello world\n");
}
return 0;
}

if In the sentence :
1. Execute first () The expression in , Get true and false results (true,false, Logical result )
2. Condition determination function
3. Branch function
if Statement implementation guess killer
There was a murder somewhere in Japan , Through investigation, the police determined that the killer must be 4 One of the suspects .
The following is a 4 A confession from a suspect :
A say : Is not my .
B say : yes C.
C say : yes D.
D say :C It's bullshit
It is known that 3 I told you the truth ,1 I'm telling a lie .
Now, based on this information , Write a program to determine who the killer is
#include<stdio.h>
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(" The murderer is A\n");
else if (B == 1)
printf(" The murderer is B\n");
else if (C == 1)
printf(" The murderer is C\n");
else if (D == 1)
printf(" The murderer is D\n");
}
}
}
}
}
return 0;
}

Code 2
#include<stdio.h>
int main()
{
int killer = 0;
// Assume that the killer is a,b,c,d, Satisfied to see who the murderer is 3 I told you the truth , A man lied
for (killer = 'a'; killer <= 'd'; killer++)
{
if ((killer != 'a') + (killer == 'c') + (killer == 'd') + (killer != 'd') == 3)
printf(" The murderer is :%c", killer);
}
return 0;
}
bool Variables and " Zero value " Compare
bool x = true/flase;
In depth understanding of C in bool
C Language has no bool type ?
c99 Before , Mainly c90 There is no , At present, most books , They all think there is no . Because of the book , Generally, it lags behind the industry .
however c99 Introduced _Bool type (_Bool It's a type , But in the new header file stdbool.h in , It was written again as bool, In order to ensure C/C++ Compatibility ).
// Test code 1
#include<stdio.h>
#include<stdbool.h>
int main()
{
bool ret = false;
ret = true;
printf("%d\n", sizeof(ret)); //vs2013,vs2019 and Linux All of them are 1
return 0;
}

View source code :
// stdbool.h
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The C Standard Library <stdbool.h> header.
//
#ifndef _STDBOOL
#define _STDBOOL
#define __bool_true_false_are_defined 1
#ifndef __cplusplus
#define bool _Bool//c99 Is a keyword in , Can be used later bool
#define false 0 // false
#define true 1 // really
#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 */
Theoretically , It means true or false , Need one bit That's enough , It still depends on the compiler's understanding .
How to write the code : Because at present, the compiler is not suitable for C99 Feature support is not comprehensive , We will still default to C90 Just code your knowledge , Use int It means true or false .
Introduce to others :c99 Before , Mainly c90 There is no , At present, most books , They all think there is no .
Another kind bool type
// Test code 2
#include<stdio.h>
#include<stdbool.h>
int main()
{
// stay vs2013 in , The cursor selects BOOL, Right click , As you can see, go to the definition , You can see BOOL What is it?
BOOL x = TRUE;//typedef int BOOl
x = FALSE;
printf("%d\n", sizeof(x));// The output is 4, Because in the source code , That's the definition :typedef int BOOL;
return 0;
}
// Standards set by Microsoft itself , It can only be used under Microsoft software , Not recommended , Because of poor portability .

/**************************************************************************** * * * minwindef.h -- Basic Windows Type Definitions for minwin partition * * * * Copyright (c) Microsoft Corporation. All rights reserved. * * * ****************************************************************************/
typedef int BOOL;
This is all Microsoft I made it myself BOOL value .
It is strongly not recommended that , Because good habits are : Make sure that the code is cross platform , Exclusive types defined by Microsoft , Other platforms do not support .
stay Linux in (centos 7), Test code 1, It can be made up ( Because it's the standard ), But test code 2 You can't make it .
[[email protected]-0-3-centos code]$ cat test.c // Cross platform
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool ret = false;
return 0;
}
[[email protected]-0-3-centos code]$ gcc test.c // Directly through
[[email protected]-0-3-centos code]$ vim test.c
[[email protected]-0-3-centos code]$ cat test.c
#include <stdio.h>
#include <stdbool.h>
int main()
{
BOOL ret = FALSE;
return 0;
}
[[email protected]-0-3-centos code]$ gcc test.c // Direct error
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
summary :
- priority of use c90, It's the way we've been using before and after
- Use bool when , recommend c99 standard .
bool Value and 0 Compare
#include <stdio.h>
#include <stdbool.h>
int main()
{
int pass = 0; //0 Said the false ,C90, We are used to using int Express bool
//bool pass = false; //C99
if (pass == 0) {
// Theoretically feasible , But at this time pass It should be regarded as bool Looking at ,== Used for integer comparison , Not recommended
//TODO
}
if (pass == false) {
// Not recommended , Although in C99 It also works in
//TODO
}
if (pass) {
// recommend
//TODO
}
// Theoretically feasible , But at this time pass It should be regarded as bool Looking at ,== Used for integer comparison , Not recommended
// in addition , Not 0 It's true , But not 0 There are many. , It's not necessarily true here
if (pass != 1) {
//TODO
}
if (pass != true) {
// Not recommended , Although in C99 It also works in
//TODO
}
if (!pass) {
// recommend
//TODO
}
return 0;
}
//bool type , Direct determination , Compare with specific values without operators .
At the end
Yours ️ Praise is the source of my creative power
Your collection is the direction of my struggle
Your attention is my greatest support
Yours ️ Comments are a bright light for me to move forward
It's not easy to create , I hope you can support Xiaomu
边栏推荐
猜你喜欢
随机推荐
Deploy odoo to the server and configure it as a service
The odoo system sets priorities for the views independently developed by the original model
【小程序项目开发-- 京东商城】uni-app开发之配置tabBar & 窗口样式
NiO programming service
for..of vs. for..in 语句
SAP ABAP report programming-08
【C语言】库函数qsort的使用
User exit and customer exit in SAP ABAP -015
SAP ABAP 表控制与示例-07
SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017
实现一个Container全局组件步骤(给还不会使用组件的新手一个思路,大佬绕道)
ALV report in SAP tutorial - ABAP list viewer -012
【C语言】深度剖析整型和浮点型在内存中的存储
Prometheus监控之Consul监控 [consul-exporter]
Modularity in SAP ABAP: macros, subroutines and function modules -04
Google Chrome small details
SAP ABAP 数据字典教程 SE11:表、锁定对象、视图和结构 -03
NiO uses writable events to handle the situation of one-time write incompleteness
Static assertion static_ assert
论催收系统的任务调度设计








