You are here:

C/C language

Advertisement


Question
What is the output of the statement

printf("%c","abcdef");

How & Why?

Answer
Hello 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

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.