8 8 votes What will be the outout of the following code? #include <stdio.h> int main() { int a = 1, b = 2; int c = a++ || b++; printf("%d %d %d", a, b, c); } 1 2 1 2 3 1 2 2 1 2 2 0 Programming in C go-programming-1 programming programming-in-c short-circuit-rule two-marks + – Arjun 1.8k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
Best answer 15 15 votes int a = 1, b = 2; int c = a++ || b++; 2nd statement will run a++ and then there is logical OR operator so it will halt. (Short Circuit Rule for logical operators in C). c = 1, a = 2, b = 2 (unaffected) Digvijay Pandey answered Oct 20, 2016 • selected Oct 20, 2016 by Arjun Digvijay Pandey comment Share Follow See all 4 Comments 4 4 Comments reply ukn commented Jan 19, 2017 reply Follow flag what are the short circuit rule in c plz explain 0 0 replyShare Arjun commented Jan 19, 2017 reply Follow flag for logical AND, if the first operand is false (0), second is not evaluated. Similarly for logical OR, if the first operand is 1, the second one is not evaluated. 10 10 replyShare jatin khachane 1 commented Dec 2, 2018 reply Follow flag a++ will be evaluated first or logical OR ..a++ is post increment so is it ??? int c = a || b; a++ ; b++ 0 0 replyShare Pratyush_Raj_Singh commented Mar 18 reply Follow flag Option C is correct. Output will be : 2 2 1 0 0 replyShare Please log in or register to add a comment.
2 2 votes || and && are logical OR and AND. (asked here) | and & are bitwise OR and AND. (not asked here) Check this: https://gateoverflow.in/44842/isro-2013-64 int c = a++ || b++; The logical OR would stop as soon as it finds any positive value ($\equiv$ 1). It stops right at a. So, c = a => c = 1 Now, a++ => a = 2 And b is never touched. So, 2,2,1. Option C JashanArora answered Dec 13, 2019 JashanArora comment Share Follow 0 reply Please log in or register to add a comment.