C language assert() usage

I. Overview

  • assert is a macro used to check a certain condition at runtime (while the program is executing), very useful when debugging a program. To use it, you need to include the header file "assert.h" in your program.
  • The assert keyword is used to execute expressions as function arguments and evaluate them during memory allocation. Therefore, we can use the malloc() method to write variables and calculate.
    • If the expression fails to evaluate or returns false, the expression along with the filename and line executed will be returned as an error.
    • If the calculation fails or returns false, the application will abort(), assert() will display an error message on stderr and abort program execution.
    • If the expression succeeds and returns true, the assert function is automatically deactivated.

manual

Created with Raphaël 2.3.0 开始 assert(exp) exp真假 继续执行✔ 错误❌程序 终止执行 yes no

2. Declaration

//表达式可以是任何有效的 C 语言表达式,很多时候它是一个条件。
void assert(int expression or variable);

3. Ignore assert() in c language

  • Assertions are enabled by default by using code generated by the GCC, C, and C++ compilers.
  • Assert statements can have unintended consequences when compiling a program, and it would be a very time-consuming task to remove all assertions from the code if we don't want to use the assert() function.
  • There is an easier way to remove all assert statements from the code during execution of the C programming language. Use the preprocessor NDEBUG. The GCC compiler decides whether to disable the assert() function by judging whether DNDEBUG is defined.
  • The code to implement the assertion when using NDEBUG and syntax is as follows:
#define assert(_Expression) ((void)0)

insert image description here

The above code is the basic syntax to define assert() and pass a parameter named _Expression to disable the assert operation in the program.

1. Example

definitionNDEBUGmacro time

#define NDEBUG
# include <assert.h>
int main()
{
    
    
	int var= 8;
	assert (var==1);
	system("pause");
	return 0;
}

output:

请按任意键继续. . .

undefineNDEBUGmacro time

//#define NDEBUG
# include <assert.h>
int main()
{
    
    
	int var = 8;
	assert(var == 1);
	system("pause");
	return 0;
}

output:

Assertion failed: var == 1, file g:\file\vscode\c语言\c语言\c.c, line 6

Error:
insert image description here

4. Key points

  • The purpose of assertions is to use and test the programmer's assumptions and other operations.
  • As an illustration, we can use an assert to determine whether the pointer returned by malloc() is NULL.
  • According to an assertion, a program satisfies certain requirements at a specific time period during its execution.
  • When executed, assert() prints an error message on stderr and aborts program execution if the expression is false.
  • Programmers can remove assertions without modifying the source code by simply recompiling the program.

5. Case

1. Example 1

For example: In the program, we divide two integers, ie calculate a/b (where a and b are integers) b cannot be zero, use assert(b != 0) in the program. If the condition (b != 0) holds, program execution will continue. Otherwise, it will terminate and an error message will be displayed on the screen, specifying the file name, line number, function name, and condition that does not hold.

#include <stdio.h>
#include <assert.h>
int main() 
{
    
    
  int a, b;
 
  printf("Input two integers to divide\n");
  scanf("%d%d", &a, &b);
  assert(b != 0);
  printf("%d/%d = %.2f\n", a, b, a/(float)b);
  system("pause");
  return 0;
}

1. Enter:

1 2

1. Output:

Input two integers to divide
1 2
1/2 = 0.50
请按任意键继续. . .

2. Enter:

2 0

2. Output:

Input two integers to divide
2 0
Assertion failed: b != 0, file g:\file\vscode\c语言\c语言\c.c, line 9

2. Error reporting:
insert image description here

2. Example 2

For example: in the program, we input an integer i, a string arr, we use assert(i>= 5) in the program.
If the condition i>= 5 holds true, program execution will continue.
If the condition i >= 5 does not hold, it will terminate and an error message will be displayed on the screen, specifying the filename, line number, function name, the false condition.
If the condition arr != NULL holds, program execution will continue.
If the condition arr != NULL is not true, it will terminate and an error message will be displayed on the screen, specifying the filename, line number, function name, and the false condition.

#include <assert.h>
#include <stdio.h>
int main()
{
    
    
   int i;
   char arr[100];
   printf("Please enter an integer value: ");
   scanf("%d", &i);
   assert(i>= 5);//断言i>=5这个条件
   printf("The input integer is: %d\n", i);
   printf("Please enter a string: ");
   scanf("%s", arr);
   assert(arr!= NULL);
   printf("The input string is: %s\n", arr);
   system("pause");
   return(0);
}

1. Enter:

 1

1. Output:

Please enter an integer value: 1
Assertion failed: i >= 5, file g:\file\vscode\c语言\c语言\c.c, line 10

1. Error reporting:
insert image description here

2. Enter:


6
qaz

2. Output:

Please enter an integer value: 6
The input integer is: 6
Please enter a string: qaz
The input string is: qaz
请按任意键继续. . .

Guess you like

Origin blog.csdn.net/qq_42815643/article/details/129164498
Recommended