The Standard Input Output File in C

Standard Input Output File:

UNIX supplies a standard package for performing input and output to files or the terminal. This contains most of the functions which will be introduced in this section, along with definitions of the datatypes required to use them. To use these facilities, your program must include these definitions by adding the line This is done by adding the line

#include <stdio.h>

near the start of the program file.
If you do not do this, the compiler may complain about undefined functions or datatypes.

Character Input / Output

This is the lowest level of input and output. It provides very precise control, but is usually too fiddly to be useful. Most computers perform buffering of input and output. This means that they’ll not start reading any input until the return key is pressed, and they’ll not print characters on the terminal until there is a whole line to be printed.


getchar returns the next character of keyboard input as an int. If there is an
error then EOF (end of file) is returned instead. It is therefore usual to
compare this value against EOF before using it. If the return value is stored in
a char, it will never be equal to EOF, so error conditions will not be handled

As an example, here is a program to count the number of characters read until an
EOF is encountered. EOF can be generated by typing Control – d.

#include <stdio.h>

int ch, i = 0;
while((ch = getchar()) != EOF)
i ++;
printf("%d\n", i);


putchar puts its character argument on the standard output (usually the

The following example program converts any typed input into capital letters.
To do this it applies the function toupper from the character conversion library
ctype.h to each character in turn.

#include <ctype.h> /* For definition of toupper */
#include <stdio.h> /* For definition of getchar, putchar, EOF */
{ int ch;
while((ch = getchar()) != EOF)

Formatted Input / Output:

They are closest to the facilities offered by Pascal or Fortran, and usually the easiest to use for input and output. The versions offered under C are a little more detailed, offering precise control of layout.

1)printf: This offers more structured output than putchar. Its arguments are, in order; a control string, which controls what gets printed, followed by a list of values to be substituted for entries in the control string.

%d, A Decimal Integer
%f, A Float Point Value
%c, A Character
%s, A String

There are several more types available. For full details type
man printf
on your UNIX system.

It is also possible to insert numbers into the control string to control field widths for values to be displayed. For example %6d would print a decimal value in a field 6 spaces wide, %8.2f would print a real value in a field 8 spaces wide with room to show 2 decimal places. Display is left justified by default, but can be right justified by putting a – before the format information, for example %-6d, a decimal integer right justified in a 6 space field.


scanf allows formatted reading of data from the keyboard. Like printf it has a control string, followed by the list of items to be read. However scanf wants to know the address of the items to be read, since it is a function which will change that value. Therefore the names of variables are preceeded by the & sign. Character strings are an exception to this. Since a string is already a character pointer, we give the names of string variables unmodified by a leading &.

Control string entries which match values to be read are preceeded by the percentage sign in a similar way to their printf equivalents.

Type man scanf for details of all options on your system.


Whole Lines of Input and Output


Where we are not too interested in the format of our data, or perhaps we cannot predict its format in advance, we can read and write whole lines as character strings. This approach allows us to read in a line of input, and then use various string handling functions to analyse it at our leisure.


gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the string is large enough to hold any expected input lines.
When all input is finished, NULL as defined in stdio.h is returned.


puts writes a string to the output, and follows it with a newline character.
Example: Program which uses gets and puts to double space typed input.

#include "stdio.h"
{ char line[256]; /* Define string sufficiently large to
store a line of input */
while(gets(line) != NULL) /* Read line */
{ puts(line); /* Print line */
printf("\n"); /* Print blank line */

Note that putchar, printf and puts can be freely used together. So can getchar, scanf and gets.

Please comment for any issue, comments or concerns.

Leave a Comment