DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 6 of 64
←PreviousPrevNextNext→

New Lines in C Output

Using \n for New Lines

In C, the escape sequence `\n` is used to insert a new line.

It moves the cursor to the beginning of the next line when printing text.

Example
#include <stdio.h>

int main(void) {
    printf("Line 1\n");
    printf("Line 2\n");
    return 0;
}
Output
Line 1
Line 2

Printing Multiple Lines in One printf

You can insert multiple `\n` characters in one `printf` to break lines.

This is useful for formatting output cleanly.

Example
#include <stdio.h>

int main(void) {
    printf("First Line\nSecond Line\nThird Line\n");
    return 0;
}
Output
First Line
Second Line
Third Line

Using puts for Convenience

`puts` prints a string and automatically appends a newline. It's handy when you just want to print a line of text.

Example
#include <stdio.h>

int main(void) {
    puts("Line 1");
    puts("Line 2");
    return 0;
}
Output
Line 1
Line 2
ℹ️ Note: `puts` adds the trailing newline for you; `printf` does not unless you include `\n`.

Buffering: Why Newlines Matter

`stdout` is often line-buffered when connected to a terminal. Printing a newline typically flushes the buffer so text appears immediately.

If you print without a newline and need immediate output, call `fflush(stdout);`.

Example
#include <stdio.h>

int main(void) {
    printf("Processing...");
    fflush(stdout); // ensure the text appears right away

    // ... do some work here ...

    printf(" done\n");
    return 0;
}
Output
Processing... done

Platform Note: Newline Translation

In C source, you always use `\n` for a newline. On some systems (e.g., Windows), the C library may translate `\n` to the platform's native line ending when writing in text mode.

You generally do not need to write `\r\n` yourself when using standard I/O; just use `\n`.

Test your knowledge: New Lines in C Output
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C BasicsTopic 6 of 64
←PreviousPrevNextNext→