Allocating char array using malloc

Yes, it’s a matter of style, because you’d expect sizeof(char) to always be one. On the other hand, it’s very much an idiom to use sizeof(foo) when doing a malloc, and most importantly it makes the code self documenting. Also better for maintenance, perhaps. If you were switching from char to wchar, you’d switch to … Read more

Printf width specifier to maintain precision of floating-point value

I recommend @Jens Gustedt hexadecimal solution: use %a. OP wants “print with maximum precision (or at least to the most significant decimal)”. A simple example would be to print one seventh as in: But let’s dig deeper … Mathematically, the answer is “0.142857 142857 142857 …”, but we are using finite precision floating point numbers. … Read more

Setting std=c99 flag in GCC

Instead of calling /usr/bin/gcc, use /usr/bin/c99. This is the Single-Unix-approved way of invoking a C99 compiler. On an Ubuntu system, this points to a script which invokes gcc after having added the -std=c99 flag, which is precisely what you want.

What is the behavior of integer division?

Will result always be the floor of the division? What is the defined behavior? Not quite. It rounds toward 0, rather than flooring. 6.5.5 Multiplicative operators 6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded.88) If the quotient a/b is representable, the expression (a/b)*b … Read more