What is the output of following program?# include <stdio.h>void fun(int x){ x = 30;}int main(){ int y = 20; fun(y); printf("%d", y); return 0;}a.30b.20c.Compile errord.Run error
Question
What is the output of following program?# include <stdio.h>void fun(int x){ x = 30;}int main(){ int y = 20; fun(y); printf("%d", y); return 0;}a.30b.20c.Compile errord.Run error
Solution
The output of the given program is "20".
Here is the step-by-step explanation:
-
The program defines a function called "fun" that takes an integer parameter "x". Inside the function, the value of "x" is assigned as 30, but this assignment does not affect the value of the variable passed from the main function.
-
In the main function, an integer variable "y" is declared and initialized with the value 20.
-
The function "fun" is called with the argument "y". This means that the value of "y" is passed to the "fun" function.
-
Inside the "fun" function, the parameter "x" is assigned the value 30. However, this assignment does not affect the value of "y" in the main function because the parameter "x" is a separate variable.
-
After the function call, the printf statement is executed, which prints the value of "y" (which is still 20) to the console.
-
Finally, the main function returns 0, indicating successful execution of the program.
Therefore, the output of the program is "20".
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.