How to use assert in C

2 Answers

0 votes
#include <stdio.h>
#include <assert.h>

int main() {
	int a = 1;

	// expression == FALSE
	assert(a == 5); // abort program when a is not 5

	puts("main");

	return 0;
}





/*
run:
  
Assertion failed: a == 5, file test.c, line 8
  
*/

 



answered Dec 10, 2022 by avibootz
0 votes
#include <stdio.h>
#include <assert.h>

void print(int* p) {
	assert(p != NULL); // abort() has been called
	printf("%d\n", *p);
}

int main()
{
	int a = 234;
	int* p1 = NULL;
	int* p2 = NULL;

	p1 = &a;

	print(p1);
	print(p2);

	return 0;
}





/*
run:

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

*/

 



answered Feb 23, 2023 by avibootz
edited May 18, 2023 by avibootz

Related questions

1 answer 152 views
152 views asked Jun 13, 2022 by avibootz
2 answers 294 views
1 answer 154 views
154 views asked Dec 10, 2022 by avibootz
1 answer 213 views
1 answer 211 views
211 views asked Oct 21, 2022 by avibootz
...