Input and Output in C

In this tutorial, we are going to look at different input and output functions that are used to take Input and present the desired Output to the user in C.

In layman terms:

INPUT – IN + PUT – PUT SOMETHING IN

OUTPUT – OUT + PUT – GET SOMETHING OUT

What comes in our mind when we think of the terms, “INPUT” and “OUTPUT”?

Think about it! You got it.

Introduction to input and output in C

Similarly, in C, the set of instructions provided by the user to the machine to perform a specific task is known as Input while the outcome that follows as a result of the process is known as Output.

The C programming language has many built-in features to take input and present it to the user.

The input and output function can be used in different ways:

  • Unformatted I/O
  • Formatted I/O

Firstly, we’ll discuss the unformatted I/O Functions and later we’ll talk about formatted I/O functions.

We’ll also be discussing the different header files associated with the input/output functions.

I/O functions in C

Printf() and scanf()

Printf() function is used to display the output of the required program while scanf() is used to get the user-input into the program.

Example: The given code illustrates the use of printf() and scanf() functions

#include <stdio.h>

int main() {
  int i; //defining variable
  printf("Let’s learn Input-Output functions.\n Enter a number:"); //displaying the message within                                                                       the quoted lines in the output
  scanf("%d", & i); //letting the machine read our input
  printf("Your declared number is: %d", i) //printing the output
  return 0;
}

There might be a lot of things going in your head right now; you might be wondering what these are: %d, &I, etc.

%d – It is known by the term “format specifier”, and this informs the scanf() function, what type of Input to anticipate from the user, and in printf() it is used to make the compiler know what kind of Output to expect.

Format Specifier Context
%d Scan or print an integer
%f Scan or print a number with decimals
%c To scan or print a character
%s To scan or print a character string. The scanning ends at whitespace.

&i – This is used to locate the address of the assigned variable integer, ‘i’. This is read as “address of i”. We will read further about this in the next few modules.

Getchar() and putchar()

Getchar() function reads a single character input from the user, regardless of what it is, and returns it to the program. It is specified in ANSI-C and is the most essential input function in C. It is included in the stdio. h header file.

Putchar() function is used to write a single character to the standard output stream, stdout.

Text Box example: The following code shows the usage of getchar() and putchar():

#include <stdio.h>

int main() {
  char ch;
  printf("\nType any alphabet:");
  ch = getchar(); //must be followed by enter key
  printf("You Typed:");
  putchar(ch);
  return 0;
}

Gets () and puts ()

Gets() accepts a string from the keyboard. Whenever an enter key is hit, the program gets terminated. Thus, spaces and tabs are absolutely acceptable as a part of the input string. gets() function receives a newline (\n) terminated string of characters as input and replaces the \n with a \0.

The puts() function works dot to dot opposite to the gets() function, it outputs a string to the screen.

Example of Gets() and puts() in C

#include <stdio.h>

int main() {
  char footballer[40];
  puts("Enter name");
  gets(footballer);
  puts("Happy Footballing!");
  puts(footballer);
  return 0;
}

Formatted I/O Functions in C

Input/output functions that are used to take one or multiple inputs from the person using it at the console and it also allows us to display one or multiple values in the output to the user at the console are known as Formatted I/O functions. 

  • Formatted Input – The function printf() is used for formatted output to standard output based on a format specification. The format specification string, along with the data to be output, are the parameters to the printf() function. 
  • Formatted Output  The function scanf() is used for formatted input from standard input and provides many of the conversion facilities of the function printf(). 
Functions  Description 
scanf()  Used to take in one or more inputs from the user at the console.
 
printf()  This function is used to display one or multiple values in the output to the user at the console.
 
sscanf()  This function is used to read the characters from a string and stores them in variables.
 
sprintf()  This function is used to read the values stored in different variables and store these values in a character array. 

 

Summary

  • There are two different types of I/O functions based on the console I/O: 
  1. Formatted I/O functions 
  2. Unformatted I/O functions 
  • We have come across three different ways in which Input can be taken in C language, and Output can be provided: 
  1.  printf() and scanf() 
  2. getchar() and putchar() 
  3. gets() and puts() 
  • Few of them have limitations, like getchar() and putchar(), and some of them save us from extra pieces of code, like gets() and puts(), while some of them play their own parts, such as printf() and scanf(). It is up to us how we use them in our program to increase efficiency. 
Back to: Learn C Programming > Overview of C

Leave a Reply

Your email address will not be published. Required fields are marked *