Comparison of C language data types with "0 value"

content

1. The bool variable is compared with the "0 value"

                  1.1 Introduction to bool type

                  1.2 bool and 0 comparison

2. Floating point number is compared with "0 value"

                   2.1 Comparison between floating point numbers

                   2.2 Floating point number and 0 comparison

3. Comparison of pointer variable and "0 value"

                   3.1 Introduction

                   3.2 Pointer and 0 comparison


1. The bool variable is compared with the "0 value"

                  1.1 Introduction to bool type

Question: Does the C language have a bool type?

One: c89 or c90 does not have a _bool type//c99 introduced a _Bool type

Two: (details) Before c99, mainly c90 did not have it. At present, most of the books think that there is no such thing. Because of books, generally have to lag behind the industry. But c99 introduced the _Bool type (you read that right, _Bool is a type, but in the new header file stdbool.h, it was rewritten as bool with macros, in order to ensure C/C++ compatibility).

In C language, we all know that 0 means false, non-0 means true

The bool type uses true and false to represent true and false

Test code 1 : (This code is compiled with VS2013, so there are #include <windows.h> and system("pause");)

#include <stdio.h>
#include <stdbool.h> //没有这个头文件会报错,使用新特性一定要加上
#include <windows.h>
int main()
{
bool ret = false;
ret = true;
printf("%d\n", sizeof(ret)); //vs2013 和 Linux中都是1
system("pause");
return 0;
}

//View the source code:

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

PS: In theory, to indicate true or false, one bit is enough, but this problem still depends on the understanding of the compiler. In vs2013 it is considered to be 1 byte.

but!!

//Test code 2:

#include <stdio.h>
#include <windows.h>
int main()
{
//在vs中,光标选中BOOL,单击右键,可以看到转到定义,就能看到BOOL是什么
BOOL ret = FALSE;
ret = TRUE;
printf("%d\n", sizeof(ret)); //输出结果是4,因为在源代码中,是这么定义的:typedef int BOOL;
system("pause");
return 0;
}

We found that it can even be edited. . . what the hell? ?

This is a set of BOOL values ​​made by Microsoft itself. Go to the header file corresponding to BOOL in vs, turn to the top, and you can see Microsoft's copyright information. OK, who should I listen to? ?

Microsoft? It is strongly not recommended, the uppercase BOOL is Microsoft's standard, the cross-platform performance is too poor, and the portability is poor. The test code 1 can be compiled in both VS2013 and Linux, but the code 2 can only be passed in VS2013, but not Linux.

Therefore, in case of using bool later, C99 standard is strongly recommended, and Microsoft is abandoned.

                  1.2 bool and 0 comparison

Explanation by code:

#include <stdio.h>
#include <stdbool.h>
#include <windows.h>
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
}
system("pause");
return 0;
}

2. Floating point number is compared with "0 value"

                   2.1 Comparison between floating point numbers

Accuracy loss:

Floating-point numbers are stored in memory, not what we think, they are completely stored, and when decimal is converted into binary, there may be a loss of precision.

Note that the losses here are not blindly reduced, but may also increase. When the floating-point number itself is stored, it will "round up" or other strategies when the calculation is endless.

E.g:

#include<stdio.h>
int main()
{
	double d = 3.6;
	printf("%.50f\n", 3.6);
	return 0;
}

 Comparison process: (look at the code first)

#include<stdio.h>
int main()
{
	double x = 1.0;
	double y = 0.1;
	printf("%.50f\n", x - 0.9);
	printf("%.50f\n", y);
	if ((x - 0.9) == 0.1)
	{
		printf("you can see me!\n");
	}
	else
	{
		printf("oops!\n");   // oops!
	}
	return 0;
}

//When comparing floating-point numbers, you must not use == to compare them directly! ! ! !

// Floating point numbers themselves have a loss of precision, which in turn causes various results to be slightly different.

Solution:! !

Hoichi:

Fake code:

if((x-y) > -精度 && (x-y) < 精度)
{
    //TODO
}

Formal code: custom EPSION is used here to define the precision range

#include<stdio.h>
#define EPSION 0.00000000001
int main()
{
	double x = 1.0;
	double y = 0.1;
	if (((x - 0.9) - y) > -EPSION && ((x - 0.9) - y) < EPSION)
	{
		printf("you can see me!\n");
	}
	else
		printf("oops!\n");
	return 0;
}

Method 2: Use the absolute value method to solve this problem

Pseudocode version:

if(fabs(x-y) < 精度)
{
    //fabs是浮点数求绝对值
    //TODO
}

 Official code:

#include<stdio.h>
#include<math.h>
#include<float.h>
int main()
{
	double x = 1.0;
	double y = 0.1;
    if (fabs((x - 0.9) - y) < DBL_EPSILON)
	{
     	printf("you can see me!\n");
    }
	else
	    printf("oops!\n");
	
	return 0;
}

                   2.2 Floating point number and 0 comparison

Now that you know the comparison between floating-point numbers, converting one of the floating-point numbers to 0 is equivalent to comparing a floating-point number with 0

// To compare a and b, you need to compare as follows

// fabs(a - b) < DBL_EPSILON)

// Compare a and 0, you can convert b in the above code to 0, as follows:

// fabs(a) < DBL_EPSILON)

// Explain that as long as the above code requirements are met, it can be shown that a and 0 are equal

#include<stdio.h>
#include<math.h>
#include<float.h>
int main()
{

	double x = 0.00000000000000000000000000001;

	//法一:
	/*if (fabs(x) < DBL_EPSILON)
	{
		printf("you can see me! x==0.0\n");
	}
	else
	{
		printf("oops!\n");
	}*/

	//法二:
	if ((x > -DBL_EPSILON) && (x < DBL_EPSILON))
	{
		printf("you can see me! x==0.0\n");

	}
	else
	{
		printf("oops!\n");
	}
	
	return 0;
}
/* Question: Do you want to use an equals sign? 
// without: if ((x > -DBL_EPSILON) && (x < DBL_EPSILON))
// with: if ((x >= -DBL_EPSILON) && (x <= DBL_EPSILON))
XXX_EPSILON is minimum error, but: XXX_EPSILON+ n is not equal to the smallest positive number of n. 
  Cannot take =
  if
  fabs(x) == DBL_EPSILON
  then double y + x != y;
  and double y + DBL_EPSILON != y;
  but y + 0.0 == y;itself, it can already cause changes in other and other +- data itself, this does not conform to the concept of 0,
  so don't write an equal sign in the future */

3. Comparison of pointer variable and "0 value"

                   3.1 Introduction

Look at the code:

#include<stdio.h>
int main()
{
	//类型是不同的
	printf("%d\n", 0);      //0 
	printf("%d\n", '\0');   //0
	printf("%d\n", NULL);   //0

	int a = 0;
	char* p = NULL;
	//这里NULL的值是0是因为发生了强制类型转换
	//	如何理解强制类型转换?
	//	需要编写算法,或使用相关库函数
	//  "123456"  -> int: 123456  
	//	"123456" 7 字节 int 类型 4 字节 //真实转换

	return 0;
}

// ******The real conversion will change the data in memory******

 // ******Forced type conversion does not change the data in memory, only changes the corresponding type***

                   3.2 Pointer and 0 comparison

#include<stdio.h>
int main()
{
	int* p = NULL;
	//if (p == NULL) 不推荐
	//if (NULL == p) 推荐
	return 0;
}

Guess you like

Origin blog.csdn.net/bit_zyx/article/details/122007540