How to print multiple line messages using single printf in C language?

It's a very basic program, where we learn printing multiline messages through printf() statement using '\n' escape sequence.
Submitted by Manju Tomar, on September 10, 2017

Example:

    This is line 1.
    This is line 2.
    This is line 3.

Let's see how to print these lines using separate printf statements?

Consider the program:

#include <stdio.h>

int main()
{

    printf("This is line 1.");
    printf("This is line 2.");
    printf("This is line 3.");
   
    return 0;
}

Output

This is line 1.This is line 2.This is line 3.

Escape sequence (New Line - "\n")

"\n" is a new line character, which can be used anywhere within the message written in printf() statement.

Write first line "This is line 1." Then write "\n" again write "This is line 2." And then write "\n" and so on...

#include <stdio.h>

int main()
{

    printf("This is line 1.\nThis is line 2.\nThis is line 3.");
   
    return 0;
}

Output

This is line 1.
This is line 2.
This is line 3.

As I wrote above that this is a very basic program, and helpful for those who are starting to learn C program, there are other escape sequences too, you may learn from here:

C Language Tutorial »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.