Input Output C

  • Input output functions.
  • Conversion specifier.

Input and output functions

  • printf
  • scanf
  • getch
  • getchar
  • gets and puts

Library functions are available for transferring of information between computer and standard input and output devices. E.g. keyboard (I/p device) and monitor (o/p device). These are called console I/O functions.

The declaration of these function and related symbolic constants (more in preprocessor chapter) are available in header file stdio.h. Thus in order to use console I/O function in a program, we only need to access this file in the program. The following figure shows the classification.

Input and output functions
Formatted functions allow us to control where o/p would appear on screen, number of places after decimal point, spaces between two values etc.

This function is used to display output on screen. I.e. it moves data from computer memory to output device. The format of the function is

printf (“format string”, arg1, arg2…) ;

Format string contains the formatting information. Which consist of general characters which can be displayed as they are, conversion specifications, and escape sequences. arg1, arg2… are individual o/p data items. They can be constants, variables (of any data type), array names or even constant expressions.

E. g.

The printf function starts examining the format string from left to right. It displays the characters as they are encountered directly on the screen, till it comes across % or . When it comes across first conversion specifier it take the first argument and prints it in given format. When it comes across an escape sequence it takes the related action. It continues further in the same way.

Here printf prints “Average =” first. When %d is encountered value of avg is printed. causes cursor to move to the beginning of newline. Then “percentage = “is printed. When %f is encountered value of next argument i. e. per is printed and so on. Number of conversion specifiers should match the number of arguments.

Data can be entered into the computer from an input device (like keyboard) using the function scanf.

The format of the function is

scanf (“format string”, &arg1, &arg2 …) ;

It returns number of data items returned successfully.

The format strings consist of the conversion specifier. The arguments can be variables or array name and should represent address of variables, where the data item is to be stored. I.e. each variable must be preceded by an ampersand (&). However array names should not begin with an ampersand. For the time being it is sufficient to know that if we have a variable i, its address is &i. We will study the address concept later.

E . g.

The scanf function is opposite to the printf function. It reads input, interprets them using the conversion specifier and stores them in the given variable. The conversion specifier for scanf are same as that used for printf, with […] being an additional conversion specifier. Using the conversion specifier %s, we cannot input string with blank spaces. This can be achieved by using […]. How do we use it? If we say

scanf (“% [A B C ‘ ‘ D] “ line) ;

Then scanf will read characters from input as long as they match characters specified in square brackets and will terminate on a mismatch. The order of entering characters is not important.If we precede the characters in square brackets with a circumflex (^), it has the opposite effect. Scanf will read characters from input as long as no character from the square bracket is encountered. The scanf function requires an enter key to be pressed after supplying the input, in order to accept the input.

The following are the unformatted function used to input and output characters.

It is used to input a single character. It will instantly read the character and does not require enter key to be pressed. It returns the character typed but does not echo it on the screen.

ch is assigned the character returned by getch.

It is the counterpart of getch. i. e. it displays a single character on the screen. It return the character displayed

It is used to input a single character. It requires enter key to be passed following the character that you typed. It echoes the character that you entered.

ch = getchar ;

It is other side of getchar. It displays a single character on the screen.

putchar(ch) ;

The above functions are alternative to scanf and printf used for inputting and outputting characters. These character I/O function can be used to input and output string by reading or writing one character at a time inside a multipass loop. (We will study looping later).
There are functions available for inputting and outputting string directly. Strings are sequence of character that is terminated with ‘�’ character, which is called NULL character.

gets() and puts()
They facilitate transfer of string between computer and standard input-output devices. They accept single argument. Argument must be data item that represent a string. The string may include white space characters. When using gets, enter key has to be pressed to end the string. The gets and puts functions offer simple alternative to use of scanf and printf for reading and displaying string as illustrated.
E. g.

Conversion Specifiers
%d, %f, %c are called conversion specifiers. They tell how the arguments are to be displayed. That is avg to be displayed as an integer, per as float and grade as character. Field width specifiers can accompany conversion specifiers. Also whether the value is to be left justified or right justified can be mentioned. Following is the table, which gives all the conversion specifiers.

Conversion SpecifiersConversion Specifiers

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

C Topics