What happens if you try to compile and run this program?
#include
int fun(int i) {
return i++;
}
int main (void) {
int i = 1;
i = fun(i);
printf("%d",i);
return 0;
}
Choose the correct answer:
Suggested Answer: D
Get Premium CLA-11-03 Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
In the fun function:
cCopy code
int fun(int i) { return i++; }
The post-increment operator i++ returns the current value of i and then increments it. So, fun(i) will return the current value of i (which is 1) and then increment i to 2.
In the main function:
cCopy code
int i = 1; i = fun(i); printf('%d', i);
Here, i is assigned the result of fun(i), which is 1. So, the program prints the value of i, which is 1.
Therefore, the correct answer is D. The program outputs 1.
10 comments · newest first
MA
Marvel
3 hours ago
I think it outputs 1. The increment happens after return.
VA
Vanda
5 days ago
Compilation won't fail, it's valid C code.
NA
Nan
10 days ago
I’m not sure about that, seems weird.
CH
Chantell
16 days ago
Wait, I thought it would output 2!
AL
Alline
21 days ago
Totally agree, it's just the original value.
JA
Janessa
26 days ago
The program outputs 1.
NY
Nydia
1 month ago
I’m confused about the increment part; does it mean the output could be unpredictable? I hope it’s not that!
LE
Leah
1 month ago
I practiced a similar question, and I feel like the output should be 1 since `i++` returns the original value.
GA
Gail
1 month ago
I'm not entirely sure, but I remember something about post-increment and how it works. Could it be 2?
AN
Antonio
2 months ago
I think the function returns the value of `i` before the increment, so it should output 1.