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

Macro Value Changes

I need to explain for the MACRO define and if else conditions.

When I using the below function I get CHANGE value to 5

#include <stdio.h>

#define CHANGE var
int var;
int main()
{
    var = 5;
    printf("%d",CHANGE);

}

Output: 5

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

But if I use like this:

#include <stdio.h>

#define CHANGABLE_VALUE var
int var;
int main()
{
    var = 5;
    #if CHANGABLE_VALUE == 5
    printf("%d",CHANGABLE_VALUE);
    #else
    printf("There is no value");
    #endif



}

Output: There is no value

Why the #if statements not working ?

>Solution :

Preprocessor definitions (aka Macros) are resolved during pre-compilation time, not during compilation time, and most certainly not during runtime, which seems to be what you’re expecting.

Hence #if CHANGABLE_VALUE == 5 is replaced with #if var == 5 and then resolved as something which is false, so the actual code inside it is not even compiled (let alone executed).

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