Consider the following C program:
#include<stdio.h>
int f1(void);
int f2(void);
int f3(void);
int x=10;
int main()
{
int x=1;
x += f1() + f2 () + f3() + f2();
printf("%d", x);
return 0;
}
int f1() { int x = 25; x++; return x;}
int f2() { static int x = 50; x++; return x;}
int f3() { x *= 10; return x;}The output of the program is ______.
View worked solution
Answer: 230
To find the output of the program, we trace the evaluation of the expression in main():
Let's analyze each function call step-by-step:
f1():- Declares a local variable
x = 25. - Increments it to
26and returns26.
- Declares a local variable
- First call to
f2():- Uses a static variable
xinitialized to50. - Increments it to
51and returns51.
- Uses a static variable
f3():- Accesses the global variable
x(initially10). - Updates global
xto and returns100.
- Accesses the global variable
- Second call to
f2():- The static variable
x(currently51) is incremented to52and returns52.
- The static variable
Now, we sum the returned values:
In main(), the local variable x is initially 1. The assignment operator updates it as:
Thus, the program prints 230.
Free practice · Your answers on this page are not saved.
