C – Input output(I/O): printf, scanf, getchar & putchar2 min read

Input output(I/O)

Input means providing or inserting some data that is to be used in a program.
Output means to display the data on the screen or write it in a file.

There are many C built-in function that are used to take in data and display the data from a program as a result.

printf() and scanf() functions

printf() and scanf() are a C built in function whose definition are present in the standard input-output header file, named stdio.h.

printf() takes the values given to it and display it on a screen.
scanf() is used to take in user input and store it in some variable to use it in a program.

Example to show the use of printf() and scanf() in a C program:

The output of printf() and scanf() in C.

The following table shows the use of %d, %f, etc in a above program.

Format StringMeaning
%dScan or print an integer as signed decimal number
%fScan or print a floating point number
%cscan or print a character
%sscan or print a character string

getchar() & putchar() functions

getchar() is used to read a character from the terminals(keyboard) and reads only a single character at a time.
putchar() is used to write a character on the screen. This is mainly used in File handling in C.

Both the function only displays one character at a time so if the user needs to display it more than one time then it needs to be used with loops.

Example to show the use of getchar() & putchar() in a C program:

The output of getchar() & putchar() in C.

MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More