What will be the output of the following C program?#include<stdio.h>int main(){ int a[5] = {5, 3, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); return 0;}
Question
What will be the output of the following C program?#include<stdio.h>int main(){ int a[5] = {5, 3, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); return 0;}
Solution
The output of the given C program will be "5, 4, 20".
Here's the step-by-step explanation:
-
The array
ais initialized with the values{5, 3, 15, 20, 25}. -
i = ++a[1];This line increments the value ata[1](which is 3) by 1, making it 4. The++beforea[1]means the increment happens before the assignment, soiis also 4. -
j = a[1]++;This line assigns the value ata[1](which is now 4) toj, and then incrementsa[1]by 1, making it 5. The++aftera[1]means the increment happens after the assignment, sojis 4. -
m = a[i++];This line assigns the value ata[i](which isa[4]and equals 25) tom, and then incrementsiby 1, making it 5. However, because the++is afteri, the increment happens after the assignment, somis assigned the value ata[4]beforeiis incremented, which is 25. -
printf("%d, %d, %d", i, j, m);This line prints the values ofi,j, andm, which are 5, 4, and 25 respectively.
So, the output of the program is "5, 4, 25".
Similar Questions
What is the output of the C Program?
What will be the output of the following program?
What will be the output of the C program? ans.09119011 Previous Marked for Review Next
What would be the output of the following program?#include <stdio.h>int main( ){int j=1;while (j <= 255){printf ( "%c %d\n ", j, j );j++;}return 0;}ans.
What will be the output of the following program?Note: This question helps in clearing AMCAT exam.12345678910#include <stdio.h>int main() { int c = 0, e = 0; for (e = 0;e < 5; e++) { c++; if (e == 3) break; } printf("%d",c);}
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.