This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Printf problem..

Hi,

This is my program.

int a = 0x0A;

printf("%c",a);

Why is it that the output from the serial port is 0x0D and 0x0A instead of just 0x0A itself?

Thanks..

Parents
  • %c means to print characters.
    0x0A is not a character. It is a code.
    The range of "printable characters" is something like 61 to 7F for lower case a-z and some other for upper case A-Z, plus the !.;; and other types.
    Do a web search for ASCII table.

    if you want to print hex values, rather than the character values, do a printf("%04X", SomeInt);

    HTH,

Reply
  • %c means to print characters.
    0x0A is not a character. It is a code.
    The range of "printable characters" is something like 61 to 7F for lower case a-z and some other for upper case A-Z, plus the !.;; and other types.
    Do a web search for ASCII table.

    if you want to print hex values, rather than the character values, do a printf("%04X", SomeInt);

    HTH,

Children
  • "0x0A is not a character. It is a code."

    That's extremely misleading. 0x0A is the numeric represention (ASCII code) for the ASCII character 'newline' or 'linefeed'. %c is a perfectly good way of outputting a linefeed with printf().

    The concept of "printable characters" has nothing to do with printf().

    Stefan

  • >>"0x0A is not a character. It is a code."

    >That's extremely misleading.
    >0x0A is the numeric represention (ASCII >code)

    You just stated that it is a code. ASCII code.

    I don't think it is "extremely misleading".

    Sure, an ASCII terminal represents the 0x0A as a Line feed or Carriage return. (Can't remember), but I don't think the user is that deep into the theory and is probably using it as an ascii character by the message.

    Regards,

  • "You just stated that it is a code. ASCII code."

    Yes, but so is 0x41. And that's an 'A'. It doesn't mean it isn't a character.

    "I don't think it is "extremely misleading"."

    Ok, totally wrong then.

    Every value in the range 0 to 127 is an ASCII character.

    "Sure, an ASCII terminal represents the 0x0A as a Line feed or Carriage return"

    Like I said, it's a linefeed.

    "but I don't think the user is that deep into the theory and is probably using it as an ascii character by the message"

    Yes, he is using it as an ASCII character. That's because it IS an ASCII character. But you've gone and told him it isn't.

    To be honest, I don't think that understanding what an ASCII character is is exactly being "deep into the theory". It's pretty fundamental stuff.

    Stefan