Array indexing error?

Two students A and B were given an assignment to write a C code in which an int array a of 5 elements is declared/initialized with {1,2,3,4,5} at compile-time and the 2nd element (0-indexing) of a is printed in output (stdout).

Given below are the code snippets written by A and B respectively.

Code by A:

1
2
3
4
5
6
#include<stdio.h>
int main() {
    int a[5]={1,2,3,4,5};
    printf("%d",2[a]);
    return 0;
}

Code by B:

1
2
3
4
5
6
#include<stdio.h>
int main() {
    int a[5]={1,2,3,4,5};
    printf("%d",a[2]);
    return 0;
}

Which of the two code snippets are correct (they give the desired output without any errors) ?

×

Problem Loading...

Note Loading...

Set Loading...