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

How does #define carries the function name in c?

After working for more than 10 years, today a code caught my eye, I am unable to understand the function name defined inside a function gets printed in the output/log without being passed as an argument in macro or being defined as a global variable. Please help me understanding the internal. Please see the screenshot for the reference.

/*...*/
#include <stdio.h>
#define x printf("%s", f);
int main() {
    char *f = "MAIN";
    printf("Hello World");
    x;
    
    return 0;
}

Output:

Hello WorldMAIN

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

>Solution :

C preprocessor macros simply do text replacement. They have no semantic awareness of your program.

This:

#include <stdio.h>
#define x printf("%s", f);

int main()
{ 
    char* f = "MAIN";  
    printf ("Hello World");
    x;
    return 0;
}

Becomes:

#include <stdio.h>

int main()
{ 
    char* f = "MAIN";  
    printf ("Hello World");
    printf("%s", f);;
    return 0;
}

Please note that if there is no f declared when this macro is used, you will see a compiler error. If f is declared, but is not a char *, you should see compiler warnings.

Some preprocessor macro best practices include (but are not limited to) using capitalized names, as x by convention looks like a variable or function name; and being careful about what syntactically significant symbols (in this case ;) you include in your macro text.

Hopefully this example was done for the sake of learning, because it is wholly unnecessary. Preprocessor macros wouldn’t exist if they didn’t serve a purpose, but beware they can easily obfuscate code.

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