Formatted Input and Output in C

The C language comes with standard functions printf() and scanf() so that a programmer can perform formatted output and input in a program. The formatted functions basically present or accept the available data (input) in a specific format.

The formatted functions accept various format specification strings along with a list of variables in the form of parameters. This format specification string refers to a character string used for specifying the data type of every variable present as the output or input along with the width and size of the I/O.

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

Table of Contents

Why Do We Use Formatted Input and Output in C?

We use the formatted input and output functions in the C language for taking single or multiple inputs from the programmer/user at the console. These functions also allow a programmer to display single or multiple values, in the form of output, to the users present in the console.

Let us take a look at some of the important types of input and output functions used in the C language.

Functions Used Description of the Function
printf() We use this function for displaying a single or multiple values in the form of output for the user end at the console.
scanf() We use this function for reading a single or multiple values in the form of input from the user end at the console.
sprintf() We use this function for reading the values that are stored in various variables, and for storing these values into the array of characters.
sscanf() We use this function for reading the characters available in the string, and then storing them into the available variables.

scanf()

We use the scanf() function for getting the formatted inputs or standard inputs so that the printf() function can provide the program with numerous options of conversion.

Syntax for scanf()

scanf (format_specifier, &data_a, &data_b,……); // Here, & refers to the address operator

The purpose of the scanf() function is to read the characters that we get from the standard input, convert them according to the string of format specification, and then store the available inputs in the memory slots that the other arguments represent.

Example of scanf()

scanf(“%d %c”, &info_a,&info_b);

When we consider the string data names in a program, the ‘&’ character does not prefix the data name.

printf()

We use the printf() function for generating the formatted outputs or standard outputs in accordance with a format specification. The output data and the format specification string act as the parameters of the function printf().

Syntax for printf()

printf (format_specifiers, info_a, info_b,…….. );

Example of printf()

printf(“%d %c”, info_a, info_b);

The character that we specify after the ‘%’ character refers to the conversion character. It is because ‘%’ allows the conversion of any given data type into another data type for further printing.

Conversion Character Description and Meaning of Character
c The program takes the data in the form of characters.
d The program performs the conversion of the data into integers (decimals).
f The program generates data output in the form of a double or a float with the default of a Precision 6.
s The program identifies the data as a string, and a single character from this string gets printed unless and until the program reaches a NULL character.

Note that the format specification string is capable of containing texts as well, in the program.

printf (“%d\n%c”, data1, info_2);

printf (“Number: %d\n”, info_1);

Symbol of Specifier Meaning of Specifier
/t Used by a program for tab space (it is equivalent to a total of 8 spaces)
/n Used by a program for a new line (it is also known as a linefeed return)

Program Example

Let us take a look at an example of a program.

#include <stdio.h>

void main()

{

int x;

printf(“Please enter a valid score: ”);

scanf(“%d”,&x);

printf(“The entered score is= %d”,x);

}

The Output obtained here should be:

The entered score is = x

Format Specifier

The format specifiers specify the data for input/output in a program.Let us take a look at some of the format specifiers that are most commonly used in the formatted functions of input and output in the table below:

Type of Format Specifier Meaning and Description
%hu These are the format specifiers that we use for displaying an unsigned form of short integer (unsigned short int) value.
%hi These are the format specifiers that we use for displaying the signed form of a short integer (signed short int) value.
%u These are the format specifiers that we use for displaying or reading the unsigned form of integer (unsigned int) values.
%d These are the format specifiers that we use for displaying or reading the signed form of an integer (signed int) value.
%lu These are the format specifiers that we use for displaying the unsigned form of a long integer (unsigned long int) value.
%ld These are the format specifiers that we use for displaying the integer values or the signed form of the long integer (signed long int) value.
%c These are the format specifiers that we use for reading or displaying a character value char. We use the %hhi format specifier for displaying the signed numerical values.
%c These are the format specifiers that we use for reading or displaying a character value unsigned char. We use the %hhu format specifier for displaying the signed numerical value.
%lf These are the format specifiers that we use for reading and displaying the floating-point value of the double.
%f These are the format specifiers that we use for reading and displaying the floating-point value of the float.
%s These are the format specifiers that we use for reading or displaying the string value that will be stored in the array of characters.
%lf These are the format specifiers that we use for displaying a floating-point value of a long double.

Optional Specifiers Used within the Format Specifiers

One can specify two more types of optional specifiers in a program within every format specifier, for instance, a sign and an integer value.

  • The integer values are capable of specifying the total number of columns that are used on the screen while printing any value. In other words, it specifies the width. The integer value may or may not consist of a negative sign before it.
  • The negative/minus (-) sign that is present before an integer value would refer to the left justification of the value that will be printed on the screen. On the other hand, the integer value that follows the minus/negative sign refers to the total number of blanks that are present on the right.
  • None of the minus signs present before the available integer would refer to the right justification of this value that we want to print on the screen. Also, the integer values won’t specify the total number of blanks that are present on the left side of it.

Some More Examples of Programs

Example 1

#include <stdio.h>

int main()

{

// Displaying of the string inside these quotations

printf(“We did not win today”);

return 0;

}

The Output generated here would be:

We did not win today

Example 2

#include <stdio.h>

int main()

{

float value1 = 67.945;

double value2 = 81.23;

printf(“The value1 is = %f\n”, value1);

printf(“The value2 is = %lf”, value2);

return 0;

}

The Output generated here would be:

The value1 is 67.945000

The value2 is 81.230000

Example 3

#include <stdio.h>

int main()

{

float value1;

double value2;

printf(“Please enter your preferred value here: “);

scanf(“%f”, &value1);

printf(“Please enter another value here: “);

scanf(“%lf”, &value2);

printf(“value1 = %f\n”, value1);

printf(“value2 = %lf”, value2);

return 0;

}

The Output generated here would be:

Please enter your preferred value here: 39.712

Please enter another value here: 84.56

value1 = 39.712000

value2 = 84.560000

Practice Problems on Formatted Input and Output in C

1. What would be the output obtained out of the program mentioned below if we enter g for char?

#include <stdio.h>

int main()

{

char chr;

printf(“Please enter a suitable character here: “);

scanf(“%c”,&chr);

printf(“The character you entered is %c.”, chr);

return 0;

}

Answer – Please enter a suitable character here: g

The character you entered is g

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

#include <stdio.h>

int main()

{

printf(“I need some coffee”);

return 0;

}

Answer – I need some coffee

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

#include <stdio.h>

int main()

{

float mark1 = 94.678;

double mark2 = 91.32;

printf(“Niko scored = %f\n”, mark1);

printf(“Cassie scored = %lf”, mark2);

return 0;

}

Answer – Niko scored 94.678000

Cassie scored 91.320000

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

#include <stdio.h>

int main()

{

float age1;

double age2;

printf(“Please enter your age here: “);

scanf(“%f”, &age1);

printf(“Please enter your child’s age here: “);

scanf(“%lf”, &age2);

printf(“age1 = %f\n”, age1);

printf(“age2 = %lf”, age2);

return 0;

}

If input: point1 = 28 and point2 = 2,

Then output:

Answer – Please enter your age here: 28

Please enter a different age here: 2


Frequently Asked Questions

Why do we use Formatted Input and Output in C?

We use the formatted input and output functions in the C language for taking a single or multiple inputs from the programmer/user at the console. These functions also allow a programmer to display a single or multiple values, in the form of output, to the users present in the console.

What is the difference between scanf() and printf() functions in the C language?

We use the scanf() function for getting the formatted inputs or standard inputs so that the printf() function can provide the program with numerous options of conversion.
We use the printf() function for generating the formatted outputs or standard outputs in accordance with a format specification. The output data and the format specification string act as the parameters of the function printf().

Why are the formatted functions known as the formatted functions in the C language?

When someone tries to call the formatted console functions for input and output, they must make use of the format specifiers. These allow them to display or read any value which is of a primitive data type.

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.

*

*