Site pages
Current course
Participants
General
Lesson-5 Managing Input and Output Operations
5.1 INTRODUCTION
In C language reading, writing and processing of data are three important functions. Most programs take some data as input and display the processed data, often known as information or result, on a suitable medium. So far we have seen two methods of providing data to the program variables. One method is to assign value to the variable through the assignment statement such as a=10 , x = 6; and so on. Another method is to use input function scanf which can read data from keyboard. For outputting the results we have used the function printf which sends the results to display device. Unlike other languages C does not have any built-in input/output statement as a part of syntax. All input and output operations are carried out through functions such as printf and scanf. There exist some functions that have become standards for input and out operations in C. These functions are collectively known as standard I/O library. In C language, any program that uses a standards input/output function must contain the statement #include<stdio.h> at the beginning. The file name stdio.h is abbreviation for standard input-output header file. The instruction #include<stdio.h> tells the compiler ‘ to search the file named stdio.h and place its content at this point in program’. The content of the header file become the part of the code when it is compiled.
5.2 Reading a character
The simplest of all input/output operations is reading a character from the standard input device (keyboard) and writing it to the standard output device (screen). Reading a single character can be done using the function getchar(). This can also be done with the help of the scanf function.
The getchar takes the following form:
variable_name = getchar();
variable_name is valid C name that has been declared as char type. When this statement is encountered, the computer will waits untill a key is pressed and then assigns the character as a value to getchar function. Since getchar is used on right hand side of assignment statement, the character value of getchar is in turn assigned to the variable on the left. For example:
char ch;
ch = getchar();
will assign “g” to the variable ch when we press the key g on the keyboard.
The getchar function may be called successively to read the characters in a line of text. For example, the following program segment reads characters from keyboard one after the another until the return key is pressed
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
char ch;
ch=’ ‘;
while(ch!= ‘\n’)
{
ch = getchar();
}
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
5.3 Writing a character
Like getchar, there is a function called putchar for writing a character one at a time to the screen, it takes the form as shown below:
putchar(variable_name)
where variable_name is a type char variable containing character. This statement display the character contained in variable_name at the screen. For example the statements:
ans = ‘y’;
putchar(ans);
will display the character Y on the screen. The statement putchar(‘\n’) would cause the curser on the screen to move to the beginning of the next line.
5.4 FORMATTED INPUT
Formatted input refers to an input data that has been arranged in a particular format. The formatted data can be read with the help of scanf function. The general form of scanf is
scanf(“control string”,arg1,arg2….argn);
The control string specifies the field format in which the data is to be entered and the arguments arg1,arg2...argn specifies the address of locations where the data are stored. Control strings and arguments are separated by commas. Control string contains field specification, which directs the interpretation of input data. It may include:
Field (or format) specifications, consisting of conversion character %, a data type character, and an optional number, specifying the field width.
Blanks, tabs, or newlines.
5.4.1 Inputting integer numbers
The field specification for reading an integer number is
%w d
The percentage sign (%) indicates that a conversion specification follows. w is an integer number that specifies the field width of the number to be read and d, known as data type character, the number to be read is in integer mode. Consider the following example:
scanf(“%2d %5d”,&num1, &num2);
Data line:
52 31456
The value 50 is assigned to num1 and 31456 to num2. Suppose the input data is as follows:
31456 52
The num1 will assign 31 (because %2d) and num2 will be assigned 456 (unread part of 31456). The value 50 that is unread will be assigned to the first variable in the next scanf call. This kind of error may be eliminated if we use the field specification without the field width. That is the statement:
scanf(“%d %d”,&num1, &num2);
will read the data
31456 52
correctly and assign 31456 to num1 and 52 to num2.
An input field may be skipped by specifying * in the place of field width. For example:
scanf(“%d %*d %d”,&a, &b);
will assign data
121 456 789
As follow:
121 to a
456 skipped (because of *)
789 to b
Program
/*Reading integer numbers*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a, ,b, c, x, y, z;
int p, q, r;
printf(“Enter three integer numbers \n”);
scanf(“%d %*d %d”,&a, &b, &c);
printf(“%d %d %d \n \n”,a, b, c);
printf(“Enter two 4-digit numbers \n”);
scanf(“%2d %4d ”,&x, &y);
printf(“%d %d \n \n”,x, y);
printf(“Enter two integer numbers \n”);
scanf(“%d %d”,&a, &x);
printf(“%d %d \n \n”,a, x);
printf(“Enter a nine digit numbers \n”);
scanf(“%3d %4d %3d”,&p, &q, &r);
printf(“%d %d %d \n \n”,p, q, r);
printf(“Enter two three digit numbers \n”);
scanf(“%d %d”,&x, &y);
printf(“%d %d \n \n”,x, y);
}
OUTPUT
Enter three integer numbers
1 2 3
1 3 –3577
Enter two 4-digit numbers
6789 4321
67 89
Enter two integer numbers
44 66
4321 44
Enter a nine digit numbers
123456789
66 1234 567
Enter two three digit numbers
123 456
89 123
5.4.2 Inputting Real Numbers
Unlike integer numbers, the field width of real numbers is not to be specified and therefore scanf reads real numbers using simple specification %f for both the notations, namely, decimal point notation and exponential notation. For example the statement:
scanf(“%f %f %f”, &x, &y, &z);
with the input data
435.25 42.31E-1 687
Will assign the value 435.25 to x, 4.231to y, and 687.0 to z.
If the number to be read is of double type, then the specification should be %lf instead of simple %f.
5.4.3Inputting Character Strings
Following are the specifications for reading character strings:
%ws or %wc
Some versions of scanf support the following conversion specifications for strings:
%[characters] and %[^characters]
The specification %[characters] means that only the characters specified within the brackets are permissible in the input string. If the input string contains any other character, the string will be terminated at the first encounter of such a character. The specification %[^characters] does exactly the reverse. That is, the characters specified after the circumflex (^) are not permitted in the input string.
5.4.4 Reading Mixed Data Types
It is possible to use one scanf statement to input a data line containing mixed mode
data. In such cases, it should be ensured that the input data items match the control
specifications in order and type.for example:
scanf(“%d %c %f %s”,&count, &code, &ratio, &name);
Table 5.1 will show some of the commonly used scanf formatted codes:
Table 5.1 commanly used Scanf Format Codes
Code |
Meaning |
%c |
Read a single character |
%d |
Read a decimal integer |
%e |
Read a floating point value |
%f |
Read a floating point value |
%g |
Read a floating point value |
%h |
Read a short integer |
%i |
Read a decimal, hexadecimal or octal integer |
%o |
Read an octal integer |
%s |
Read a string |
%u |
Read an unsigned decimal integer |
%x |
Read a hexadecimal integer |
%[..] |
Read a string of word(s) |
Points To Remember while using scanf
All function arguments, except the control string, must be pointers to variables (use &).
Format specifications contained in the control string should match the arguments in order.
Input data items must be separated by spaces and must match the variables receiving the input in the same order.
The reading will be terminated, when scanf encounters an ‘invalid mismatch’ of data or a character that is not valid for the value being read.
When searching for a value, scanf ignores line boundaries and simply looks for the next appropriate character.
Any unread data items in a line will be considered as a part of the data input line to the next scanf call.
When the field width specifier w is used, it should be large enough to contain the input data size.
5.5 FORMATTED OUTPUT
The printf statement provides certain features that can be effectively exploited to control the alignment and spacing of print-outs on the screen. The general form of printf statement is
printf(“control string”,arg1, arg2…argn);
Control string consists of three types of items:
Characters that will be printed on the screen as they appear.
Format specifications that define the output format for display of each item.
Escape sequence characters such as \n, \t and \b
The control string indicates how many argument follows and what their types are. The arguments arg1, arg2, …, argn are the variables whose values are formatted and printed according to the specification of the control string. The arguments should match in number, order and type with the format specifications.
A simple formate specification has the following form:
% w.p type-spcifier
where w is an integer number that specifies the total number of columns for the output value and p is another integer number that specifies number of digits to right of the decimal point for real number. Both w and p are optional.
5.5.1 Output of Integer Numbers
The format specification for printing an integer number is
%w d
where w specifies the minimum field width for the output. However, if the number is greater than the specified field width, it will be printed in full, overriding the minimum specification. d specifies the value to be printed is an integer. The number is right-justified in the given field width. Leading blanks will appear as necessary. The following example will output the number 6789 under different formats:
Format |
Output |
||||||
|
|
||||||
printf(“%d”,6789); |
|
||||||
printf(“%6d”,6789); |
|
||||||
printf(“%2d”,6789); |
|
||||||
printf(“%-6d”,6789); |
|
||||||
printf(“%06d”,6789); |
|
5.5.2 Output of Real Numbers
The output of real numbers may be displayed in decimal notation using the following format specification:
%w.p f
The integer w indicates the minimum number of positions that are to be used for the display of the value and the integer p indicates the number of digits to be displayed after the decimal point.
We can also display real numbers in exponential notation by using the specification
%w.p e
The following example will illustrate the output of the number y = 56.4567 under different format specification:
Format |
Output |
||||||||||||
|
|
||||||||||||
printf(“%7.4f”,y); |
|
||||||||||||
printf(“%7.2f”,y); |
|
||||||||||||
printf(“%-7.2f”,y); |
|
||||||||||||
printf(“%f”,y); |
|
||||||||||||
printf(“%10.2e”,y); |
|
||||||||||||
printf(“%11.4 e”,-y); |
|
||||||||||||
printf(“%-10.2e”,y); |
|
||||||||||||
printf(“%e”,y); |
|
5.5.3 Printing of Single Character
A single character can be displayed in a desired position using the format:
%wc
The character will be displayed right-justified in the field of w columns. We can make the display left-justified by placing a minus sign before the integer w. The default value for w is 1.
5.5.4 Printing of Strings
The format specification for outputting strings is of the form:
%w.ps
where w specifies the field width for the display and p indicates that only first p characters of the string are to be displayed. The display is right-justified.
5.5.5 Mixed Data Output
It is permitted to mix data types in one printf statements. For example the statement of the following type
printf(“%d %f %s %c”,a, b, c, d);
is valaid. As printf uses its control string to decide how many variables to be printed and what their types are. Therefore, the formate specification should match the variable in number, order and type.
Table 5.1 will show some of the commonly used scanf formatted codes:
Table 5.1 commanly used Scanf Format Codes
Code |
Meaning |
%c |
Print a single character |
%d |
Print a decimal integer |
%e |
Print a floating point value in exponent form |
%f |
Print a floating point value without exponent |
%g |
Print a floating point value either e-type or f-type |
%i |
Print a signed decimal integer |
%o |
Print an octal integer, without leading zero |
%s |
Print a string |
%u |
Print an unsigned decimal integer |
%x |
Print a hexadecimal integer |
The following letters may be used as prefix for certain conversion characters.
h for short integer
l for long integer or double
L for long double
Table 5.3 will show some of the commonly used output formatted flags:
Table 5.3 commanly used output Format flags
Flag |
Meaning |
- |
Output is left justified with in the field. Remaining field will be blank. |
+ |
+ or – will precede the sign numeric item. |
#(with o or x) |
Causes octal and hex items to be presented by O and Ox, respectib\vely. |
#(with e, f or g) |
Causes the decimal point to be presented in all floating point numbers, even if it is whole number. Also prevents the truncation of trailing zero in g-type conversion. |