Data Input and Output in C, Part 1
The C language is accompanied by a collection of library functions which includes a number of input/output functions.These functions are used to permit the transfer of information between the computer and the standard input/output device. The basic input/output functions are getchar
, putchar
, puts
, scanf
and printf
. The first two functions, getchar
and putchar,
are used to transfer single characters. The next function puts
is used to output strings, and the last two functions, scanf
and printf
, permit the transfer of single characters, numerical values and strings.
An input/output function can be accessed from anywhere within a program simply by writing the function name, followed by a list of arguments enclosed in parentheses. The arguments represent data items that are sent to the function. Some input/output functions do not require arguments, though the empty parentheses must still appear.
The names of those functions that return data items may appear within expressions, as though each function reference were an ordinary variable (eg, c=getchar();
), or they may be referenced as separate statements (eg, scanf(...);
). Some functions do not return any data items. Such functions are referenced as though they were separate statements (eg, putchar(...);
).
C includes a collection of header files that provide necessary information in support of the various library functions. Each file generally contains information in support of a group of related library functions. These files are entered into the program via an #include
statement at the beginning of the program. As a rule, the header file required by the standard input/output library functions is called stdio.h.
Single Character Input: the getchar Function
The getchar
function is a part of the standard C input/output library. It returns a single character from a standard input device (typically a keyboard). The function does not require any arguments, though a pair of empty parentheses must follow the word getchar
.
In general, a function reference would be written as:
character variable = getchar( );
where character variable refers to some previously declared character variable.
If an end-of-file condition is encountered when reading a character with the getchar
function, the value of the symbolic constant EOF
will automatically be returned. (This value will be assigned within the stdio.h file. Typically, the EOF will be assigned the value -1). The detection of EOF
in this manner offers a convenient way to detect an end of file, whenever and wherever it may occur. Appropriate corrective action may then be taken.
The getchar
function can also be used to read multi-character strings by reading one character at a time within a multi-pass loop.
Single Character Output: the putchar Function
Single characters can be displayed using the C library function putchar
. This function is complementary to the character input function getchar
.
The putchar
function like getchar
is a part of the standard C input/output library. It transmits a single character to the standard output device (the computer screen). The character being transmitted will be represented as a character-type variable. It must be expressed as an argument to the function, enclosed in parentheses, following the word putchar
.
In general, a function reference would be written as:
putchar(character variable)
where character variable refers to some previously declared character variable.
A simple example demonstrating the use of getchar
and putchar
is given below:
#include<stdio.h>
int main()
{
char c;
printf("n Please enter a character:");
c=getchar();
printf("n The character entered is: ");
putchar(c);
return 0;
}
In the above program, the statement c=getchar();
accepts a character entered by the user and stores it in the variable c
. The character entered by the user can be anything from the C character set. The statement putchar(c);
prints the character stored in the variable c
.
The putchar
function can be used to output a string constant by storing the string within a one-dimensional character-type array. Each character can then be written separately within a loop. The most convenient way to do this is to utilize the for statement, which we will discuss in future.
Entering Input Data: the scanf Function
Input data can be entered from a standard input device by means of the C library function scanf
. This function can be used to enter any combination of numeric values, single characters and strings. The function returns the number of data items that have been entered successfully.
In general terms, the scanf
function is written as:
scanf(control string, arg1, arg2,.........,argN)
where control string refers to a string containing certain required formatting information, and arg1,arg2,….,argN are arguments that represent the individual data items. (Actually the arguments represent pointers that indicate the addresses of the data items within the computer’s memory. We will discuss pointers in greater detail in a future article, but until then it would be helpful to remember the fact that the arguments in the scanf
function actually represent the addresses of the data items being entered.)
The control string consists of the individual group of characters called format specifiers, with one character group for each input data item. Each character group must begin with a per cent sign (%) and be followed by a conversion character which indicates the type of the data item. Within the control string, multiple character groups can be contiguous, or they can be separated by whitespace characters (ie, white spaces, tabs or newline characters).
The most commonly used conversion characters are listed below:
Conversion Character Data type of input data c character d decimal integer e floating point value f floating point value g floating point value h short integer i decimal, hexadecimal or octal integer o octal integer x hexadecimal integer s string u unsigned decimal integer [. . .] string which may include whitespace characters
The arguments to a scanf
function are written as variables or arrays whose types match the corresponding character groups in the control string. Each variable name must be preceded by an ampersand (&). However, character array names do not begin with an ampersand. The actual values of the arguments must correspond to the arguments in the scanf
function in number, type and order.
If two or more data items are entered, they must be separated by whitespace characters. The data items may continue onto two or more lines, since the newline character is considered to be a whitespace character and can therefore separate consecutive data items.
Example 1:
#include<stdio.h>
int main()
{
char a[20];
int i;
float b;
printf(" n Enter the value of a, i and b");
scanf("%s %d %f", a, &i, &b);
return 0;
}
In the above program, within the scanf
function, the control string is "%s %d %f"
. It denotes three-character groups or format specifiers. The first format specifier %s
denotes that the first argument a
represents a string (character array), the second format specifier %d
denotes that the second argument i
is an integer and the third format specifier %f
denotes that the third argument b
is a floating point number.
Also note that the only argument not preceded by an ampersand (&) is a
since a
denotes a character array.
The s-type conversion character applies to a string that is terminated by a whitespace character. Therefore a string that includes whitespace characters cannot be entered in this manner. To do so, there are two ways:
1. The s-type conversion character is replaced by a sequence of characters enclosed in square brackets, designated as [. . .]. Whitespace characters are also included in the string so that a string that contains whitespaces may be read.
When the program is executed, successive characters will continue to be read as long as each input character matches one of the characters enclosed in the square brackets. The order of the characters in the square brackets need not correspond to the order of the characters being entered. As soon as an input character is encountered that does not match one of the characters within the brackets, the scanf
function will stop reading any more characters and will terminate the string. A null character will then automatically be added to the end of the string.
Example:
#include<stdio.h>
int main()
{
char line[80];
scanf(" %[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]", line);
printf("%s", line);
return 0;
}
If the input data is:
READING A STRING WITH WHITE SPACES
then the entire data will be assigned to the array line
. However, if the input is:
Reading A String With white Spaces
then only the letters in uppercase (R, A, S, W, S) will be assigned to line, as all the characters in the control string are in uppercase.
2. To enter a string that includes whitespaces as well as uppercase and lowercase characters we can use the circumflex, ie (^), followed by a newline character within the brackets.
Example:
scanf("[^n]", line);
The circumflex causes the subsequent characters within the square brackets to be interpreted in the opposite manner. Thus, when the program is executed, characters will be read as long as a newline character is not encountered.
Reading Numbers: Specifying Field Width
The consecutive non-whitespace characters that define a data item collectively define a field. To limit the number of such characters for a data item, an unsigned integer indicating the field width precedes the conversion character. The input data may contain fewer characters than the specified field width. Extra characters will be ignored.
Example: If a
and b
are two integer variables and the following statement is being used to read their values:
scanf( "%3d %3d", &a, &b);
and if the input data is: 1 4
then a
will be assigned 1 and b
4.
If the input data is 123 456 then a=123
and b=456
.
If the input is 1234567, then a=123
and b=456
. 7 is ignored.
If the input is 123 4 56 (space inserted by a typing mistake), then a=123
and b=4
. This is because the space acts as a data item separator.
Assignment Suppression
The % sign is followed by an asterisk (*) to skip over a data item without assigning it to the designated variable or array.
Example:
scanf( "%s %*d %f", a, &i,&b);
If the input is: Alice 12 34.5 then a=Alice
and b= 34.5
. However, 12
will not be assigned to i
because of the asterisk which is interpreted as an assignment suppression character.
Prefixes
Certain conversion characters within the control string can be preceded by a prefix; for example, an l or L to indicate a long integer argument and an h to indicate a short integer argument. Therefore “%ld” will indicate a long integer.
The next article will discuss the functions printf
, puts
and some basic programming examples that illustrate the use of these input/output functions.