What will be the output of the following C code?#include <stdio.h> void main() { m(); m(); } void m() { static int x = 5; x++; printf("%d", x); }6 76 65 55 6
Question
What will be the output of the following C code?#include <stdio.h> void main() { m(); m(); } void m() { static int x = 5; x++; printf("%d", x); }6 76 65 55 6
Solution
The output of the given C code will be "6 7".
Here's the step by step explanation:
-
The main function is called first. Inside the main function, the function m() is called twice.
-
The function m() has a static integer variable x which is initialized to 5. The keyword 'static' means that the variable retains its value between function calls.
-
When m() is called the first time, x is incremented by 1 (from 5 to 6) and then printed. So, "6" is printed.
-
When m() is called the second time, x is again incremented by 1 (from 6 to 7) and then printed. So, "7" is printed.
-
Therefore, the output of the program is "6 7".
Similar Questions
what will be the output of the c program?
What will be the output of the following code?
What will be the output of the following C++ code?
What will be the output of the following program?
What will be the output of the following C code?#include <stdio.h>int main() { int i; for (i = 0; i < 5; i++); printf("%d\n", i); return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.