2
void main()
{
printf("Adi%d"+2,3);
}

output= i3

This printf statement worked, but how the statement worked ?

msc
  • 33,420
  • 29
  • 119
  • 214
AjithAj
  • 77
  • 1
  • 1
  • 10

2 Answers2

8
printf("Adi%d"+2,3);

"Adi%d" - is interpreted as start of the address of the memory where the string literal "Adi%d" is stored. When you add 2 to it, it became address of memory where string "i%d" is stored. So basically you passed to printf string: "i%d". Then %d and printf came into play replacing %d with 3, hence the output i3.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
1

Its part of pointer to character, nothing to do with printf, "Adi" + 2 will make it read from position 0 + 2 = 2 that will be i

int main()
{
    char* a = "Adi" + 2;
    printf(a); // output i

}
Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51