Sunday, September 28, 2025

CONCEPT OF \n

 #include <stdio.h>

int main() {

    printf("First line\n");

    printf("Second line\n");

    printf("Third line\n");

    return 0;

}

Output:


arduino

Copy code

First line

Second line

Third line

👉 Explanation:


"First line\n" → prints First line then moves to the next line.


"Second line\n" → prints Second line then moves to the next line.


"Third line\n" → prints Third line then moves to the next line.


So every time you put \n at the end, the cursor moves down one line after printing the text.

-----------------------------------------------------------------------------------------------------------------------------

📌 Example 1: \n at the end

#include <stdio.h> int main() { printf("Hello World\n"); printf("This is C programming\n"); return 0; }

Output:

Hello World This is C programming

👉 Here \n moves the cursor to the next line after the text.


📌 Example 2: \n at the beginning

#include <stdio.h> int main() { printf("\nHello World"); printf("\nThis is C programming"); return 0; }

Output:

Hello World This is C programming

👉 Because \n is written before the text, the cursor first jumps to a new line and then prints.


📌 Example 3: \n in the middle

#include <stdio.h> int main() { printf("Hello\nWorld\nin C\nProgramming"); return 0; }

Output:

Hello World in C Programming

👉 Here, \n splits the text into multiple lines wherever it appears in the middle.


Summary:

  • \n = moves to a new line.

  • At end → text prints then moves to next line.

  • At beginning → first moves to next line, then prints text.

  • In middle → splits the output into multiple lines.

No comments:

Post a Comment