1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C programming question (beginner)

  1. Jun 13, 2004 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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!
     
  2. jcsd
  3. Jun 13, 2004 #2
    '\x4d' simply produces the character which has the ASCII code 4D (or 77 in decimal). It happens to be M.
     
  4. Jun 13, 2004 #3

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    the \xhh form, where hh are two hex digits, is interpreted by the compiler into the actual byte hh.

    - Warren
     
  5. Jun 13, 2004 #4

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    ahhh.. I wondered if it might be ASCII.
    Thanks, Muzza, you're a peach!
     
  6. Jun 13, 2004 #5

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Come again, Warren..? So I am dealing with a hex #?
     
  7. Jun 13, 2004 #6
    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.
     
  8. Jun 13, 2004 #7

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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?
     
  9. Jun 13, 2004 #8

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    hmm.. I guess not. I just tried it.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C programming question (beginner)
  1. Beginner question (Replies: 4)

  2. Beginner Question (Replies: 3)

Loading...