#include<stdio.h>
int main(void)
{
int a=5;
printf("%d"+1,a);
}
Output: d.
I didn’t get how the output is coming: d ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You passed as first argument of
printf"%d"+1;"%d"is actually seen as aconst char *that points to a memory location where%dis stored. As with any pointer, if you increment it by one, the result will point to the following element, which, in this case, will bed.ais not used, but this should not be a problem since in general (I don’t know if it’s standard-mandatedEdit: yes it is, see bottom) the stack cleanup responsibility for variadic functions is up to the caller (at least,cdecldoes it that way, this however may or may not be UB, I don’t know*).You can see it easier this way:
Thus, (
"%d"+1) (which is"d") is interpreted as the format string, andprintf, not finding any%, will simply print it as it is. If you wanted instead to print the value ofaplus 1, you should have doneEdit:
* ok, it’s not UB, at least for the C99 standard (§7.19.6.1.2) it’s ok to have unused parameters in
fprintf:and
printfis defined to have the same behavior at §7.19.6.3.2