What will be the output of the following code?123456789#include <stdio.h>int main() { int x = 100; int *ptr = &x; *ptr = *ptr / 3 + 5 - *ptr % 2; printf("%d", *ptr); return 0;}
Question
What will be the output of the following code?123456789#include <stdio.h>int main() { int x = 100; int *ptr = &x; *ptr = *ptr / 3 + 5 - *ptr % 2; printf("%d", *ptr); return 0;}
Solution
The output of the code will be 33.
Here's the step-by-step explanation:
- The integer variable
xis declared and initialized with the value 100. - A pointer
ptris declared and it is assigned the address ofx. So,ptrnow points tox. - The value at the address pointed by
ptr(which isx) is updated with the result of the expression*ptr / 3 + 5 - *ptr % 2.*ptr / 3is equivalent to100 / 3which is33(since it's integer division, the fractional part is discarded).*ptr % 2is equivalent to100 % 2which is0(since 100 is an even number).- So, the expression becomes
33 + 5 - 0which is38.
- The
printffunction prints the value at the address pointed byptr(which is now38).
So, the output of the code will be 38.
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 is the output for the following code?123456789#include <stdio.h>int main(){ char ch = '1'; for (ch = '5'; ch <= '8'; ch++) putchar(ch); return (0);}
What will print out when the following code runs?
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.