Keil Logo


Information in this article applies to:

  • C51 All Versions


It appears that my \n does not work in printf() statements. When I view the output on a dumb terminal, I do not see a carriage return-line feed. For example:

printf("hello world\n");

The above statement does not appear to send the '\n'. Instead, I have to use this to make it work:

printf("hello world");

Why does this happen? What is printf doing to the '\n'?


In most C compilers, including ours, the newline escape sequence '\n' yields an ASCII line feed character. The C escape sequence for a carriage return is '\r'.

When you create C programs in what the C Standard calls a "hosted implementation" (where the programs run under an operating system), the operating system or the stdio library routines are supposed to convert the newline character to whatever it takes to actually go to the beginning of the next line, e.g., carriage return + line feed.

For a "freestanding implementation" (where the programs run without any operating system), the Standard doesn't specify any requirements for library routines. It is up to the compiler vendor to decide what library routines to provide, and how each should work. All Keil compilers are freestanding implementations of C.

Users of our compilers send the output from printf() to a variety of devices with different requirements for marking the end of a line. So, any method we might choose to handle '\n' would be wrong in many cases.

On output, the default version of our library routine putchar() handles newline characters by outputting a carriage return and a line feed. Many projects require a user-written version of putchar(). If your project has a custom putchar() function, it may handle newline characters differently. For example, it may pass all characters through with no special-case treatment for '\n' (line feed).


There are three different ways to solve this problem:

  • Include a '\r' along with (or perhaps instead of) the '\n' in output strings. For example:
    printf("Hello world\r\n");
  • Modify the putchar() function such that when it is called to output a line feed, it outputs a carriage return in addition to the line feed.
  • Use a terminal that will treat a line feed character as a newline character (i.e., as a carriage return and line feed).


Last Reviewed: Wednesday, August 3, 2005

Did this article provide the answer you needed?
Not Sure
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.