What happens when you compile and run the following program?
#include
int fun (void) {
static int i = 1;
i += 2;
return i;
}
int main (void) {
int k, 1;
k = fun ();
1 = fun () ;
printf ("%d", 1 - k);
return 0;
}
Choose the right answer:
The provided program has a few key points to consider:
1. fun is a function that uses a static variable i. This means i retains its value between function calls. It's initialized to 1 and then incremented by 2 each time fun is called.
2. The main function calls fun twice, assigning the results to k and l (though there's a typo in the variable name l, it should be l = fun();, not 1 = fun();).
Let's step through the code:
* First call to fun: i starts at 1, increments by 2, so i becomes 3. This value (3) is as-signed to k.
* Second call to fun: i is now 3, increments by 2 again, so i becomes 5. This value (5) is assigned to l.
Finally, the printf statement attempts to print l - k, which is 5 - 3, resulting in 2.
So, the correct answer is:
A . The program outputs 2.
Currently there are no comments in this discussion, be the first to comment!