What is the output of the following code?123456789101112#include <stdio.h>int main() { int x = 10; int y = 5; int *a = &x; int *b = &y; (*a)++; *b = (*b) * 2; printf("%d %d", *a, *b); return 0;}
Question
What is the output of the following code?123456789101112#include <stdio.h>int main() { int x = 10; int y = 5; int *a = &x; int *b = &y; (*a)++; *b = (*b) * 2; printf("%d %d", *a, *b); return 0;}
Solution
The output of the code will be "11 10".
Here's the step by step explanation:
-
The code first declares two integer variables
xandy, and assigns them the values 10 and 5 respectively. -
Then it declares two pointers
aandb, and assigns them the addresses ofxandyrespectively. So,apoints toxandbpoints toy. -
The line
(*a)++;increments the value ofxby 1. So,xbecomes 11. -
The line
*b = (*b) * 2;multiplies the value ofyby 2. So,ybecomes 10. -
Finally, the
printffunction prints the values pointed to byaandb, which are the updated values ofxandy. So, it prints "11 10".
Similar Questions
What is the output for 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?12345678910111213#include <stdio.h> int main() { int i = 1; while (i <= 10) { if (i % 5 == 0) { i++; continue; } printf("%d ", i); i++; } 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 is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}101010101001234567890101010101I don't knowSubmit
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.