catalogue
- 1, Introduction to assert function
- 2, The assert function uses
- 3, Usage Summary and precautions of assert function
- 4, Guess you like it
Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language
1, Introduction to assert function
Assertions are familiar to everyone, and most programming languages also have the feature of assertions. Simply put, an assertion is a check on a hypothetical condition. assert macro prototype definition:
//Need to include header file < assert. H > #include <assert.h> /* Description: terminates program execution if its condition returns an error Parameters: expression : Conditional judgment or expression */ void assert( int expression );
assert The value of the expression will be checked to determine whether the executor needs to be terminated. That is, if the value of expression is false (i.e. 0), it will first print an error message to the standard error stream stderr, and then call abort Function to terminate program operation; Otherwise, assert has no effect.
2, The assert function uses
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - C language assert function //@Time:2021/07/18 07:30 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include <stdio.h> #include <assert.h> #include <stdlib.h> int main( void ) { FILE *fp; fp = fopen( "456.txt", "w" );//Open a file in a writable manner. If it does not exist, create a file with the same name assert( fp ); //So there will be no mistakes here fclose( fp ); fp = fopen( "123.txt", "r" );//Open a file as read-only. If it does not exist, opening the file fails assert( fp ); //So there's a mistake here fclose( fp ); //The program will never run here return 0; }
By default, assert Macros work only in the Debug version (internal Debug version) and will be ignored in the Release version (Release version).
3, Usage Summary and precautions of assert function
1. Use assert to verify the validity of the passed parameters at the beginning of the function
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - C language assert function //@Time:2021/07/18 07:30 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ int resetBufferSize(int nNewSize) { //Function: change buffer size, //Parameter: nNewSize buffer length //Return value: current length of buffer //Note: keep the original information content unchanged. Nnewsize < = 0 means to clear the buffer assert(nNewSize >= 0); assert(nNewSize <= MAX_BUFFER_SIZE); ... }
2. Each assert only checks one condition, because when multiple conditions are checked at the same time, if the assertion fails, it is impossible to intuitively judge which condition fails
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - C language assert function //@Time:2021/07/18 07:30 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ //Wrong writing assert(a>=0 && b<=0 && c>0); //Correct writing assert(a>=0); assert(b<=0); assert(c>0);
3. assert The disadvantage is that frequent calls will greatly affect the performance of the program and increase additional overhead.
4, Guess you like it
- Difference between C language array subscript out of bounds and memory overflow
- C language uses pointers to traverse arrays
- Difference between C language pointer and array
- Difference between C language pointer array and array pointer
- C language field pointer
- C language function value transfer and address transfer
- C language function indefinite length parameter
- C language function pointer
- C language pointer function
- C language callback function callback
- C language #pragma once
- Difference between C language #include < > and #include ""
- C language const modifier function parameters
- Difference between const and define in C language
- C language # operators
- C language ## operators
- C language__ VA_ARGS__
- C language##__ VA_ARGS__
- C language function indefinite length parameter##__ VA_ARGS__ Classic case
- C language va_start macro
- C language va_end macro
- C language va_arg macro
- C language vprintf function
- C language va_start / va_end / va_arg custom printf function
- C language main function
- C language main function parameter main(int argc, char *argv [])
- C language exit function
- C language abort function
- C language assert function
No reprint without permission: Ape programming » C language assert function
This article is written by blog - Ape programming Ape programming release!