2[a] in C – Why Backwards Array Indexing Compiles and Runs

2[a] looks like a typo that should never compile — the index and the array are backwards. Yet it compiles cleanly, runs, and prints a well-defined value. This question from our C Programming Quiz App is a favorite C interview curveball, and the explanation reveals what array subscripting actually is underneath.

The Quiz Question

int a[3] = {1, 2, 3};
printf("%d", 2[a]);

What is printed by this code?

  1. 1
  2. 2
  3. 3
  4. Compile error

The Correct Answer: 3

2[a] is exactly a[2] — the third element, which is 3. Verified on gcc 13.3 and Apple clang 21, with -Wall -Wextra producing no warnings at all:

$ gcc -Wall -Wextra weird.c && ./a.out
3

The reason is the definition of the subscript operator. The C standard (C11 §6.5.2.1) says E1[E2] is identical to *((E1) + (E2)) — subscripting is nothing but pointer addition plus a dereference. And since addition commutes:

a[2]  ≡  *(a + 2)  ≡  *(2 + a)  ≡  2[a]

The compiler never “checks which side is the array”. It just adds the two operands — one must be a pointer (the array decays to one), the other an integer — and dereferences the result. Which side each sits on is irrelevant.

Why Each Wrong Answer Is Wrong

Why not 1?

1 is a[0]. This answer usually reflects a guess that the “weird” syntax somehow resets to the first element, or that 2[a] means “2 copies of a” and the compiler takes the start. There’s no such rule — the arithmetic is the same as a[2].

Why not 2?

2 is a[1] — the off-by-one trap layered on top of the syntax trick. Offset 2 from the start is the third element, not the second. We printed a[2] alongside 2[a] in the same run and got 3 3: identical, as the standard requires.

Why not “Compile error”?

This is the answer most people pick, and it’s wrong precisely because subscripting is defined as commutative addition. Neither gcc nor clang emits even a warning — as far as the language is concerned, nothing unusual happened. (A linter or code reviewer is another matter.)

Should You Ever Write 2[a]?

No. It’s valid C, but it communicates nothing except that the author knows an obscure fact — and code is read far more often than it’s written. The form survives in obfuscated-code contests and interview questions, and historically in early C code before conventions settled. Its real value is pedagogical: once you understand why 2[a] works, you truly understand that arrays in expressions are pointer arithmetic in disguise — the same insight behind why *(p + 1) reads the next element, and why array parameters in functions are “really” pointers.

Frequently Asked Questions

Is 2[a] valid C?

Yes. The standard defines E1[E2] as *((E1) + (E2)), and addition commutes, so 2[a] and a[2] are the same expression. Both compile warning-free on gcc and clang.

Why does array indexing commute in C?

Because subscripting isn’t a primitive operation — it’s sugar for pointer addition plus dereference. In a + 2 or 2 + a, the array decays to a pointer either way, and pointer-plus-integer addition accepts its operands in either order.

Does 2[a] work in C++ too?

For built-in arrays and pointers, yes — C++ inherits the same definition. It stops working for class types with an overloaded operator[] (like std::vector), where the object must be on the left.

Related Reading

Recommended Books

This question is #93 in the C Programming Quiz App — 155 questions with explanations covering operators, pointers, memory, and more.
Download on Google Play →

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>