(This answers tries to address good practices, but doesn't consider the question of "standard c". This answer is also specific to POSIX)
Standard constants
Use M_PI
from math.h
, or define PI
as M_PI
.
Comments
You could explain what the value -.83
represents. Is it an angle? If so, is it in radians or degresesdegrees, and what does it measure.? Maybe include a Wikipedia link.
Listen to compiler warnings
These are from clang 15. See for yourself on godboltgodbolt. These are the ones I'd consider important:
<source>:39:9: warning: variable 'length' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
if (fp) {
Very important.
<source>:70:15: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
len_rep = strlen(trg);
Follow the return type of strlen
and use size_t
for pointer-sized things.
<source>:155:29: warning: cast from 'const char *' to 'char *' drops const qualifier [-Wcast-qual]
char *template = (char*)insert;
Declare insert
as char *
, not const char *
, so the signature doesn't tell lies.
<source>:212:16: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wimplicit-float-conversion]
float N1 = floor(275*month/9);
<source>:218:24: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wimplicit-float-conversion]
float lngHour = lng/15.0;
Be consistent and use either float
or double
consistently. Use floorf
and 15.0f
for floats.
<source>:296:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]
}
This is about days_in_month
. Think about what happens if the argument is 42.