C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 14)
14.
What will be the output of the program?
#include<stdio.h>
int func1(int);

int main()
{
    int k=35;
    k = func1(k=func1(k=func1(k)));
    printf("k=%d\n", k);
    return 0;
}
int func1(int k)
{
    k++;
    return k;
}
k=35
k=36
k=37
k=38
Answer: Option
Explanation:

Step 1: int k=35; The variable k is declared as an integer type and initialized to 35.

Step 2: k = func1(k=func1(k=func1(k))); The func1(k) increement the value of k by 1 and return it. Here the func1(k) is called 3 times. Hence it increements value of k = 35 to 38. The result is stored in the variable k = 38.

Step 3: printf("k=%d\n", k); It prints the value of variable k "38".

Discussion:
16 comments Page 2 of 2.

Akshay said:   1 decade ago
Can any one explain me.

k = func1(k=func1(k=func1(k)));

What is above expression means? I'm confused.

Shivam said:   1 decade ago
This is not the correct answer first there is no change in value of variable if it's treated in function and when we print it in main no change is seen it can be done only if it's addresses is sent.

Preethiraju said:   1 decade ago
Can anyone explain me the execution of this statement:

k = func1(k=func1(k=func1(k)));

It works by first calling the internal function call or not?

Siva selagala said:   1 decade ago
The difference is: if p=12 then
k=++p;then k value first assiened to 13(k=13)
k=p++;then k value asigned to 12(k=12)
Like that we can calculate

Rupinder said:   1 decade ago
@priya: No you are wrong.If the return statement would be like return(k++),then answer should be 35.But here,in this case,first k is post incremented and in next step return(k++) it actually incremented and return incremented value.

Priya said:   1 decade ago
There is a postfix increment operator so the value should not increase before going to the caller function so there should not be any increment in value.


Post your comments here:

Your comments will be displayed after verification.