Simple and Static Assertion (assert) in C Programming Language

An assertion is a statement used to assert that a fact must be true when that line of code is reached. Assertions are useful for ensuring that expected conditions are met.

1. Simple Assertion

Simple assertion can be implemented using assert(expression) method of assert.h header file.
Syntax

assert(expression)

Parameters

  • expression - expression of scalar type

When the condition passed to an assertion is true, there is no action. The behaviour on false conditions depends on compiler flags. When assertions are enabled, a false input causes an immediate program halt. When they are disabled, no action is taken. It is common practice to enable assertions in internal and debug builds, and disable them in release builds, though assertions are often enabled in release (Whether termination is better or worse than errors depends on the program.)

Assertions should be used only to catch internal programming errors, which usually means being passed bad parameters.

Example

We are going to use gcc compiler on Windows 10 using Bash on Ubuntu on Windows. You can use any other compiler but If you've Windows 10 pc you can install gcc compiler on Windows 10. Read this post to learn how to install gcc compiler on Windows 10 using Bash on Ubuntu on Windows

#include <stdio.h>
/* Uncomment to disable `assert()` */
/* #define NDEBUG */
#include <assert.h>

int main(void)
{
    int x;
    printf("Enter the value of x");
    scanf("%d",&x);

    assert(x >= 0);

    printf("x = %d\n", x);
    return 0;
}

Possible output for x = -1 with NDEBUG undefined:

a.out: main.c:12: main: Assertion `x >= 0' failed.

Possible output for x = -1 with NDEBUG defined:

x = -1

Here the more test cases

2. Static Assertions (C11)

Static assertions are used to check if a condition is true when the code is compiled. If it isn't, the compiler is required to issue an error message and stop the compiling process.

A static assertion is one that is checked at compile time, not run time. The condition must be a constant expression, and if false will result in a compiler error. The first argument, the condition that is checked, must be a constant expression, and the second a string literal.

Unlike assert, _Static_assert is a keyword. A convenience macro static_assert is also defined in assert.h header file. Static assertion only available in C11 version of C..

Syntax

static_assert(expression, message)
"or"
_Static_assert(expression, message)

Parameters

  • expression - expression of scalar type.
  • message - string literal to be included in the diagnostic message.

Example

#include <assert.h>

enum 
{
    N = 5
};

_Static_assert(N == 5, "N does not equal 5");
static_assert(N > 10, "N is not greater than 10");  /* compiler error */

Possible output:

test.c:9:17: error: expected ‘)’ before ‘>’ token
 static_assert(N > 10, "N is not greater than 10");  /* compiler error */

3. Trick to display error message in Simple Assertion

A trick exists that can display an error message along with an assertion. Normally, you would write code like this

void f(void *p)
{
    assert(p != NULL);
    /* more code */
}

If the assertion failed, an error message would resemble

Assertion failed: p != NULL, file main.c, line 5

However, you can use logical AND (&&) to give an error message as well

void f(void *p)
{
    assert(p != NULL && "function f: p cannot be NULL");
    /* more code */
}

Now, if the assertion fails, an error message will read something like this

Assertion failed: p != NULL && "function f: p cannot be NULL", file main.c, line 5

The reason as to why this works is that a string literal always evaluates to non-zero (true). Adding && 1 to a Boolean expression has no effect. Thus, adding && "error message" has no effect either, except that the compiler will display the entire expression that failed.

Next - Graphics (graphics.h)


Discussion

Read Community Guidelines
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.