Sunday, March 18, 2007

Input/Output in C

Character Input / Output

This is the lowest level of input and output. It provides very precise control, but is usually too fiddly to be useful. Most computers perform buffering of input and output.
This means that they'll not start reading any input until the return key is pressed, and they'll not print characters on the terminal until there is a whole line to be printed.

getchar

getchar returns the next character of keyboard input as an int.

If there is an error then EOF (end of file) is returned instead. It is therefore usual to compare this value against EOF before using it. If the return value is stored in a char, it will never be equal to EOF, so error conditions will not be handled correctly.

As an example, here is a program to count the number of characters read until an EOF is encountered. EOF can be generated by typing Control - d.

#include
main()
{
int ch, i = 0;
while((ch = getchar()) != EOF) i ++;
printf("%d\n", i);
}

putchar

putchar puts its character argument on the standard output (usually the screen).

The following example program converts any typed input into capital letters.
To do this it applies the function toupper from the character conversion library ctype.h to each character in turn. #include

#include
main()
{
int ch;
while((ch = getchar()) != EOF) putchar(toupper(ch));
}

This above code converts the typed letter into uppercase

------------------------------------------------------------------------------------------------------------------------------

Formatted Input / Output

printf

This offers more structured output than putchar. Its arguments are, in order; a control string, which controls what gets printed, followed by a list of values to be substituted for entries in the control string.

Control String Represents
%d A Decimal Integer
%f A Floating Point Value
%c A Character
%s A String

It is also possible to insert numbers into the control string to control field widths for values to be displayed. For example %6d would print a decimal value in a field 6 spaces wide, %8.2f would print a real value in a field 8 spaces wide with room to show 2 decimal places. Display is left justified by default, but can be right justified by putting a - before the format information, for example %-6d, a decimal integer right justified in a 6 space field.

scanf

scanf allows formatted reading of data from the keyboard.

Like printf it has a control string, followed by the list of items to be read. However scanf wants to know the address of the 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 string variables unmodified by a leading &.
Example,
scanf("%d",&job_no);
scanf("%c",&job_code);

In the above examples the first one gets an integer value and the second one gets a char value.

-----------------------------------------------------------------------------------------------------------------------------------

Line Input and Output

This kind of input method is used where we are not too interested in the format of our data, or perhaps we cannot predict its format in advance, we can read and write whole lines as character strings.

This approach allows us to read in a line of input, and then use various string handling functions to analyse it at our leisure.

gets

gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the string is large enough to hold any expected input lines.When all input is finished, NULL as defined in stdio.h is returned.

puts

puts writes a string to the output, and follows it with a newline character.
Example:
Program which uses gets and puts to double space typed input.

#include
main()
{ char line[256];
while(gets(line) != NULL)
{ puts(line);
printf("\n");
}
}

No comments: