18

I used the abs() function and I added #include <math.h> at the top of code. But I keep getting this error:

hello.c:20:11: warning: implicit declaration of function 'abs' is invalid in C99
[-Wimplicit-function-declaration]
      int a = abs(arrOfHour[i] - hour) * 60 + minute;
              ^

I'm using LLVM compiler.

Why does this error occurs even though I have included math.h?

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
wakwakwak99
  • 321
  • 1
  • 2
  • 9
  • Can you post the code, especially the part you call `abs`? – holgac Apr 11 '15 at 12:31
  • @holgac , That isn't needed. The part where the OP calls `abs` is in the warning message. – Spikatrix Apr 11 '15 at 12:33
  • @CoolGuy yeah I forgot undeclared functions are implicitly declared, sorry. Anyway, for future, you can check where a function is declared using *manpages*, http://linux.die.net/man/3/abs – holgac Apr 11 '15 at 12:36
  • BTW: Suspect code's `int a = abs(arrOfHour[i] - hour) * 60 + minute;` and its previous calculation of `minute` will not work as desired. Maybe want something like `a = abs(arrOfHour[i]*60 + arrOfMin[i] - (hour*60 + min))` – chux - Reinstate Monica Apr 11 '15 at 15:55

2 Answers2

37

I'm going to quote straight from the docs : "Prototypes for abs, labs and llabs are in stdlib.h"

As a rule of thumb the mathematical functions that operate on floating point numbers are in math.h, and the ones that operate on integers are in stdlib.h.

There's a pretty good Wikipedia article on C mathematical functions if you need more information.

tux3
  • 7,171
  • 6
  • 39
  • 51
1

The function headers are missing, but since the abs function requires the library math.h and also stdlib.h, the latter must be missing. It also includes the call to the library stdlib.h

#include <stdlib.h>
Alejandro Caro
  • 998
  • 1
  • 10
  • 22