Managing Input-Output Operations
In any programming language, the interface forms a very important part. It deals with taking data from the user and displaying back the output. For this purpose, we require the input-output operations. Since this is an important concept for any programming language, I would like to take it in parts rather than as a long single article. So this article deals with the basics of input operations.
Reading, processing, and writing of data are the three essential functions of a computer program. Most programs take some data as input and display the processed data, often known as information or results, on a suitable medium.
Unlike other high-level languages , C does not have any built-in input/output statements as part of its syntax . All input / output operations are carried out through function calls such as printf and scanf. There exist several functions that have more or less become standard for input and output operations in C. These functions are collectively known as the standard I/O library.
Each program that uses a standard input/output function must contain the statement
#include
at the beginning. However, there might be exceptions. For example, this is not necessary for the functions printf and scanf which have been defined as part of the C language.
The file name stdio.h is an abbreviation for standard input -output header file. The instruction #include
Reading A Character
The simplest of all input/output operations is reading a character from the standard input unit (usually the keyboard) and writing it to the standard output unit (usually the screen). Reading a single character can be done by using the function getchar. The getchar takes the following form :
variable_name = getchar();
variable_name is a valid C name that has been declared as char type. When this statement is encountered , the computer waits until a key is pressed and then assigns this character as a value to getchar function . Since getchar is used on the right-hand side of an assignment statement , the character value of getchar is in turn assigned to the variable name on the left.
For example
char name;
name = getchar();
will assign the character 'H' to the variable name when we press the key H on the keyboard .Since getchar is a function ,it requires a set of parentheses as shown.
C supports testing of the character keyed in by the user by including the file ctype.h & the functions which can be used after including this file are
isalnum(c) Is c an alphanumeric character?
isalpha(c) Is c an alphabetic character?
isdigit(c) Is c a digit?
islower(c)Is c a lower case character?
isprint(c)Is c a printable character?
ispunct(c)Is c a punctuation mark?
isspace(c)Is c a white space character?
isupper(c) Is c a upper case character?
Formatted Input
Formatted input refers to an input data that has been arranged in a particular format . For example, consider the following data:
15.73 123 John
This line contains three pieces of data, arranged in a particular form. Such data has to be read conforming to the format of its appearance. For example, the first part of the data should be read into a variable float, the second into int, and the third part into char. This is possible in C using the 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 specify the address of locations where the data is stored . Control string and arguments are seperated by commas.
*** Field (or format ) specifications, consisting of the conversion character %, a data type character (or type specifier ), and an optional number, specifying the field width.
*** Blanks, tabs, or new lines are ignored.
Inputting Integer Numbers
The field specification for reading an integer number is:
% w d
The percent (%) indicates 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, indicates that the number to be read is in integer mode
Inputting Real Numbers
Unlike integers numbers , the field width of real numbers is not to be specified and therefore scanf reads real numbers using the 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
472.34 43.21E-1 678
will assign the value 472.34 to x, 4.321 to y, and 678.0 to z. The input field specifications may be separated by any arbitrary blank spaces.
Inputting Character Strings
We have already seen how a single character can be read from the terminal using the getchar function. The same can be achieved using the scanf function also. In addition, a scanf function can input strings containing more than one character strings:
%ws or %wc
The corresponding argument should be a pointer to a character array. However %c may be used to read a single character when the argument is a pointer to a char variable.
scanf Format Codes:
%c reads 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)
– Ravinder Singh
*ks*
No comments:
Post a Comment