Technical Support

GENERAL: PRINTF EXPANDS '0X0A' INTO '0X0A'+'0X0D'


Information in this article applies to:

  • C51 All Versions
  • CX51 All Versions
  • C166 All Versions
  • C251 All Versions
  • CARM All Versions

QUESTION

When I output a '\n' character the printf function expands it into a '\r' + '\n' sequence. Here is an example:

unsigned char ch;

ch = 0x0A;            // '\n' character
printf("%c",ch);      // output is actually 0x0D + 0x0A ('\r'+'\n')

ANSWER

The putchar function expands 0x0A character into 0x0A + 0x0D. To avoid this expansion, modify the putchar library routine  as follows and add the source file to your project.

Modified putchar code:

char putchar (char c)  {
#if 0                  // do not expand '\n' into CR+LF
  if (c == '\n')  {
    while (!TI);
    TI = 0;
    SBUF = 0x0d;       // output CR
  }
#endif                 // end modification
  while (!TI);
  TI = 0;
  return (SBUF = c);
}

MORE INFORMATION

  • Refer to Basic I/O in the Cx51 User's Guide.
  • Refer to Basic I/O in the C166 User's Guide.
  • Refer to Basic I/O in the CARM User's Guide.

SEE ALSO

Last Reviewed: Thursday, September 22, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure