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