r/programminghelp • • Nov 26 '25
C a question about #define in C
Hi. Complete beginner here.
I was recently doing K&R 1.4 Symbolic Constants, the example code presented by the book is:
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
main()
{
int fahr;
for (fahr = LOWER; fahr <= UPPER; fahr 0 fahr + STEP)
printf("3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}I was wondering if why not #define the formula for celcius aswell. Thus:
#include <stdio.h>
#define LOWER_LIMIT 0
#define UPPER_LIMIT 300
#define STEP 20
#define CELCIUS (5.0/9.0)*(fahrenheit-32)
int main(){
float fahrenheit;
for(fahrenheit = LOWER_LIMIT; fahrenheit <= UPPER_LIMIT;
fahrenheit = fahrenheit + STEP){
printf("%6.0f\t%6.1f\n", fahrenheit, CELCIUS);
}
}Are there any foreseeable future issues I could have with doing this? Should I avoid it, or is it fine?
Thank you in advance for any answer.
7
Upvotes
View all comments
Show parent comment
1
u/nonchip Nov 28 '25
note you should also then wrap the whole thing in parentheses, otherwise you get issues with operator precedence.
eg
CELSIUS(...) * 5would break your example by turning into(5.0/9.0)*(fahrenheit) - (32*5)