Like many other programming languages, C programs read in (input) or write out (output) characters. Of note is that most any underlying computer (executing C code) also generally has a mechanism by which a single character may be input or output. This input or output of a single character will often be the only way for a program to accomplish I/O functionality.
The typical way that I/O is accomplished for a C program is to use a standard
(always provided) library of I/O functions. The example C program given used
the printf
function to print out a string (a bunch of characters).
To include the standard I/O library, the line
#include <stdio.h>
appears at the top of the C source code.
These notes are a guide to a very few of the standard I/O functions. Much more complete information is available in the Kernighan and Ritchie book.
stdin
and stdout
The C language (the Unix operating system, really) has a notion of input
coming from a place known as standard input.
It is written as stdin
.
By default, standard input comes from the keyboard.
And, output
goes to a place known as standard output.
It is written as stdout
.
By default, standard output goes to the screen.
getchar()
and putchar()
getc()
and putc()
The function getchar()
retrieves a single character
from standard input.
Its signature appears as
int getchar(void)
This function's return value will be one of
EOF
at the end of file
The function putchar()
sends a single character
to standard output.
Its signature appears as
int putchar(int)
The character to be output is passed as a parameter.
The return value of the function will be
EOF
if an error occurred
These two I/O functions are rather similar to the I/O available with our 354 MIPS simulator. It has a standard way to get a single character (from standard input) or output a single character (to standard output).
The functions
getc()
and putc()
are much the same.
The difference between these and
getchar()
and putchar()
are an extra parameter that identifies a file (through the use of
a file descriptor or file pointer) for input to come from,
or output to go to.
The structure FILE
defined in
<stdio.h>
gives access to file pointers and files.
These descriptions leave much to understand. Read Kernighan and Ritchie Chapter 7 for the details.
NOTE: The simulator used for 354 adds extensions to the assembly
language, such that there appears to be new MIPS assembly laguage
instructions that do input and output.
This makes it ever so much easier for the assembly language programmer
to write code that does I/O.
As the simulator (a program) was originally written in C,
these I/O pseudoinstructions mimic
getc()
and putc()
,
and even have the mnemonics
getc
and putc
.
scanf()
and printf()
printf()
is a function that "converts, formats, and prints
it arguments on the standard output. . .".
It is similar to Java's System.out.print
method.
It takes a variable number of arguments,
so its signature appears as
int printf(char *format, argument1, argument2, ... )
The integer returned by this function is the number of characters printed.
The first argument is a string, which describes the formatting of
what is printed.
This string contains 2 types of items:
characters to be copied onto the output (very much like in Java),
and specifications of placement and how the arguments are to be formatted.
Each of these specifications (which require a further argument within
the list of arguments),
is a set of characters that is identified by the percent character (%
).
The percent character may be followed by a modifier
(see the list on page 153, section 7.2 in Kernighan and Ritchie),
and is ended by what is known as a conversion character.
Examples are
d
to print the next argument within the argument list as
a decimal number.
c
to print the next argument within the argument list as
a character.
x
to print the next argument within the argument list as
a hexadecimal number.
s
to print the next argument within the argument list as
a null-terminated string.
x = 25;
y = 1;
printf("The value of x is %d, and the value of y is %d.\n", x, y);
The output (to standard output) will appear as
The value of x is 25, and the value of y is 1.
Further output will start at the beginning of the next line, due to
the newline character (\n
).
Here is a short example, to help with the spacing and number of characters that get printed when using a modifier. This not a program, as what the output looks like has been written in to the right of the line that would generate that particular output.
#include <stdio.h>
main()
{
int x; OUTPUT
x = 354;
printf("[%d]\n", x); [354]
printf("[%1d]\n", x); [354]
printf("[%2d]\n", x); [354]
printf("[%3d]\n", x); [354]
printf("[%4d]\n", x); [ 354]
printf("[%-4d]\n", x); [354 ]
}
scanf()
is similar to printf
, but it reads input.
The first argument will be a format string,
which specifies the expected form of the input.
Further arguments are pointers to variables.
According to the format string,
variable values are assigned as encountered.
They must be pointers.
Like printf
, it takes a variable number of arguments,
so its signature appears as
int scanf(char *format, addr of argument1, addr of argument2, ... )
scanf
returns the number of successfully
matched and assigned input items.
Here is a quick code fragment example:
if ( scanf("%d %d\n", &x, &y) != 2 ) {
printf("bad input\n");
}
If the input typed appeared as
-3 6
then in this code fragment, scanf
returns the value
2, and the value of variable x
will be -3, and
the value of variable y
will be 6.
Note: reread this explanation after looking at, and understanding the topic of pointers.
Again, these descriptions leave much to understand. Read Kernighan and Ritchie Chapter 7 (sections 7.2 and 7.4) for the details.
Copyright © Karen Miller, 2006,2007,2008 |