0

Is there any way to force compiler to get warning on such code:

double func(double a,double b)
{
    double c=a/b;
    return c;
}

but not on this one:

double func(double a,double b)
{
    if(b==0)
        return 0;
    double c=a/b;
    return c;
}

Something like:

warning: b might be zero in division line ...

I want the compiler forces me to check division by zero in my code.

ar2015
  • 5,558
  • 8
  • 53
  • 110
  • Hmm, might depend on the compiler but I thought you would already receive this type of warning on the first example. Should be `return a/b;` as well, or just `return c;`. – DigitalNinja Mar 24 '15 at 00:47
  • check out this [post](http://stackoverflow.com/questions/12617408/a-few-things-about-division-by-zero-in-c). It may not give you that warning because of floating point arithmetic. If you change the type to `int` you might get the warning. – DigitalNinja Mar 24 '15 at 00:51
  • 2
    That is a perfectly valid piece of code, with well defined result, why would you want it to warn? What about a large `a` divided by a very small `b`? – David Rodríguez - dribeas Mar 24 '15 at 00:51
  • @ar2015 I doubt it, since it is technically not an error with floating point types (though you may not appreciate nor like the result). `gcc` won't warn you about it even with `-Wdiv_by_zero` for precisely this reason. It is valid code even with `b` = `0.0`. – WhozCraig Mar 24 '15 at 00:55

0 Answers0