Data Input and Output in C, Part 1

Share this article

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.

Frequently Asked Questions (FAQs) about Data Input and Output in C

What is the significance of the printf() function in C programming?

The printf() function is a standard library function in C programming used for output. It sends formatted output to the standard output device, which is usually the screen. The function is included in the stdio.h header file. It allows you to print the value of one or more variables. The syntax for the printf() function is printf(“format string”, argument_list); where the format string can include format specifiers – special characters that format the output of the argument list.

How does the scanf() function work in C programming?

The scanf() function is used to read input from the standard input device, typically a keyboard. It is included in the stdio.h header file. The function reads the input according to the specified format. The syntax for the scanf() function is scanf(“format string”, argument_list);. The format string can include format specifiers, which define the type and the size of the input.

What are the different format specifiers in C?

Format specifiers in C are used in input and output functions like printf() and scanf() to determine the type of data that is to be inputted or outputted. Some common format specifiers include %d for integers, %c for characters, %f for floating point numbers, %s for strings, and %ld for long integers.

How can I read and write strings in C?

Strings in C are arrays of characters. To read a string, you can use the scanf() function with the %s format specifier. However, scanf() stops reading input after encountering the first whitespace. To read a line of text, you can use the fgets() function. To write a string, you can use the printf() function with the %s format specifier.

What is the difference between puts() and printf() in C?

Both puts() and printf() are used for output in C. The main difference between them is that puts() automatically adds a newline character at the end of the output, while printf() does not. Also, puts() only takes a string as an argument and cannot format the output, while printf() can take multiple arguments and format the output.

How can I handle errors in input and output operations in C?

C provides several functions to handle errors in input and output operations. For example, the ferror() function can be used to check for errors in file operations, and perror() can be used to print an error message to the standard error device.

How can I read and write data from and to files in C?

C provides several functions for file input and output. To open a file, you can use the fopen() function. To read from a file, you can use functions like fscanf() or fgets(). To write to a file, you can use functions like fprintf() or fputs(). Remember to close the file after you are done with it using the fclose() function.

What is the use of the getchar() and putchar() functions in C?

The getchar() function is used to read a single character from the standard input device. The putchar() function is used to write a single character to the standard output device. Both functions are included in the stdio.h header file.

How can I format output in C?

You can format output in C using the printf() function. The function allows you to specify the format of the output using format specifiers. For example, you can control the width of the output, the number of decimal places for floating point numbers, and the alignment of the output.

What is the difference between getc() and getchar() in C?

Both getc() and getchar() are used to read a single character from a file. The main difference between them is that getc() can read from any input stream, while getchar() reads from the standard input device.

Surabhi SaxenaSurabhi Saxena
View Author

My name is Surabhi Saxena, I am a graduate in computer applications. I have been a college topper, and have been awarded during college by the education minister for excellence in academics. I love programming and Linux. I have a blog on Linux at www.myblogonunix.wordpress.com.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week