This blog is under construction

Saturday, 5 May 2012

assert

Header file:
    assert.h

Synopsis:
     void assert(int expr);

Description:
     It aborts the program if the expr is false.




Sample program for assert in C:



  #include<stdio.h>
  #include<assert.h>
  #include<errno.h>
  int main() {
        FILE *fp;
        fp = fopen("jp.txt", "r");
        if (fp == NULL) {
                assert(fp);
        }

        printf("File opened in read mode\n");
        fclose(fp);
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/cpgms/assert$ ./a.out
  a.out: assert.c:8: main: Assertion `fp' failed.
  Aborted
  jp@jp-VirtualBox:~/cpgms/assert$ ls
  a.out  assert.c



No comments:

Post a Comment