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.
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.
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)
Pratik said:
3 years ago
I agree, thanks @Priya.
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;
}
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;
}
Rachana said:
5 years ago
That's post Decrement so it should be 35.
Shubham said:
5 years ago
Nice explanation and I agree @Ranjeet Kr.
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;
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;
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
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.
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.
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers