C New Lines

1. Using \n for New Line

In C, the newline character \n is used to move the cursor to the next line when printing output.

Example:

#include <stdio.h>

int main() {
printf("Hello World\n");
printf("Welcome to C Programming\n");
return 0;
}

Output:

Hello World
Welcome to C Programming

Here, \n after "Hello World" moves the cursor to the next line.


2. New Lines in the Middle of Text

You can also add \n anywhere in the string:

printf("Line1\nLine2\nLine3\n");

Output:

Line1
Line2
Line3

3. Using Multiple New Lines

  • You can use \n multiple times to create blank lines:

printf("Hello\n\nWorld\n");

Output:

Hello

World

Notice the blank line between “Hello” and “World”.


4. Using \n vs puts()

  • puts() automatically adds a newline at the end, unlike printf().

#include <stdio.h>

int main() {
puts("Hello");
puts("World");
return 0;
}

Output:

Hello
World

No need for \n with puts().


5. Tips for New Lines

  1. Use \n in printf() for formatting output.

  2. Use multiple \n for spacing or blank lines.

  3. Use puts() for simple strings that require a newline automatically.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *