Example
Let's look at a better example of how to use new lines in a C program. Suppose you want to print a simple poem:
#include <stdio.h>
int main() {
printf("Roses are red,\n");
printf("Violets are blue,\n");
printf("Programming is fun,\n");
printf("And so are you!\n");
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this code, we use the printf function to display each line of the poem. By adding '\n' at the end of each string, we ensure that each line appears on a separate line in the output:
Roses are red,
Violets are blue,
Programming is fun,
And so are you!
Without the new line characters, the output would look like this:
Roses are red,Violets are blue,Programming is fun,And so are you!
As you can see, the new line characters make a big difference in how the output is formatted and presented.
What is \n exactly?
In C, '\n' is an escape sequence that represents a new line character. Escape sequences are special characters that are used to represent certain actions or characters that cannot be easily typed or displayed.
The backslash (\) is used to indicate the start of an escape sequence, followed by a specific character or combination of characters. In the case of '\n', the 'n' stands for "new line".
When the compiler encounters '\n' in a string or character literal, it interprets it as a special instruction to move the cursor to the beginning of the next line, rather than as the literal characters '\' and 'n'.
Other common escape sequences in C are:
- '\t' for a horizontal tab
- '\\' for a backslash
- '\"' for a double quote
- '\'' for a single quote
By using escape sequences like '\n', you can control the formatting and layout of your program's output, making it more readable and organized.
Frequently Asked Questions
What does the \n character do in C?
The \n character inserts a new line in the output, allowing you to start text on the next line.
Can \n be used in both printf and fprintf functions?
Yes, you can use \n in both printf() and fprintf() to add line breaks in console or file outputs.
Is there a difference between \n and other whitespace characters like \t?
Yes, while \n moves the cursor to a new line, \t inserts a tab, which spaces the output horizontally.
Conclusion
In this article, we discussed the newline character in C language. We've seen how it can be used to format output by starting a new line that makes the text easier to read and organize. Whether you need to show debugging messages or format data neatly, “\n” proves to be invaluable in presenting information clearly.
You can also check out our other blogs on Code360.