Input and Output in C


Input and Output in C

Input and Output in C : Reading the data from input devices and displaying the results in the screen, are the two main tasks of any program. To perform these tasks user friendly C has a number of input and output functions. It takes the data through the input functions and sends results obtained through the output functions. The input/output functions are the link between the user and the terminal.

One of the essential operations performed in a C language programs is to provide input values to the program and output the data produced by the program to a standard output device. We can assign values to variable through assignment statements such as x = 5 a = 0; and so on. Another method is to use the Input then scanf which can be used to read data from a key board. For outputting results we have used extensively the function printf which sends results out to a terminal. There exists several functions in ‘C’ language that can carry out input output operations. These functions are collectively known as standard Input/ Output Library. Each program that uses standard input/output function must contain the statement.

#include <stdio.h>

at the beginning.

Input And Output Functions

The number of input/output functions in C based on the data types. The I/O functions are classified into two types:

  1. Formatted Functions
  2. Unformatted Functions

Formatted Functions

The formatted input/output functions read and write all type of data values. The formatted functions return the values after execution. The return value is equal to the number of variable successfully read/written. Using this value the user can find out the errors occurring during reading or writing the data.

1. printf( ): The printf() prints all types of data values to the console. It requires conversion symbol and variables names to print the data.

Example:-    main()                                                                           {                                                                                     int x=2;                                                                         float y=2.2;                                                                 char z=’C’;                                                                     printf(“%d %f %c”,x,y,z);                                              }  

2. scanf( ):  The scanf() statement reads all types of data values. It is used for runtime assignment of variables.

The scanf() statements requires ‘&’ operator called address of operator. The address operator prints the memory location of the variables.

Example:  void main()                                                                   {                                                                                     int a;                                                                              clrscr();                                                                         printf(“Enter value of A:”);                                         scanf(“%c”,&a);                                                            }

The printf() and scanf() statements follow the combination of characters called as escape sequences.

Unformatted Functions

The unformatted input/output functions only work with the character data type. There is no need to convert the data. The unformatted functions also return values, but the return value of unformatted function is always the same.

  • getch(): This function read any alphanumeric characters from the standard input device. The character entered is not displayed by getch( ) function.
  •  getchar( ): This function reads character type data from the standard input. It reads one character at a time till the user presses the enter.
  • putchar( ): This function prints one character on the screen at a time which is ready by the standard input.
  • gets( ) and puts( ): gets() and puts() functions facilitate the transfer of strings between the computer and the standard input/output devices. Each of these functions accepts a single argument. The argument must be a data item that represents a string(an array of characters). In the case of gets(), the string will be entered from the keyboard, and will terminate with a newline character.

Single character input output

The basic operation done in input output is to read a characters from the standard input device such as the keyboard and to output or writing it to the output unit usually the screen. The getchar function can be used to read a character from the standard input device. The scanf can also be used to achieve the function. The getchar has the following form.

Variable name = getchar:

Variable name is a valid ‘C’ variable, that has been declared already and that possess the type char.


The putchar function which in analogus to getchar function can be used for writing characters one at a time to the output terminal. The general form is

putchar (variable name);

Where variable is a valid C type variable that has already been declared Ex:-

putchar ( );

Displays the value stored in variable C to the standard screen.                                          Program shows the use of getchar function in an interactive environment.

String input and output

The gets function relieves the string from standard input device while put S outputs the string to the standard output device. A strong is an array or set of characters.

The function gets accepts the name of the string as a parameter, and fills the string with characters that are input from the keyboard till newline character is encountered. (That is till we press the enter key). All the end function gets appends a null terminator as must be done to any string and returns.

The puts function displays the contents stored in its parameter on the standard screen. The standard form of the gets function is

gets (str)

Here “str” is a string variable.                              The standard form for the puts character is

puts (str)

Where str is a string variable.

Formatted Input For Scanf:

The formatted input refers to input data that has been arranged in a particular format. Input values are generally taken by using the scanf function. The scanf function has the general form.

Scanf (“control string”, arg1, arg2, arg3 ……….argn);

The format field is specified by the control string and the arguments                                              arg1, arg2, …………….argn specifies the address of location where address is to be stored.

The control string specifies the field format which includes format specifications and optional number specifying field width and the conversion character % and also blanks, tabs and newlines.

The Blanks tabs and newlines are ignored by compiler. The conversion character % is followed by the type of data that is to be assigned to variable of the assignment. The field width specifier is optional.

The general format for reading a integer number is

% x d

Here percent sign (%) denotes that a specifier for conversion follows and x is an integer number which specifies the width of the field of the number that is being read. The data type character d indicates that the number should be read in integer mode.

Example:                                                   scanf(“%3d %4d”, &sum1, &sum2);

If the values input are 175 and 1342 here value 175 is assigned to sum1 and 1342 to sum 2. Suppose the input data was follows 1342 and 175.

The number 134 will be assigned to sum1 and sum2 has the value 2 because of %3d the number 1342 will be cut to 134 and the remaining part is assigned to second variable sum2. If floating point numbers are assigned then the decimal or fractional part is skipped by the computer.

To read the long integer data type we can use conversion specifier % Id & % hd for short integer.

Input specifications for real number

Field specifications are not to be use while representing a real number therefore real numbers are specified in a straight forward manner using % f specifier.

The general format of specifying a real number input is

Scanf (%f”, &variable);

Example:                                                      Scanf(“%f %f % f”, &a, &b, &c);

With the input data

321.76, 4.321, 678 The values                         321.76 is assigned to a, 4.321 to b & 678 to C.

If the number input is a double data type then the format specifier should be % If instead of %f.

Input specifications for a character

Single character or strings can be input by using the character specifiers.                                      The general format is

% xc or %xs

Where C and S represents character and string respectively and x represents the field width. The address operator need not be specified while we input strings.

Example :                                                                    Scanf (“%C %15C”, &ch, nname):

Here suppose the input given is a, Robert then a is assigned to ch and name will be assigned to Robert.

Formatted Output for Printf:


The most simple output statement can be produced in C Language by using printf statement. It allows you to display information required to the user and also prints the variables we can also format the output and provide text labels. The simple statement such as

Printf (“Enter 2 numbers”);

Prompts the message enclosed in the quotation to be displayed.

A simple program to illustrate the use of printf statement :

                #include <stdio.h>                                                      main ( )                                                                        {                                                                                      printf (“Hello!”);                                                            printf (“Welcome to the world of Engineering!”);                                                                            }

Output :

Hello! Welcome to the world of Engineering.

Both the messages appear in the output as if a single statement. If you wish to print the second message to the beginning of next line, a new line character must be placed inside the quotation marks.

For Example :                                                     printf (“Hello!\n”);                                                      OR                                                                    printf(“\n Welcome to the world of Engineering”);

Conversion Strings and Specifiers:

The printf ( ) function is quite flexible. It allows a variable number of arguments, labels and sophisticated formatting of output. The general form of the printf ( ) function is


Printf (“conversion string”, variable list);

The conversion string includes all the text labels, escape character and conversion specifiers required for the desired output. The variable includes all the variable to be printed in order they are to be printed. There must be a conversion specifies after each variable.

Specifies Meaning                                                 %c – Print a character                                            %d – Print a Integer                                                  %i – Print a Integer                                                   %e – Print float value in exponential form            %f – Print float value                                                  %g – Print using %e or %f whichever is smaller      %o – Print actual value                                            %s – Print a string                                                     %x – Print a hexadecimal integer (Unsigned) using lower case a – F                                            %X – Print a hexadecimal integer (Unsigned) using upper case A – F                                              %a – Print a unsigned integer                                %p – Print a pointer value                                       %hx – hex short                                                        %lo – octal long                                                      %ld – long

Escape Sequence with their ASCII values
Escape Sequence UseASCII Values
\nNew line10
\fForm feed12
\’Single quote39
\\Backslash 92
\tHorizontal tab9
\rCarriage return13
\”Double quote34
\vVertical tab11
\?Question mark63

About the author

Pooja Rastogi

Leave a Comment