I have checked that cast operators work fine also with #defined constants, e.g.
#define SAMPLES 100
#define SAMP_TIME_MAX 51
int main()
{
float f_samp = (double) SAMPLES / SAMP_TIME_MAX;
printf("f_samp = %f",f_samp);
return 0;
}
My questions are:
- is it a good practice or is there any better way to obtain the same effect?
- how are the precedence rules, e.g. in the example above does (double) has effect on SAMPLES or on (SAMPLES / SAMP_TIME_MAX)?
When you define something, all that really happens at a high level is that the preprocessor goes around and replaces the instance of the definition with the value. So your example of:
Is really just equivalent to:
So yes, its fine to cast it to a double. Also, you would want to cast to a float, or change f_samp to a double.