Input and Output in C Programming

In this post you will learn how you can do Input and Output in C programming. For this we will read about printf(), scanf(), getchar() and putchar() functions as well as we will also read about format specifiers and escape sequence.


In this post you will hear some such terms about which you have not read yet such as string, function. We will soon read about them in our upcoming tutorials.

The main work in any computer program is input, process and output. Most of the input in C programs is taken from the user.

We process the input given by the user in our program and then give the output to the user with a user friendly message.

Whenever you (programmer) make any program, always keep in mind that you are making this program for someone (user) else who does not know anything about your code and will not show your code to him.

Computer program is like communication between programmer and user, so your program should be user friendly only then your program is of any use otherwise it is useless.

To communicate with users, we (programmers) take the help of input and output functions in our programs.

For data input and output, the stdio.h (Standard Input Output) header file has been given in C language, in which many built-in input and output functions have been given.

To use all these input and output functions, you have to include the stdio.h header file at the top of your program, whose syntax I have given below.

Input and Output in C Programming.

#include <stdio.h>

You can also read: Constants in C Programming

In C programming, all input and output functions are divided into 2 categories.

1. Formatted Input and Output Functions

  • printf()
  • scanf()

2. Non-formatted Input and Output Functions

  • getchar()
  • putchar()
  • gets()
  • puts()


You will read the use of gets and puts functions in String post.

What are Format Specifiers in C Programming Language?

In C programming, format specifiers are used while taking input into variables and outputting variables.

Format specifiers allow the compiler to specify the format of the variables at the time of input and output. Some common data types and their format specifiers are given in the table below.

Data TypesFormat Specifiers
Data Types Format Specifiers

Format specifiers are used only in formatted input/output functions. Let us now learn the use of all formatted input/output functions one by one.

Input and Output in C Programming
Input and Output in C Programming

Output using printf() function

As I told you above that printf() and scanf() are built-in functions and they are defined in stdio.h header file so you have to include this header file to use these functions.

Whenever you want to print (output) any message, variables or both in your program on the user’s monitor (console) in a particular format, you can use printf() function for this.

Input and Output in C Programming

printf() Function Syntax and Example 1:

printf("You can write anything. It will print on output screen as it is.");
printf("Avinash Singh");

Explanation:Like in this example I have written my text message with the help of printf() function. Like in this example, I have printed my text message Avinash Singh with the help of printf() function, in the same way you can also print any text message.

printf() Function Syntax 2:

printf("<Format Specifiers><format-specifiers>", variables);
printf("<Format Specifiers</format-specifiers>><format-specifiers><format-specifiers>  Text Message", variables);</format-specifiers></format-specifiers>

printf() Function Example Program:

#include <stdio.h><stdio.h>
int main()
    int rollnumber=61;
    int stu_age=15;
    float stu_cgpa=9.6;
    char stu_gen = 'F';
    printf("%d\n", rollnumber);
    printf("Your Age : %d\n", stu_age);
    printf("Roll : %d, CGPA : %f and Gender : %c", rollnumber, stu_cgpa, stu_gen);
    return 0;


Your Age : 15
Roll : 61, CGPA : 9.6 and Gender : F


  • From line 4 to 7 we have declared variables of different data types.
  • In Line 9, we have printed only the variable roll.
  • In Line 10 and 11, we have printed the message (text) as well as the variable.
  • Whenever you print like this, then use the format specifier in the middle of the text where you have to print the value of your variable.

Input and Output in C Programming

Input using scanf() function

scanf() is the main input function in C programming. Whenever you want to take input from any kind of data user through keyword, then you can use scanf() function.

scanf() Function Syntax:

scanf("<format-specifiers><Format Specifiers>",&variables);</format-specifiers>

scanf() Function Example Program:

#include <stdio.h><stdio.h>
int main()
    int stu_roll;
    float stu_cgpa;
    char stu_gen;
    printf("Enter Your Roll No. : ");
    printf("Enter Your CGPA and Gender : ");
    scanf("%f %c",&stu_cgpa,&stu_gen);
    printf("Roll : %d, CGPA : %f and Gender : %c",stu_roll,stu_cgpa,stu_gen);
    return 0;


  • On line 9 we have taken input only in 1 variable, if you want to take input in 2 variables then you have to apply format specifier 2 times as we have done on line 11.
  • We have used scanf() function 2 times in our example and both times before that we have used printf() function but it is not necessary. We have done this only to make our program user friendly.

getchar() and putchar() character input output functions

If you want to input and output a single character from the user in your program in a very easy way, then for that you use getchar() and putchar() functions.

The getchar() function is used to take a single character input from the keyboard and the putchar() function is used to output a single character to the monitor.

As you know that putchar() is not-formatted output function, so you can make it simple to print single character and that too without any text message.

Input and Output in C Programming

getchar() function syntax:

char variable = getchar();

getchar() function example:

char ch;
ch= getchar();

putchar() function syntax:


putchar() function example:

char ch;
ch = getchar();

getchar() and putchar() function example program:

#include <stdio.h><stdio.h>
int main()
    char ch;
    printf("Enter Your Char : ");
    ch = getchar();
    printf("You Entered : ");
    return 0;

Escape Sequence in C Programming

Input and Output in C Programming

Escape sequences are used with the printf() function to format the output and are not shown on the output screen.

Escape sequences begin with a backslash ( \ ) and have 1 or 2 characters with a backslash, and their use and function are already fixed.

In the table below, I have given only useful escape sequences and have explained them all with the help of examples at the bottom of the table.

Escape SequenceDescription
\nCreate New Line
\tSet Horizontal Tab
\’Print Single Quote
\”Print Double Quote
\\Print Backslash
Escape Sequence

Without \n Escape Sequence:

printf("Avinash ");


Avinash Singh


We have written Hello and World in different printf() function but you will see in the same line in the output.

Line break has to be done in C Programming i.e. you have to use \n escape sequence. Whatever words are after \n will come in the new line.

Input and Output in C Programming

With \n Escape Sequence:




Without \t Escape Sequence:

printf("Blog    of      Avinash Singh");


Blog of Avinash Singh


We have written post in printf() function by giving more than 1 space between words but in the output all three words are printed with single space.

In C Programming, if you want to give more than one space between words, then you have to use \t escape sequence. Due to this, about 4-5 space (tab) comes in between the words.

Input and Output in C Programming

With \t Escape Sequence:

printf("Blog\tof\tAvinash Singh");


Blog     of     Avinash Singh

Print ” Without \” Escape Sequence:

printf("You are reading " blog of Avinash Singh."");



Print ” With \” Escape Sequence:

printf("You are reading\" blog of Avinash Singh.\"");


You are reading " blog of Avinash Singh."


Similarly, if you want to print single quotation mark and backslash in the output then you have to use \’ and \ escape sequences.

Input and Output in C Programming

I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at

You can also visit my Hindi YouTube Channel.

Leave a Comment