Using M_PI with C89 standard

后端 未结 4 1568
执念已碎
执念已碎 2020-11-27 08:25

I\'m using C and trying to get access to the constant M_PI (3.14159...). I have imported the math.h header file, but the M_PI constant was still undefined. Through some sear

4条回答
  •  鱼传尺愫
    2020-11-27 08:44

    I fail to see what the problem is here; there is no incompatability between -std=c89 and _USE_MATH_DEFINES, one defines what language the compiler will compile, the other defines what parts of math.h get enabled.

    Those parts that are enabled are not defined as part of the ISO C standard library, but that is not the same thing as not being standard C language, language and library are separate entities in C. It is no less C89 compliant than it would be if you had defined your own macros in your own header.

    I would however suggest that you define the macro on the command-line rather than in the code:

    -std=c89 -D_USE_MATH_DEFINES
    

    If you ever encounter a math.h implementation that does not define M_PI, then that is easily fixed without code modification by similarly using command line defined macros:

    -std=c89 -DM_PI=3.14159265358979323846
    

提交回复
热议问题