Input and Output Functions in C

Input and output functions are available in the C language to perform the most common tasks. In every C program, three basic functions take place – accepting of data as input, the processing of data, and the generation of output. The acceptance of data refers to input and the presentation of data refers to the output. The C program accepts input from the keyboard and displays output on the screen.

The C program performs input and output operations using different input and output functions. In this article, we will take a look at input and output functions in C according to the GATE Syllabus for CSE (Computer Science Engineering).

Table of Contents

Why Do We Use Input and Output Functions in C?

When a programmer says input, it would mean that they are feeding some data or info in the program. They can give this input from the command line or in the form of any file. The C programming language comes with a set of various built-in functions for reading the input and then feeding it to the available program as per our requirements.

On the other hand, when a programmer says output, they mean displaying some data and information on the printer, the screen, or any other file. The C programming language also comes with various built-in functions for generating the output of the available data on any screen & printer, and also for saving the output in the form of binary files or texts.

Types of Input and Output Functions in C

In C, input refers to providing it with some information or data to be utilised in the program. On the other hand, output refers to writing the data and information on any printer file and displaying it on the screen. A standard library is available in the C language for reading any input and generating the output to be displayed on the console. There are mainly two types of these functions available in the C language:

Formatted

The formatted functions basically present or accept the available data (input) in a specific format. The standard library in C contains various functions for the input-output operations. The scanf() and printf() out of these functions help a programmer format the functions in their desired format. The program can use these functions for reading any form of data, like a real number, an integer, a character, and many more.

Unformatted

The unformatted functions are not capable of controlling the format that is involved in writing and reading the available data. Thus, these functions constitute the most basic forms of output and input. The supply of input or the display of output isn’t allowed in the user format – thus, we call these functions unformatted functions for input and output.

The unformatted input-output functions further have two categories:

  • The character functions
  • The string functions

We use the character input functions for reading only a single character from the input device (the keyboard). On the other hand, we use the character output functions for writing just a single character on the output source (the screen). Here, the getchar(), getche(), and the getch() refer to the input functions of unformatted type, while the putchar() and putch() refer to the output functions of unformatted type.

Now, let’s come to the string output and input. In any programming language, the character array or string refers to the collection of various characters. Now, various types of input and output functions are present in C programming that can easily read and write these strings. The puts() and gets() are the most commonly used ones. Here, the gets() refers to the input function used for reading the string characters while the puts() refers to the output function used for writing the string characters (in unformatted forms).

The C Standard Files

All the C language programs treat all of the devices in the program in the form of files. Thus, every display device is also addressed the same way the program addresses a file. In such a case, when a program starts executing, the following three files get opened up automatically for providing the program with access to the device’s screen and computer.

Device File Pointer Standard File
Screen stdout Standard output
Keyboard stdin Standard input
Your screen stderr Standard error

The C language uses the file pointer as a means for accessing the available files for reading and writing them.

The Format Specifiers

We use the format specifiers when we are trying to print the values available with different data types with the use of printf() statement. As a matter of fact, we also use these format specifiers when taking the input from the users with the use of the scanf() function. It is because, here, we must let the program know what types of input it should be expecting from its user.

Take a look at what format specifiers are used by the functions in any program:

Format Specifier Datatype
%d, %i int
%c char
%lf double
%f float
%hd short int
%li long int
%u unsigned int
%lu unsigned long int
%lli long long int
%llu unsigned long long int
%Lf long double
%c signed char
%c unsigned char

Showing Output with Printf() Function

It is one of the most used functions in C. The printf() function basically gets defined in the header file stdio.h, and we use it for showing the standard output (the output available on the console).

The programmers use the printf() function for printing the value of the variable or any simple sentence (text). These variables can be of float, char, int, or the other data types.

Examples of printf()

Printing of a Sentence

#include <stdio.h>

int main() {

// with the use of printf()

printf(“Let us go home tonight”);

return 0;

}

The Output generated here would be:

Let us go home tonight

Printing of an Integer

Here, we use the format specifier %d or %i.

#include <stdio.h>

int main() {

int q = 50;

// with the use of printf()

printf(“The given value of q is: %d”, q);

return 0;

}

The Output generated here would be:

The given value of q is 50

Printing of a Character

Here, we use the format specifier %c.

#include <stdio.h>

int main() {

// with the use of printf()

char age = ’43’;

printf(“Today is Clara’s birthday and she is: %c”, age);

return 0;

}

The Output generated here would be:

Today is Clara’s birthday and she is 43

Printing of Double and Float

Here, we use the format specifier %f for the float value and the format specifier %lf for the double value.

#include <stdio.h>

int main() {

// with the use of printf()

double num_11 = 15556522.0978678;

float num_22 = 15.50;

printf(“The value of the num_11 is: %f \n”, num_11);

printf(“The value of num_22 is: %lf”, num_22);

return 0;

}

The Output generated here would be:

The value of the num_11 is 15556522.0978678

The value of the num_22 is 15.50

Printing of Multiple Outputs

#include <stdio.h>

int main() {

// with the use of printf() for multiple outputs in the program

int year = 1997;

int month = 08;

int day = 18;

printf(“Today’s date is: %d-%d-%d”, year, month, day);

return 0;

}

The Output generated here would be:

Today’s date is 1997-08-18

Taking Input with Scanf() Function

We make use of the scanf() function whenever we want the program to receive inputs from us. Thus, when the program receives input from the user, it stores those input values into any variable.

In short, we use the scanf() function to receive inputs of all datatypes from a user. The only thing that we must take care of is that those variables in which we store the input values have a similar data type.

Examples of scanf()

Taking Input of Integer Value

Here, we must perform the definition of the integer variable before using the scanf() function in the program.

#include <stdio.h>

int main() {

// with the use of scanf()

int input_from_user;

printf(“Hello, please enter a digit here: “);

scanf(“%d”, &input_from_user);

printf(“The digit that you entered is: %d”, input_from_user);

return 0;

}

The Output generated here would be:

Hello, please enter a digit here: 25

The digit that you entered is: 25

Notice that we have made use of the format specifier %d for informing the scanf() function here that the input received from the user will be of the integer type. We have also used the symbol ‘&’ here before the variable name because the &input_from_user would refer to the address of the input_from_user variable where any user would store their input values.

Taking Input of Float Value

#include <stdio.h>

int main() {

// with the use of scanf()

float input_from_user;

printf(“Hi, please enter a decimal value: “);

scanf(“%f”, &input_from_user);

printf(“The value that you have entered is: %f”, input_from_user);

return 0;

}

The Output generated here would be:

Hi, please enter a decimal value: 8.016

The value that you have entered is: 8.016

Here, note that we are making use of the format specifier %f and also defined the float variable type. If we try to do the same when taking the input in the form of a double data type, in this case, the %lf will be the format specifier.

Taking Input of Character Value

#include <stdio.h>

int main() {

// with the use of scanf()

char gender;

printf(“Hi, we would like you to enter your gender (F, M or O): “);

scanf(“%c”, &gender);

printf(“Thanks, your gender is: %c”, gender);

return 0;

}

The Output generated here would be:

Hi, we would like you to enter your gender (F, M or O): F

Thanks, your gender is: F

Taking Input of Multiple Values

#include <stdio.h>

int main() {

// with the use of scanf() for multiple numbers of inputs

int age;

char gender;

printf(“Please enter your gender and then age(F, M or O): “);

scanf(“%d %c”, &gender, &age);

printf(“The information that you entered is: %c and %d”, gender, age);

return 0;

}

The Output generated here would be:

Please enter your gender and then age(F, M or O): 24 M

The information that you entered is: 24 M

Extra Information about Input-Output Functions

The scanf() function assists in returning the total number of characters that it reads. On the other hand, the printf() function assists in returning the total number of characters that it prints.

int x = printf(“chocolate”);

printf(“Value of x is: %d”, x);

The Output generated here would be:

chocolateValue of x is: 9

In the program given above, the program printf(“chocolate”); would generate 9 as the output/ result. This will then be stored in the available variable x. It is because the word chocolate has a total of 9 characters. As a matter of fact, the first printf() statement here will also print the statement chocolate on the output/ result.

The Putchar() & Getchar() Functions

The putchar() function in a program displays all the characters that are passed to this function on the screen. After that, it returns the very same character. The putchar() function also displays one single character at any given time.

We can also utilise the putchar() method in any given loop if we are willing to display multiple characters.

The getchar() function in a program basically reads the characters available from the terminal. It then returns these characters in the form of integers. The getchar() function is capable of reading just a single character at any given time. A programmer can easily use this method in the case of a loop if they are willing to read more than a single character.

#include <stdio.h>

void main( )

{

int x;

printf(“Please enter a character here”);

/*

We are taking a character in the form of input and storing it in the variable x

*/

x = getchar();

/*

we are displaying the character stored in the variable x

*/

putchar(x);

}

The Output generated here would be:

Please enter a character here: Chocolate C

The compilation of the code mentioned above will ask the programmer to enter their desired value. Whenever we enter the available value, the program will generate an output displaying the value that we have entered in the program.

Practice Problems on Input and Output Functions in C

1. What would be the output obtained out of the program mentioned below:

#include <stdio.h>

int main() {

printf(“Do not sit on the chair”);

return 0;

}

Answer – Do not sit on the chair

2. What would be the output obtained out of the program mentioned below?

#include <stdio.h>

int main() {

int b = 38;

printf(“The value available for the variable b is: %d”, b);

return 0;

}

Answer – The value available for the variable b is 38

3. What would be the output obtained out of the program mentioned below?

#include <stdio.h>

int main() {

char marks = ’94’;

printf(“Dave passed his exams today and scored: %c”, marks);

return 0;

}

Answer – Dave passed his exams today and scored: 94

Your temperature in Fahrenheit is: 440.33

4. What would be the output obtained out of the program mentioned below?

#include <stdio.h>

int main() {

int year = 2021;

int month = 11;

int day = 07;

printf(“Today’s date is: %d-%d-%d”, year, month, day);

return 0;

}

Answer – Today’s date is: 2021-11-07


Frequently Asked Questions

Why do we use Input and Output Functions in C?

When a programmer says input, it would mean that they are feeding some data or info in the program. They can give this input from the command line or in the form of any file. The C programming language comes with a set of various built-in functions for reading the input and then feeding it to the available program as per our requirements.
On the other hand, when a programmer says output, they mean displaying some data and information on the printer, the screen, or any other file. The C programming language also comes with various built-in functions for generating the output of the available data on any screen & printer, and also for saving the output in the form of binary files or texts.

What is the difference between a formatted and unformatted function in C?

The formatted functions basically present or accept the available data (input) in a specific format. The standard library in C contains various functions for the input-output operations. The scanf() and printf() out of these functions help a programmer format the functions in their desired format. The program can use these functions for reading any form of data, like a real number, an integer, a character, and many more.
The unformatted functions are not capable of controlling the format that is involved in writing and reading the available data. Thus, these functions constitute the most basic forms of output and input. The supply of input or the display of output isn’t allowed in the user format – thus, we call these functions unformatted functions for input and output.
The unformatted input-output functions further have two categories:

  • The string functions
  • The character functions

Why do we use the format specifiers in the C language?

We use the format specifiers when we are trying to print the values available with different data types with the use of printf() statement. As a matter of fact, we also use these format specifiers when taking the input from the users with the use of the scanf() function. It is because, here, we must let the program know what types of input it should be expecting from its user.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Leave a Comment

Your Mobile number and Email id will not be published.

*

*