What will be the output of the given code?123456789101112#include <stdio.h> void fun(int *ptr){ *ptr = 30;}int main(){ int y = 20; fun(&y); printf("%d", y); return 0;}
Question
What will be the output of the given code?123456789101112#include <stdio.h> void fun(int *ptr){ *ptr = 30;}int main(){ int y = 20; fun(&y); printf("%d", y); return 0;}
Solution
The output of the given code will be 30.
Here's the step by step explanation:
-
The main function is called first in C programming. Here, an integer 'y' is declared and initialized with the value 20.
-
Then, the function 'fun' is called with the address of 'y' as the argument. This is done using the '&' operator which returns the address of a variable.
-
Inside the function 'fun', the pointer 'ptr' points to the address of 'y'. The value at this address is then changed to 30. This change is reflected in the 'y' variable in the main function because 'ptr' is pointing to 'y'.
-
Finally, 'y' is printed out using the printf function. Since the value of 'y' was changed to 30 in the 'fun' function, 30 is the output of this program.
Similar Questions
What will be the output of the following code?
What will be the output of the following program?1234567891011121314151617181920#include <stdio.h>void one();void two();void one() { for(int i = 0; i < 2; i++){ two(); }}void two() { printf("TWO ");}int main(){ one(); two(); return 0;}
What will be the output of the following code?a=(1111,2)print(a)
123456789101112#include <stdio.h> int main(){ if(10){ printf("x"); printf("y"); } else printf("z"); printf("w"); return 0;}
What will be the output of the following code snippet?123456789101112131415#include <stdio.h> int main() { char direction = 'N'; if (direction == 'N') printf("North"); else if (direction == 'S') printf("South"); else if (direction == 'E') printf("East"); else if (direction == 'W') printf("West"); else printf("Unknown"); 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.