Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit declaration of function abs - gcc-5.1.0

Compiling the following code using gcc-5.1.0 produces a warning:

warning: implicit declaration of function ‘abs’ [-Wimplicit-function-declaration]

Code:

#include <stdio.h>
#include <math.h>

int main (void)
{
  printf ("%d\n", abs (-1));

  return 0;
}

I have compiled the same code with gcc-4.9.2 and it's not producing any warning.

like image 832
Shahzad Avatar asked Feb 10 '23 01:02

Shahzad


1 Answers

The abs() function is declared in <stdlib.h> which you've not included.

GCC 4.9.2 didn't complain because the default compilation mode was C89/C90 (-std=gnu89) and functions did not need to be declared before being used in C89 as long as they returned an int, but the default compilation mode was changed to C11 (-stdd=gnu11) in GCC 5.1.0 (see the release notes) and in C11 functions must be declared (or defined) before they are used.

like image 149
Jonathan Leffler Avatar answered Feb 27 '23 13:02

Jonathan Leffler