Do you use assert in your code?

assertIt’s a macro that checks an expression, and if that expression isn’t true (.e. non-zero) it outputs a message and halts the program.

Here’s an example.


int main() {
	int x = 0;
	assert(x != 0);
	printf("It is the end");

Because x is 0, it triggers the assert and the program never reaches the printf statement. It’s a bit of a crude tool. In other programming languages like C++ or Delphi it raises an exception which can be handles but C of course does not have exceptions.

My own preference is to check the value for example making sure a pointer is not null and nthen displaying an error but other programmers prefer to use assert and have it kill the program if things go wrong.

(Visited 2 times, 1 visits today)