void main()
{
printf("Adi%d"+2,3);
}
output= i3
This printf statement worked, but how the statement worked ?
void main()
{
printf("Adi%d"+2,3);
}
output= i3
This printf statement worked, but how the statement worked ?
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
.
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
}