in Programming in C retagged by
8,776 views
19 votes
19 votes

​​​​​Consider the following$\text{ ANSI C}$ program.

#include <stdio.h>
int main()
{
    int i, j, count;
    count=0;
    i=0;
    for (j=-3; j<=3; j++)
    {
        if (( j >= 0) && (i++))
            count = count + j;
    }
    count = count +i;
    printf("%d", count);
    return 0;
}

Which one of the following options is correct?

  1. The program will not compile successfully
  2. The program will compile successfully and output $10$ when executed
  3. The program will compile successfully and output $8$ when executed
  4. The program will compile successfully and output $13$ when executed
in Programming in C retagged by
by
8.8k views

4 Comments

Option B?
2
2
yes, it compiles and returns 10
0
0
edited by

Short – circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.


Two important points:

  • In case of logical AND(&&), the second operand is NOT evaluated if first operand is false.
  • In case of logical OR(||), the second operand is NOT evaluated if first operand is true.

Source: https://www.geeksforgeeks.org/operators-in-c-set-2-relational-and-logical-operators/

1
1

 What is the reason behind giving this in option A  ? from which part of code , student will think this option may be true ?

A. The program will not compile successfully

0
0

4 Answers

16 votes
16 votes
Best answer
for (j=-3; j<=3; j++)
{
    if (( j >= 0) && (i++))
        count = count + j;
}

From the above loop code, we can see that the loop iterates $7$ times for $j \in \{-3,-2,-1,0,1,2,3\}.$

Now, we have an $\text{“if”}$ condition and inside it, we have a logical AND operator (&&). In $C$ language we have the following short-circuit rule for binary logical operators

  1. The second operand of logical $\text{OR}$ operator || is ignored if the first operand is non-zero.
  2. The second operand of logical $\text{AND}$ operator (&&) is ignored if the first operand is $0$.

So, for $j \in \{-3,-2,-1\}$ the first operand of && operator (j >= 0) will be $0,$ and hence the second operand (i++) will be ignored. 

For $j \in \{0,1,2,3\}$ the first operand of && operator (j >= 0) will be $1,$ and hence the second operand (i++) will get evaluated $4$ times and final value of $i = 4.$

Initial value of $i = 0.$ 

The postincrement operator i++, returns the original value of $i$ and then increments $i.$ So, when the first time i++ happens, the second operator of logical $\text{AND}$ operator is $0$ and hence the $\text{“if”}$ condition fails. So, count = count +j happens only for $j \in \{1,2,3\}$ and we get $\text{count} = 0 + 1 + 2 + 3 = 6.$

After the loop, we have count = count + i, which makes $\text{count} = 6 + 4 = 10.$

So, the correct option is B.

Reference: https://gateoverflow.in/62409/what-is-the-output

edited by

3 Comments

I have verified with count =1  here when i=0  (i++) will return 0 only after that i will increment to 1 so  condition will be false only we do not need to enter if condition here. I am writing just to clarify one doubt which was asked in operator class.

0
0
in which cases the given code gives compiler error/not executed successfully.
0
0
step 1 : put the bracket based on associative and precedence

step 2: you always go from left to right in order to evaluate

step 3: True || don't need to evaluate

            False && don't need to evaluate

this is how short circuiting work
0
0
4 votes
4 votes
It should be Option B

In for loop till value of  j  does not reach to 0   first  if condition reamin false and so second condition won’t be evaluated and  no change will happen in value of  i  (it will remain 0 )  

Then for  j from 0 to 3 loop will execute 4 times

 to increase value of i  to 4

and count = count+j will cause value to  6

count+i =10  

program will compile successfully to output 10 (no reason to not compile successfully)
1 vote
1 vote
It is possible that few  people will be confused for i value to be 7 in the end but condition will not increment for ( j=-3,-2,-1 ) as j>=0 will be false so further condition will not be checked hence at last i value will be 4 and count =6=(count+j)

count+i = 6+4=10

Correct option is (b).
0 votes
0 votes

Answer 

1 comment

for j=0, the if condition will be false as i++ value is 0 as it is post incerement operator and will be update after sequence point if(). So for this iteration in for, the if block won’t run and count won’t be updated, Even though the results if same if we take it as true but if initially j wouldn’t be 0 the output would have been wrong
1
1
Answer:

Related questions