LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 2

Output of 2[a], *(a + 3) and *a + 3 in C Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a[] = {5, 6, 7, 8};
    printf("%d %d %d\n", 2[a], *(a + 3), *a + 3);
    return 0;
}
  • A 7 8 8 Correct
  • B 7 8 6
  • C 6 7 8
  • D Compilation error
Correct Answer: Option A

2[a] is *(2 + a) = a[2] = 7. *(a + 3) is a[3] = 8. *a + 3 takes a[0] (5) and adds 3 = 8 – the brackets make all the difference.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links