Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A little bit confused with using conditional compilation

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
}

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading