+ 1
[solved]Can anyone explain why the output is "Try Again"
Check the code in this post -- https://www.sololearn.com/post/281725/?ref=app
6 Answers
+ 1
If macro was not defined, then ! operator with nonexisting macro name is evaluated to true. it is not an error for #if directive (not to be confused with if statement for which it will be an error).
+ 2
Akshay Jain Do not confuse preprocessor directives with conditional if statement. Revise the course again if needed or read on it on the internet. Also see the example that I have shared.
+ 1
#if is preprocessor directive which uses to make conditional compilation.
If condition in #if directive evaluates to true then the subsequent code (till #else) will be included in the compilation process. If it evaluates to false then code followed after #else directive (till #endif) will be included in the compilation process.
Condition of #if directive is !SOLO. It is evaluated to true because there is no any difinition of SOLO (! means not). So, after prepocessing stage the compiler will get follow:
int main(){
printf("Try again");
return 0;
}
As result "Try again" will be printed.
0
SOLO is not defined, so #if !SOLO will return true and it prints 'Try Again'.
If I define SOLO then it will result in false and the else part is executed.
https://code.sololearn.com/chC8zcRItFd9/?ref=app
0
Avinesh As SOLO is not defined so it should show error...how it can return True???
If SOLO is defined to be 0 then only it should show "Try Again".
0
Got It...thanks for answering andriy kan Avinesh