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 1 of 2.

Srishti said:   1 decade ago
@Akshay.

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

We are simply solving the brackets. As we know a functions requires an argument. In order to get the required argument for k=func1(), we are solving the inside terms of k=func1(........).

In k= func1(k=func1("k=func1(k)")), the last term inside " " sends a function call to func1(k) with argument k=35.

Once the value is returned from the called function it becomes the parameter of next bracket statement as --> k=func1(k=func1(36)) --> k=func1(37).
(1)

Varun ranjan said:   4 years ago
# include <stdio.h>


int post(int i)
{
int m=i++;
return m;//////return then increment
}

int post3(int k)
{
k++;
return k;//////increment then return
}

int post2(int j)
{
return j++;/////return then increment
}


int main()
{
int i =10;
printf("%d\n", post(i));


int j=20;
printf("%d\n", post2(j));


int k=23;
printf("%d",post3(k));
return 0;
}

Shonit said:   3 years ago
The output should be 35.

Because it's call by value can't increase the value in the calling function as its function activation record will be destroyed each time while returning the value. If it will pass by reference then op would be 38.
(1)

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.

Naresh said:   8 years ago
Here we are taking about post increment meaning ( first initialize later increment).

void main (){
int i=1 j;
j= i++;
printf ("%d%d", i, j);
}

The output will be i=2, j=1 so in above example output should be 35.

Ranjeet kr said:   8 years ago
The right answer will be 38.

If there will be statement like this,

return k++ then the value will be 35
and 35 will be also in other case if
we declare as the extra variable as follows
int i =k++;
return i;

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.

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.

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


Post your comments here:

Your comments will be displayed after verification.