← Back to C Course | Chapter 2: Input & Output | Lesson 2 of 7

C Newline in Output

The \n escape sequence moves output to a new line, whether used once inside a single printf call or repeated across several separate calls.
Syntax
c
printf("first line\nsecond line\n");

The \n Escape Sequence

The \n escape sequence represents a newline character, and placing it inside a string tells printf to move the cursor to the beginning of the next line at exactly that point in the output.

Example: The \n Escape Sequence

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Line one\nLine two");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multiple Lines in One printf

A single printf call isn't limited to one line -- multiple \n sequences can appear within the same string literal, each one starting a new line as the string is printed.

Example: Multiple Lines in One printf

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Line one\nLine two\nLine three");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Newlines Across Multiple printf Calls

Newlines don't have to come from a single printf call; several consecutive calls can build up multi-line output together, with each \n in any of them starting a fresh line.

Example: Newlines Across Multiple printf Calls

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Line one\n");
	// Print formatted text to the screen
	printf("Line two\n");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Newline vs No Newline

Without an explicit \n, consecutive printf calls print their text immediately next to each other on the same line, which is a common source of confusing output for beginners expecting automatic line breaks.

Example: Newline vs No Newline

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Hello");
	// Print formatted text to the screen
	printf("World");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Platform Differences in Line Endings

The C standard library handles the translation between \n and the operating system's actual line-ending convention automatically when writing to a text-mode stream, so the same \n works correctly across Linux, macOS, and Windows.

Example: Platform Differences in Line Endings

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("This works the same on Linux, macOS, and Windows\n");
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting \n at the end of output, so the shell prompt appears on the same line.
  2. Writing /n instead of \n, which prints those characters literally.
  3. Expecting output to appear immediately when stdout is buffered and no newline is printed.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.