1
#include <stdio.h>
int main()
{
int a=3, b = 6;
printf(&a["Hi!Hello! %s\n"], &b["Mnnit/Softathalon"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
return 0;
}

output:

Hello! Softathalon

That is C !

Why is this the output? Can anyone explain different format specifier in it?

Community
  • 1
  • 1
Utkarsh Srivastav
  • 3,105
  • 7
  • 34
  • 53

1 Answers1

6

For any array T arr[N], the expression arr[i] is equivalent to *(arr + i).

Because the addition is commutative in the latter expression, you can also write this as *(i + arr), and hence as i[arr].

In particular, arr[3] and 3[arr] denote the same thing.

It's one of those "curiously funny things you can do in C", but it should go without saying that serious code should never actually use such a construction.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084