How to use assert() to check if its argument equal to zero and output specific diagnostic in C

3 Answers

0 votes
#include <stdio.h>
#include <assert.h>
#include <math.h>
 
int main(void)
{
    double n = -9.0;
    assert(n >= 0.0);
    printf("sqrt(n) = %f\n", sqrt(n));   
  
    return 0;
}

  
/*
run:
 
Assertion failed!

Program: D:\c_programming_test\c_test\test\Debug\test.exe
File: D:/c_programming_test/c_test/test/main.c, Line 8

Expression: n >= 0.0

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

*/

 



answered Aug 24, 2016 by avibootz
0 votes
#include <stdio.h>

#define NDEBUG
#include <assert.h>

#include <math.h>


 
int main(void)
{
    double n = -9.0;
    assert(n >= 0.0);
    printf("sqrt(n) = %f\n", sqrt(n));   
  
    return 0;
}

  
/*
run:
 
sqrt(n) = -1.#IND00

*/

 



answered Aug 24, 2016 by avibootz
0 votes
#include <stdio.h>
#include <assert.h>
#include <math.h>
 
int main(void)
{
    double n = 81.0;
    assert(n >= 0.0);
    printf("sqrt(n) = %f\n", sqrt(n));   
  
    return 0;
}

  
/*
run:
 
sqrt(n) = 9.000000

*/

 



answered Aug 24, 2016 by avibootz
...