Using M_PI with C89 standard

A conforming standard library file math.h is not only not required to, but actually must not define M_PI by default. In this context ‘by default’ means that M_PI must only get defined through compiler-specific tricks, most often undefined behavior through the use of reserved identifiers. Just define the constant yourself (you can use the name … Read more

How to sum large numbers?

In practice, you want some arbitrary precision arithmetic (a.k.a. bigint or bignum) library. My recommendation is GMPlib but there are other ones. Don’t try to code your own bignum library. Efficient & clever algorithms exist, but they are unintuitive and difficult to grasp (you can find entire books devoted to that question). In addition, existing … Read more

How to find my current compiler’s standard, like if it is C90, etc

This is compiler dependent, I’m supposing you’re using GCC. You could check your compiler defined macros using: gcc -dM -E – < /dev/null Check the manual about the flags, specially: ###__STDC_VERSION__### This macro expands to the C Standard’s version number, a long integer constant of the form yyyymmL where yyyy and mm are the year … Read more

What are the major differences between ANSI C and K&R C?

There may be some confusion here about what “K&R C” is. The term refers to the language as documented in the first edition of “The C Programming Language.” Roughly speaking: the input language of the Bell Labs C compiler circa 1978. Kernighan and Ritchie were involved in the ANSI standardization process. The “ANSI C” dialect … Read more

How to generate NaN, -Infinity and +Infinity in ANSI C?

There is in C99, but not in previous standards AFAIK. In C99, you’ll have NAN and INFINITY macros. From “Mathematics <math.h>“ (§7.12) section The macro INFINITY expands to a constant expression of type float representing positive or unsigned infinity, if available; … If you’re stuck with ANSI C89, you’re out of luck. See C-FAQ 14.9.