0

In C Programming, If a pre processor directive is not defined it is ignored as un defined. The below code doesn't print, since PRINT_DEFINED macro is not defined, is there a way to get it flagged during compilation time to avoid any potential miss of the feature.

# include <stdio.h>
int main (void)
{
#if PRINT_DEFINED
    printf("I am printing the statement\n");
#endif
    return 0;
}

The problem with using #ifdef and #error is I have a large code base and where if any wrong macro is entered assuming that is defined it will be skipped in compilation which is undesired.

Iker
  • 123
  • 1
  • 7
  • Check out the top answer to [this question](https://stackoverflow.com/q/2221517/1679849). – r3mainer Jun 16 '21 at 07:30
  • Thanks for the response @r3mainer , but the problem is I have a large code base and where if any user gives a wrong macro assuming that is defined it will be skipped in compilation without notice – Iker Jun 16 '21 at 07:32
  • 1
    It's totally unclear why you can't use `#ifdef` and `#error `. – Jabberwocky Jun 16 '21 at 07:44
  • 1
    If I understand correctly, you want it to fail if someone misspells one of the valid macros (e.g. `PRNT_DIFENED`). That's not a feature that exists. You could implement your own build step that does this, you would have to scan the whole codebase for all tokens that occur after `#if/#ifdef/#ifndef` and issue a warning/error if any of the `-D` options defines something that is not found among those. –  Jun 16 '21 at 08:43
  • Corrections on terminology and C rules: `PRINT_DEFINED` is not a “preprocessor directive.” It is an identifier. Directives are things like `#if`, `#define`, and `#include`. The rule in C `#if` and `#elif` directives is that if an identifier is still present after macro replacement and processing of `defined` operators, it is replaced with `0`, not that it is ignored. – Eric Postpischil Jun 16 '21 at 09:00
  • 2
    It would appear that you should simply compile with `gcc -Wundef`. See this: https://stackoverflow.com/questions/54476352/how-to-catch-undefined-macro-in-preprocessor-if-condition – Lundin Jun 16 '21 at 09:05
  • Thank you @Lundin it really helps – Iker Jun 16 '21 at 09:39
  • @Iker If that answered your question, I can close this as a duplicate to the linked one. (Which doesn't mean there's anything wrong with the question, just that it has been answered elsewhere.) – Lundin Jun 16 '21 at 09:40
  • Yes Lundin, please close this question as duplicate – Iker Jun 16 '21 at 09:41
  • @Jabberwocky if there is extensively large codebase then it becomes difficult to do it and also it should come as a coding standard. – Iker Jun 16 '21 at 09:43

0 Answers0