What will be the output for the following code?12345678910111213#include <stdio.h>void setZero(int *num) { *num = 0;}int main() { int i = 10; int *ptr = &i; setZero(&(*ptr)); printf("%d", *ptr); return 0;}
Question
What will be the output for the following code?12345678910111213#include <stdio.h>void setZero(int *num) { *num = 0;}int main() { int i = 10; int *ptr = &i; setZero(&(*ptr)); printf("%d", *ptr); return 0;}
Solution
The output of the code will be 0.
Here's the step by step explanation:
-
The
mainfunction is called. Insidemain, an integeriis declared and initialized with the value 10. -
A pointer to an integer
ptris declared and it is initialized with the address ofi. So,ptris pointing toi. -
The
setZerofunction is called with the address ofi(which is whatptris holding). -
Inside the
setZerofunction, the value at the address that the pointernumis holding (which is the address ofi) is set to 0. This changes the value ofito 0. -
Back in the
mainfunction, the value at the address thatptris holding (which is the value ofi) is printed. Sinceiwas set to 0 in thesetZerofunction, 0 is printed. -
The
mainfunction returns 0, signaling successful termination of the 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 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; }
What will be the output of the following code?a=(1111,2)print(a)
What is the output for the following code?12345678910111213#include <stdio.h> int main() { int i = 1; while (i <= 10) { if (i % 5 == 0) { i++; continue; } printf("%d ", i); 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.