how and when to use the ASSERT statement

Could anyone please describe, or put an example when and how to use an assert statement?

More specifically in C/C++

2 Likes

I think you came across the function in the testers’ solutions in the editorials of the problems of official contests. They use the assert() function to check if the test data violates the constraints as given in the problem statement.

void assert (int expression);

Evaluate assertion
If the argument expression of this macro with functional form compares equal to zero (i.e., the expression is false), a message is written to the standard error device and abort is called, terminating the program execution.

Other than programming contests, it may be found useful in your own projects.

For further readings in discussions, I would recommend these SO questions.

2 Likes

Got some of it… Thankz :slight_smile:

I would also like to ask if the ‘assert’ statement behaves differently on different compilers in any context, even the assertion error messages it displays?

Yes it depends on the compiler that you’re using, which can only be understood by testing your code on different platforms.

//