Why this code outputs "0" in C language? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why this code outputs "0" in C language?

printf("%d", !(1 || 0 && !1));

3rd Jun 2020, 4:23 PM
Win Htay 🇲🇲
Win Htay 🇲🇲 - avatar
2 Answers
+ 5
Win Htay I am not sure but it's because of precedence. As precedence of not (!) Operator is more and it works from right to left so first it will make last 1 to 0 than in next step the more precedence is of and(&&) operator so that perform left to right so 0&&0 will equate to 0. After that 1||0 make 1 as thr result after that again doing not operation it will make that 1 to 0 and print it. if I'm wrong anyone can correct me but it should work like this.
3rd Jun 2020, 4:36 PM
Preity
Preity - avatar
+ 5
code executes from left to right in the bracket also. it executes as this:- 1. printf("%d", !(1 || 0 && !1)); 2. printf("%d", !(1 || 0 && 0)); 3. printf("%d", !(1 || 1)); 4. printf("%d", !(1)); 5. printf("%d", !1); 6. printf("%d", 0); output 0 hence there is output of 0. Hope this will help you. Happy Coding.
3rd Jun 2020, 5:04 PM
ツSampriya😘ツ
ツSampriya😘ツ - avatar