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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

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 …

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 …