Input/Output Functions in C with Examples

A set of program statements that are used to perform a certain task is called functions. Every C program starts execution from the main function. In C there are two types of functions: library function and user-defined function.

The main difference between these two functions is that the library function is already defined in the compiler but the user-defined function needs to be written by the programmer.

Library Function in C

The functions which are predefined for performing some specific task are called library function.

A library function is used by programmers in their program to simplify the programming task. Library functions are started on the header file of the programming language.

User Defined Function in C

The function which is used and written by programmer to their program is called user define function.

Input/Output function in C

Input refers to giving the software some data to work with, while output refers to displaying data on screen or writing data to a printer or disk.

The C programming language has several built-in functions for reading any given input and displaying data on the screen when the result is required.

scanf() and printf() functions

The functions printf() and scanf() are specified in the standard input-output header file stdio.h, and are used to view the output on screen and take input from the user, respectively.


  • scanf(“farmat speciar”,&list of variable);
  • printf(“format specier”,list of variable);

Let us consider the example below:

/* WAP to convert hours into minute & Seconds*/
void main()
	int h;
	printf("Enter the time in hours: ");
	printf("%d Hours = %d Minutes",h,h*60);
	printf("\n %d Hours = %d Seconds",h,h*60*60);

In the above example, %d is used in the scanf() and printf() functions. Which is called the format string, and it tells the scanf() function what kind of input to expect, and it’s used in printf() to tell the compiler what kind of output to expect.

There are 4 types of format strings in C as listed below:

  • %d = Scan or print an integer decimal number
  • %f = Scan or print a floating-point number
  • %c = To scan or print a character
  • %s = To scan or print a character string. The scanning will end only if whitespace is found

getchar() & putchar() functions

The getchar() function returns an integer representation of a character read from the program. Only a single character is read at a time.

The putchar() function returns the same character that was passed to it and shows it on the screen. This feature shows one character at a time.

Use the putchar() or getchar() method in a loop if you want to input/output more than one character.

gets() & puts() functions

The puts() function looks a lot like the printf() function. The puts() function is used to print the string that was previously read using the gets() or scanf() functions on the program.

The integer value returned by the puts() function represents the number of characters printed.

The gets() function allows the user to type a string of characters and then press the enter key. A character list is used to store all of the characters entered by the user. The user will enter space-separated strings using the gets() method. It returns the string that the user typed in.

Examples on Input/Output Functions

Write a C Program to find square root of any number

/* WAP to find square root of any number */
void main()
	int a;
	float r;
	printf("Enter any number: ");
	printf("The square root of %d is %f.",a,r);

Write a C Program to add the first & last digit of any 4 digit number

/* WAP to add the first & last digit of any 4 digit number */
void main()
	int n,a,b,sum=0;
	printf("Enter a four digit number: ");
	a=n/1000; 	 /* to seperate first digit */
	b=n%10;		/* to seperate the last digit */
	printf("The sum of first & last digit is %d",sum);

Write a C program to find the area & perimeter of circle

/* WAP to find the area & perimeter of circle*/
#define pi 3.14
void main()
	float r;
	printf("Enter the radius of the circle: ");
	printf("The area of the circle is %f ",pi*r*r);
	printf("\n and the perimeter is %f.",2*pi*r);

Print Friendly, PDF & Email

Leave a Comment

Hey, wait!Get Regular Update's

Subscribe Stay Connected, Keep Learning!