assert in C programming

  • برمجة سي c

assert in C programming language

Assert is a macro that is used to check specific conditions at runtime (when a program is under execution) and is very useful while debugging a program.

To use it, you must include the header file "assert.h" in the program.

Declaration: void assert(int expression);

The expression can be any valid C language expression many a time it is a condition.

In the program, we divide two integers, i.e., calculate a/b (where a and b are integers) b can't be zero, so we use assert(b != 0) in the program. If the condition (b != 0) holds, then the program execution will continue. Otherwise, it terminates, and an error message is displayed on the screen specifying the filename, the line number, the function name, the condition that does not hold (see image below).

الأجوبة

#include 

#include 

 

int main() {

  int a, b;

 

  printf("Input two integers to divide\n");

  scanf("%d%d", &a, &b);

 

  assert(b != 0);

 

  printf("%d/%d = %.2f\n", a, b, a/(float)b);

 

  return 0;

}

output

Input two integers to divide

21 0

a.out: main.c:19: main: Assertion `b != 0' failed.

هل كان المحتوى مفيد؟

القوائم الدراسية التي ينتمي لها السؤال

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...