assert

Full prototype:

void assert(scalar-type);  

Purpose and Notes:

A function-like macro to check at run-time for something that should never occur and if it does- print the file name, line number, and function name where the assert() macro was called in the source and then abort the program.

Essentially this function can be used for creating tests, sanity checks, and for aborting the program on exceptions that the program doesn't want to recover from or handle in another way.

This function can be turned off and will not be included in the compiled binary when the NDEBUG macro is defined. So it is really easy to turn off and on between testing and production binaries with a simple #define or #undef preprocessor macro call.

A neat trick is that a string literal can be appended to any scalar expression with the && operator because string literals themselves will always evaluate to true will it will not effect the rest of the expression, and the string can be used as a special error message for when the assert macro crashes the program. This is shown in example two which is identical to example one, except a string was appended to the end of the expression with the && operator.

Returns:

void- Nothing returned.

Argument 1:

Scalar-expression is defined as an arithmatic or pointer type. Anything not an aggregate-type which are arrays and structs, because C does not allow them to be compared using the == operator to other arrays and structs (not L-values). The argument is usually a conditional expression just like you would use in an "if statement" and if the conditional expression returns a "c-style false (a.k.a. 0)" then the function executes and aborts the program. Otherwise the program continues normally from the point right after the assert() call.


Example 1
  1    #include <stdio.h>
  2    #include <assert.h>
  3    
  4    int main(void)
  5    {
  6            printf("Please enter your age: ");
  7            char buffer[100];
  8            fgets(buffer, 100, stdin);
  9            int age;
 10            sscanf(buffer, "%d", &age);
 11            assert(age > 0 && age < 125);
 12            if (age < 18) {
 13                    printf("You are a child.\n");
 14            else {
 15                    printf("You are an adult.\n");
 16            }
 17            return 0;
 18    }
$ gcc assert.c  
$ ./a.out 
Please enter your age: 12
You are a child.
$ ./a.out 
Please enter your age: 126
Assertion failed: (age > 0 && age < 125), function main, file assert.c, line 11.
Abort trap: 6
$ ./a.out 
Please enter your age: 36
You are an adult.
Example 2
  1    #include <stdio.h>
  2    #include <assert.h>
  3    
  4    int main(void)
  5    {
  6            printf("Please enter your age: ");
  7            char buffer[100];
  8            fgets(buffer, 100, stdin);
  9            int age;
 10            sscanf(buffer, "%d", &age);
 11            assert(age > 0 && age < 125 && "If you are reading this then the program crashed because someone entered an invalid age.");
 12            if (age < 18) {
 13                    printf("You are a child.\n");
 14            else {
 15                    printf("You are an adult.\n");
 16            }
 17            return 0;
 18    }
$ gcc assert.c  
$ ./a.out 
Please enter your age: 168
Assertion failed: (age > 0 && age < 125 && "If you are reading this then the program crashed because someone entered an invalid age."), function main, file assert.c, line 11.
Abort trap: 6