1

I would like to compile a simple C90 code utilizing math library:

main.c:

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

int main()
{
  printf("M_PI: %f\n", M_PI);
}

I use GCC compiler and use option -ansi -pedantic to enforce C90 standard.

gcc -ansi -pedantic -lm main.c

But it doesn't compile. The following is the error message:

main.c: In function ‘main’:
main.c:7:25: error: ‘M_PI’ undeclared (first use in this function)
main.c:7:25: note: each undeclared identifier is reported only once for each function it appears in

My question is, why? Does C90 standard prohibits the use of math library?

Andree
  • 3,033
  • 6
  • 36
  • 56

2 Answers2

5

M_PI isn't defined when strict iso standard is required. Look on this page under trigonometric functions. It is suggested that when using -ansi, just define it yourself:

#define M_PI 3.14159265358979323846264338327
Darcy Rayner
  • 3,385
  • 1
  • 23
  • 15
2

M_PI is generally declared as a macro and there is an explicit guard #if !defined(_ANSI_SOURCE) (at least in OSX), which suggests that the ANSI implementation doesnt support it

for gcc you can also use -std=c90 to force C90

Foo Bah
  • 25,660
  • 5
  • 55
  • 79