372 views
7 7 votes

What is the output of the following code?

#include <stdio.h>

int main() {
    int a[] = {2, 4, 6, 8, 10};

    printf("%d %d %d", *a, *(a + 3), 3[a]);

    return 0;
}
  1. $\texttt{2 6 8}$
     
  2. $\texttt{2 8 8}$
     
  3. $\texttt{4 8 10}$
     
  4. $\texttt{Compilation error}$

2 Answers

0 0 votes

In C, the array name $\texttt{a}$ represents the base address of the array.

So,

$\texttt{a}$ means $\texttt{(a + 0)}$

Therefore,

$\texttt{*a = a[0] = 2}$

Now,

$\texttt{*(a + 3)}$ means value at index $\texttt{3}$.

So,

$\texttt{*(a + 3) = a[3] = 8}$

Also,

$\texttt{3[a]}$ looks unusual, but it is valid in C.

Because:

$\texttt{3[a] = *(3 + a)}$

And this is the same as:

$\texttt{a[3]}$

So,

$\texttt{3[a] = 8}$
 

$\therefore$ Output : $\texttt{2 8 8}$
 

Answer: B

Answer:
Position:
Show:

Related questions

8 8 votes
2 2 answers
374
374 views
GO Classes asked Jun 22
374 views
What is the output of the following code?#include <stdio.h int main() { int a[] = {2, 4, 6, 8, 10}; int *p = a + 2; printf("%d %d %td", p[-1], 1[p], p - a); return 0; }$\...
7 7 votes
3 3 answers
357
357 views
GO Classes asked Jun 22
357 views
What is the output of the following code?#include <stdio.h int main() { int a[4] = {6, 4, 1, 2}; int b[8] = {9, 8, 11, 10, 5, 7, 0, 3}; int *p = &a ; int *q = b; int *r =...
8 8 votes
2 2 answers
357
357 views
GO Classes asked Jun 22
357 views
What is the output of the following code?#include <stdio.h int main() { int a[] = {3, 6, 9, 12, 15}; int *p = a + 1; printf("%d %d %td", *p, *(p + 2), p - a); return 0; }...
8 8 votes
2 2 answers
365
365 views
GO Classes asked Jun 22
365 views
What is the output of the following code?#include <stdio.h int main() { int a[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; int *b = a + 4; int *c = &(a[4]); printf("%d...