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

output= i3

This printf statement worked, but how the statement worked ?

3
  • 4
    This is not an example to follow. Commented Sep 8, 2016 at 6:41
  • Now why does this work? printf(&2??("Adi%d" :> ,010-05); Same kind of pointless obfuscation question. Commented Sep 8, 2016 at 6:46
  • Note What should main() return in C and C++? Commented Sep 8, 2016 at 7:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.