While I was learning the concept of conditional compilation, i encountered some problems. I was trying to define a Symbolic constant and change its value if the value wasn't something i expected. And facing some errors
SO this is the code
// Demonstrating conditional compilation
#include <stdio.h>
#define PI 3.14
int main()
{
#if (PI==3.14)
printf("Correct value!");
#else
#undef PI
#define PI 3.14
printf("Correct value assigned!");
#endif
}
and this is the error(s)
Starting build...
/usr/bin/gcc -fdiagnostics-color=always -g "/home/karthik/karthik/Learning-C-Lang/Learning_C/10. Preprocessor/Preprocessor DIrectives/programs/programs/conditional_compilation_demo.c" -o "/home/karthik/karthik/Learning-C-Lang/Learning_C/Misc/Binaries (compiled on Linux)/Binaries/conditional_compilation_demo"
/home/karthik/karthik/Learning-C-Lang/Learning_C/10. Preprocessor/Preprocessor DIrectives/programs/programs/conditional_compilation_demo.c: In function ‘main’:
/home/karthik/karthik/Learning-C-Lang/Learning_C/10. Preprocessor/Preprocessor DIrectives/programs/programs/conditional_compilation_demo.c:4:12: error: floating constant in preprocessor expression
4 | #define PI 3.14
| ^~~~
/home/karthik/karthik/Learning-C-Lang/Learning_C/10. Preprocessor/Preprocessor DIrectives/programs/programs/conditional_compilation_demo.c:8:10: note: in expansion of macro ‘PI’
8 | #if (PI==3.14)
| ^~
/home/karthik/karthik/Learning-C-Lang/Learning_C/10. Preprocessor/Preprocessor DIrectives/programs/programs/conditional_compilation_demo.c:8:14: error: floating constant in preprocessor expression
8 | #if (PI==3.14)
| ^~~~
Build finished with error(s).
But this code works
// Write a program to calculate the area of a circle. Make a symbolic constant for PI.
#include <stdio.h>
// Define a symbolic constant PI
// It is a convention to name symbolic constants in uppercase
#define PI 3.14
int main()
{
int r;
printf("Enter radius: ");
scanf("%d",&r);
printf("Area = %f", PI*r*r);
}
CodePudding user response:
As explained in this thread:
You can do integer arithmetic with the C pre-processor; you cannot do floating point arithmetic with it.
Apparently, the C preprocessor does not allow floating constants in conditional operators.