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

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More