C/C language
Expert: Zlatko - 9/28/2011
QuestionWhat is the output of the statement
printf("%c","abcdef");
How & Why?
AnswerHello Deepak
In C, any text in quotes is a string. The string is a sequence of bytes in memory. Any text in quotes evaluates to a pointer which points to the first byte of the string.
This means that where we see "abcdef", the compiler really sees the address of the first character.
Try this
printf("%p", "abcd");
You will see the address printed.
Lets say the address is 0x45464748.
printf("%c", "abcd") is the same as
printf("%c", 0x45464748);
The value 0x45464748 uses 4 bytes but %c prints one byte. So one of the bytes is interpreted as a printable character and that character is printed out. In some cases, you might get a visible character, if the byte happens to have the right value. In other cases, you might get nothing, of the byte has no meaning as a visible character. In the example above, the last byte of the address is 0x48, which corresponds to the capital H, so capital H is printed.
You probably already know that the 0x in front of the number means that the digits are hexadecimal digits.
I hope that helps you to understand.
Best regards
Zlatko