Input and Output (I/O)

As we all know the three essential functions of a computer are reading, processing, and writing data. The majority of the programs take data as input, and then after processing the processed data is being displayed which is called information or result/output. so far we have seen two methods of providing data to variables. One method is to assign value to a variable through assignment operator and Another method is to use scanf() which read data from keyword. We have used both method in our earlier example

For outputting result we have used extensively the function printf which send result out to a terminal

Unlike other high-level languages, C does not have any built-in input/output features. A set of library functions provided by the operating system UNIX for input and output are borrowed and used by C language..

Compilers using other operating systems such as MS-DOS have also emulated the same idea. All operations are carried out through I/O Library. In this tutorial, we will discuss some common I/O functions that can be used on many machines without any change.

Reading Character In C

C Code Example

The simplest of all i/o operations is reading a character from the ‘standard input’ unit (From the keyboard) and writing it to the ‘standard output ‘unit(usually the screen). Reading a single character can be done by using this function getchar.

Syntax:-

var_name = getchar();

var_name is the valid C name that has been declared as Char type. When this statement encountered, the machine waits until a key is pressed then assigns this entered character as the value to the var_name.For example:-

Writing Character In C

Similar to getchar(), there is an analogous function putchar for writing character one at a time to the terminal.

Syntax:

putchar(var_name);

Formatted Input

Formatted input refers to input data that has been arranged in a particular format. For example, consider the following data:

This line contains three-piece of data, arranged in a particular form. such data has to read conforming to the format of its appearance. For example, the first part contains the data that should be read into a variable float, the second into an int, and the third part into char. This is only possible in C using scanf() function.

Syntax:

The control string consist of two type:-

  1. Fields (or format) specification, consisting of the conversion character % , a data type character(or type specifier), and an optional number, specifying the field width.
  2. Escape sequence character such as \n, \t, \b.

C Code Example

 

% Format Specifiers

The % specifiers that you can use in ANSI C are:
Usual variable type Display

  1. %c char single character
  2. %d (%i) int signed integer
  3. %e (%E) float or double exponential format
  4. %f float or double signed decimal
  5. %o int unsigned octal value
  6. %p pointer address stored in the pointer
  7. %s array of char sequence of characters
  8. %u int unsigned decimal
  9. %x (%X) int unsigned hex value

Formatted output

We have seen the use of printf functions for printing captions and numerical results. It is highly desirable that the output are produced in such a way that they are understandable and are in an easy-to-use form. It is, therefore, necessary for the programmer to give careful considered to the appearance and clarity of the output produced by the program.

The printf() statement provides certain features that can be effectively exploited to control the alignment and spacing of print-out on the terminal. The general form of printf()statement is:-

printf(“control string”,arguments);

The control string consists of three types:-

  1. The character that will be printed on the screen as they appear.
  2. Format specification that defines the output format for display of each time.
  3. Escape sequence character such as \n, \t, \b.

C Code Example