×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

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.