C Standard Libraries C++

assert()

Declaration

void assert(int iExpression);

Description

This function is used to test whether an expression is true (non-zero) or false (zero). An argument value of false (zero), causes the program to stop execution and output a diagnostic message when compiling in debug mode. In addition, when compiling in Visual studio, a dialog will pop up offering three options as buttons: Abort, Retry, and Ignore. In release mode, when NDEBUG is defined, the assert() function does nothing.

Example

#include <iostream>
#include <cassert>

int main() {
    using namespace std;

    char caName[] = "XoaX.net";
    // Assert a condition, which is false in this case.
    assert(caName[4] == 'T');

    cout << "This isn't executed in debug mode." << endl;

    return 0;
}

Output

assert() Output

Dialog

assert() Dialog
 

© 2007–2024 XoaX.net LLC. All rights reserved.