+ 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

2nd Apr 2020, 6:37 AM
Akshay Jain
Akshay Jain - avatar
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).
2nd Apr 2020, 7:14 AM
andriy kan
andriy kan - avatar
+ 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.
2nd Apr 2020, 8:12 AM
Avinesh
Avinesh - avatar
+ 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.
2nd Apr 2020, 7:10 AM
andriy kan
andriy kan - avatar
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
2nd Apr 2020, 6:58 AM
Avinesh
Avinesh - avatar
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".
2nd Apr 2020, 7:07 AM
Akshay Jain
Akshay Jain - avatar
0
Got It...thanks for answering andriy kan Avinesh
2nd Apr 2020, 9:18 AM
Akshay Jain
Akshay Jain - avatar