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;
}
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.
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.
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.
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
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
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?
k = func1(k=func1(k=func1(k)));
It works by first calling the internal function call or not?
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.
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.
k = func1(k=func1(k=func1(k)));
What is above expression means? I'm confused.
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).
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)
Abhishek said:
10 years ago
According to me local variable gets the preference so k is taken as 35 again and after all being post increment it should print 35 each time.
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.
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 said:
8 years ago
The answer will be 38.
if return k++;
will be given then the value will be
35
if return k++;
will be given then the value will be
35
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers