Issue
I am wondering if the code below, when the line #define PRINT_STATEMENT
is commented out, will take many CPU cycles to execute on an ARM microcontroller:
#define PRINT_STATEMENT 1
#if PRINT_STATEMENT
#define PRINT_DBG(...) printf(__VA_ARGS__)
#else
#define PRINT_DBG(...)
#endif
int main(){
PRINT_DBG("Hello World\n");
return 0;
}
I tested commenting out the #define PRINT_STATEMENT
line on an online C compiler and can see that the print statement did not get executed. However, I am still wondering if the program were to be flashed onto an ARM microcontroller (with arm_gcc), will that line be turned into a nop
operation?
Solution
The compiler is free to produce a nop or not. It's pretty much free to produce any program that does not print anything.
In modern compilers, preprocessing and compiling is done simultaneously. But in theory, the compiler gets the code after the preprocessor. And in that case, provided that you have set PRINT_STATEMENT
to 0, the compiler will only see a main function with a sinle return 0;
statement.
The standard does not dictate what should happen on this level. If you want to know what happened in your specific case with your compiler and your target, you will have to take a look at the assembly.
The most likely scenario is that it gets completely removed. Both because the compiler will not even see it, and because why produce a nop when you can produce nothing?
Answered By - klutt Answer Checked By - Robin (WPSolving Admin)