当前位置:网站首页>[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


 Insert picture description here

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;
}

 Insert picture description here
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;
}

 Insert picture description here
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;
}

 Insert picture description here
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 .

 Insert picture description here

/**************************************************************************** * * * 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 :

  1. priority of use c90, It's the way we've been using before and after
  2. 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

原网站

版权声明
本文为[Muxixi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221515568084.html