What will be the output of the following C code? #include <stdio.h> void main() { int x = 0; int *ptr = &x; printf("%d\n", *ptr); } Address of xJunk value0Run time error
Question
What will be the output of the following C code? #include <stdio.h> void main() { int x = 0; int *ptr = &x; printf("%d\n", *ptr); } Address of xJunk value0Run time error
Solution
The output of the given C code will be 0.
Here's the step by step explanation:
-
The integer variable 'x' is declared and initialized with the value 0.
-
A pointer variable '*ptr' is declared and it is assigned the address of 'x'. So, it points to the memory location of 'x'.
-
The printf function is used to print the value at the address pointed by 'ptr'. The '*' before 'ptr' is a dereference operator which is used to access the value at the address that 'ptr' is pointing to.
-
Since 'ptr' is pointing to 'x', and the value of 'x' is 0, the output of the printf function will be 0.
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.