7 votes 7 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 + – Arjun asked Oct 18, 2016 Arjun 923 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 12 votes 12 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 3 Comments See all 3 3 Comments reply ukn commented Jan 19, 2017 reply Follow Share what are the short circuit rule in c plz explain 0 votes 0 votes Arjun commented Jan 19, 2017 reply Follow Share 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. 9 votes 9 votes jatin khachane 1 commented Dec 2, 2018 reply Follow Share a++ will be evaluated first or logical OR ..a++ is post increment so is it ??? int c = a || b; a++ ; b++ 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 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 See all 0 reply Please log in or register to add a comment.