void main()
{
printf("ABCD");
printf("\n");
printf("ABCD" +1);
printf("\n");
printf("ABCD" +3);
}
Outputs is:
ABCD
BCD
D
Can anyone explain me why?
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.
"ABCD"is actually an array of characters{'A','B','C','D', '\0'}(where'\0'is the trailing null byte). If you add 3 to that, then that is the equivalent of advancing a pointer 3 bytes forward from A, so you end up pointing atD.Question 6.2 in the C FAQ has a picture that makes this clearer. The array decays to a pointer as described in 6.4 so you have the situation of the variable
p.