Archive for 09. Input/Output Functions in C

I/O Functions in C

The standard Input Output File.

To perform,input and output to files or the terminal,UNIX supplies a standard package.To use them,this contains most of the functions which will be introduced in this section,along with definitions of the datatypes required.Your program must include these definitions by adding the line to use these facilities.
#include near start of the program file. If you do not do this, the compiler may complain about undefined datatypes or functions.

Character Input / Output
If you will not press the return key then they’ll not start reading any input, and they’ll not print characters on the terminal until there is a whole line to be printed. This is the lowest level of output and input. It provides very precise control, but is usually too fiddly to be useful also. Most computers perform buffering of inputs and outputs. These include :
1. getchar
2. putchar

getchar always returns the next character of keyboard input as an int. The EOF (end of file) is returned,if there is an error . It is usual to compare this value against EOF before using it. So error conditions will not be handled correctly,if the return value is stored in a char, it will never be equal to EOF.
The following program is used to count the number of characters read until an EOF is encountered. EOF can be generated by typing Control – d.

{ int ch, i = 0;

while((ch = getchar()) != EOF)
i ++;
printf(“%d\n”, i);

putchar puts its character argument on the standard output (usually the screen).
The following example program converts any typed input into capital letters.It applies the function toupper from the character conversion library ctype.h to each character in turn to do this.
#include /* For definition of toupper */
#include /* For definition of getchar, putchar, EOF */

{ int ch;
while((ch = getchar()) != EOF)

Formatted Input / Output
These includes following:
1. printf
2. scanf

This offers more structured output than the putchar. Its arguments are, in order; a control string, which controls what get printed, followed by a list of values to be substituted for entries in the control string. The prototype for the printf() is:
Control String Entry
What Gets Printed
Decimal Integer
Floating Point Value
Single Character
Character String
int printf(const char *format, …);
To print,printf takes in a formatting string and the actual variables . An example of printf is:
int x = 5;
char str[] = “abc”;
char c = ‘z’;
float pi = 3.14;

printf(“\t%d %s %f %s %c\n”, x, str, pi, “WOW”, c);

With the use of the formatting line,you can format the output.You can change how the particular variable is placed in output,by modifying the conversion specification, . For example
printf(“%10.4d”, x);
The . allows for the precision.To floats as well,this can be applied.The number 5 is on the tenth spacing as the number 10 puts 0005 over 10 spaces. You can also add – and + right after % to make the number explicitly output as +0005. Note that the value of x does not actually change. In other words,you will not get output using %-10.4d will not output -0005. %e is useful for outputting floats using the scientific notation. %le for doubles and %Le for the long doubles.

To grab things from input,scanf() is used. Beware though, scanf isn’t greatest function that C has to offer. Some people brush off the scanf as a broken function that shouldn’t be used often. The prototype for scanf is:
Last example will create a rectangle with rounded corner:
int scanf( const char *format, …);
To read of data from the keyboard,scanf allows formatted . Like printf it has a control string,followed by the list of items to be read. However scanf wants to know the address of items to be read, since it is a function which will change that value. Therefore the names of variables are preceeded by the & sign. Character strings are an exception to this. Since a string is already a character pointer, we give the names of the string variables unmodified by a leading &. Control string entries which match values to be read are preceeded by the percentage sign in a similar way to their printf equivalent. Looks similar to printf, but doesn’t completely behave like the printf does. Take the example:
scanf(“%d”, x);
For grabbing things from input. Beware though, scanf isn’t the greatest function that C has to offer,scanf() is used. The following is the example which shows the use of scanf:
int x, args;

for ( ; ; ) {
printf(“Enter an integer bub: “);
if (( args = scanf(“%d”, &x)) == 0) {
printf(“Error: not an integer\n”);
} else {
if (args == 1)
printf(“Read in %d\n”, x);

Leave a Comment


Get every new post delivered to your Inbox.