113k views
4 votes
//CODEvoid f(int a){while(a--) {static int n = 0;int x = 0;cout << "n: " << n++ << " ,x: " << x++ << endl;}}int main(){f(3);return 0;}Question: What is the output of the above code?

1 Answer

3 votes

Answer:

The static n variable is incremented twice and the results printed in the console but the output of the x variable remains constant.

Step-by-step explanation:

The while loop in the C++ source code increments the n variable as the argument of the function "a" is decremented. The n variable output is 0,1, and 2 while the x variable is 0 for every iteration.

//CODEvoid f(int a){while(a--) {static int n = 0;int x = 0;cout << "n: &quot-example-1
User Franz Becker
by
4.7k points