42 views
#include<stdio.h>

int x=14;
int fun(){
return x--;
}

int main(){
int sum=0;
for(fun();--x;fun())
sum=sum+fun();
printf("%d",sum);
return 0;
}

What will be the output?

See , inside for loop , at the time of initialization fun() is called and 14 become 13

Next condition checking ‘--x’ is called, again 13 become 12

Next, sum =sum+fun();

As fun() is a post decrement operation , so fun() value 12 is added with sum and then decremented to 11.

In iteration 2 of ‘for’ loop

x is containing 11,

Now  for(${\color{Red} {fun()}}$;--x;${\color{Green} {fun()}}$)

${\color{Green} {fun()}}$: Here fun also first assign then decrement, but where it will decrement? It will not decrement until and unless it will get some sequence point. right? Where will ${\color{Green} {fun()}}$ it get sequence point?

Will in this iteration value of sum be 9?

| 42 views
0
0
yes, but that I havenot asked

+1 vote

$x=1 \text{ and sum = }0 \text{ before loop iteration starts}$

$1^{st} \text{ iteration}$

In the initialization fun has been called which will return the value 14 but post decrement will save $\text{X=13}$ in it. Then condition of loop will be evaluated which will first decrement $X$ value to 12 and then sum will be evaluated. Fun will called from statement

sum = 0 + 12;

Here fun() will return the value 12 but $X$ will save value 11.

That means every time fun() is being called which will return a value and save 1 less than the value returned

So at the end of $1^{st}$ iteration $\text{X=11 and sum = 12}$

$2^{nd} \text{ iteration}$

In the increment section of loop fun() will return value 11 but $\text{X will be 10}$. Then condition of loop will decrement $X$ to 9. Now sum will be evaluated as

sum = 12 + 9;

and $\text{X will be changed to 8}$

So at the end of $2^{nd}$ iteration $\text{X=8 and sum = 21}$

$3^{rd} \text{ iteration}$

In the increment section of loop fun() will return value 8 but $\text{X will be 7}$. Then condition of loop will decrement $X$ to 6. Now sum will be evaluated as

sum = 21 + 6;

and $\text{X will be changed to 5}$

So at the end of $3^{rd}$ iteration $\text{X=5 and sum = 27}$

$4^{th} \text{ iteration}$

In the increment section of loop fun() will return value 5 but $\text{X will be 4}$. Then condition of loop will decrement $X$ to 3. Now sum will be evaluated as

sum = 27 + 3;

and $\text{X will be changed to 2}$

So at the end of $4^{th}$ iteration $\text{X=2 and sum = 30}$

$5^{th} \text{ iteration}$

In the increment section of loop fun() will return value 2 but $\text{X will be 1}$. Then condition of loop will decrement $X$ to 0 and the loop will be terminated. So finally $\text{sum = 30 will be stored in the end}$

by (475 points)
selected by
0

@!KARAN

U have told this

Here fun() will return the value 12 but X will save value 11.

But please check  , my explanation

sum=sum+fun();

We can say like this

sum=sum+something--;

Now check the precedence of post decrement operator and addition operator

https://aticleworld.com/operator-precedence-and-associativity-in-c/

We can say post decrement operator has higher precedence. So, will evaluate before addition operation. Then why not sum will store 11 too??

0

We can say post decrement operator has higher precedence. So, will evaluate before addition operation. Then why not sum will store 11 too??

The answer to that question lies in the property of

$\textbf{post decrement operation and Activation record}$

In post decrement operation - Say a variable $Y$ is being used, then its value is $\textbf{first used}$ then its value is decremented by 1.

Whenever a function is called its $\textbf{activation record}$ is created and and as soon as the last statement is executed successfully its corresponding activation record is deleted. Activation records has many components including like

$\color{blue}{\text{Actual Parameters, Return value, Control Link, Access Link, Machine Status}}$ etc.

So once the fun() reaches last statement it prepares the return value and and also the final value to be stored in the variable $X$. After the last statement executed successfully then activation record of the fun() is deleted.

We can say like this

sum=sum+something--;

Yes we can say that but value of $\textbf{something}$ will be used first with the sum and then $\textbf{something}$ will be decremented by 1.

+1

U might not got my point

We can say post decrement operator has higher precedence. So, will evaluate before addition operation.

How higher precedence operator work after lower precedence operator?

0

sum=sum+something--;

Here technically the post decrement operator evaluates first.

since the value of something-- is something itself(it is decremented later),it seems to be  -- operator is not evaluated  and + operator is evaluated first which is wrong.