PDA

View Full Version : C programming question (beginner)


Math Is Hard
Jun13-04, 03:28 PM
Here's a question I am working on for a c programming assignment.

If the following printf statement was part of a complete program, what would it print?

printf("%c",'\x4d');

Now, I know this prints M because I ran it, and I know the output is a single character because %c is a single character conversion spec. But I don't understand what value '\x4d' is returning. Is it just producing the character M and if so why?

Thanks for your help!

Muzza
Jun13-04, 03:33 PM
'\x4d' simply produces the character which has the ASCII code 4D (or 77 in decimal). It happens to be M.

chroot
Jun13-04, 03:34 PM
the \xhh form, where hh are two hex digits, is interpreted by the compiler into the actual byte hh.

- Warren

Math Is Hard
Jun13-04, 03:36 PM
ahhh.. I wondered if it might be ASCII.
Thanks, Muzza, you're a peach!

Math Is Hard
Jun13-04, 03:38 PM
Come again, Warren..? So I am dealing with a hex #?

e(ho0n3
Jun13-04, 03:45 PM
Come again, Warren..? So I am dealing with a hex #?All ASCII characters are encoded as a binary number using eight binary digits. Out of conveniance, it is easier to write the equivalent hexadecimal number rather than the binary. It's just maths.

Math Is Hard
Jun13-04, 04:02 PM
OK, I think I get it. I have forgotten hexadecimal notation and had to punch it in a calculator for hex to base 10 translation.

If i had used '\d77' would I have gotten the same result?

Math Is Hard
Jun13-04, 04:32 PM
hmm.. I guess not. I just tried it.