Text Input/Output Functions in the C Language - dummies

Text Input/Output Functions in the C Language

By Dan Gookin

When you start dealing with variables in C, you’ll invariably stumble into the garden of I/O, or input/output. The computer’s primary input device is the keyboard, and its primary output device is the monitor, and you need to know how to get C to recognize input and create create output.

Here is a quick summary of the C language text input and output functions that you can use to help read information from the keyboard and push information to the screen.

C Language Text I/O Functions
Function Format Description
atof() [numvar = ]atof(string); Converts a floating-point value found in string into a
floating-point number, which can be stored in a variable, numvar, or used immediately. Requires the
STDLIB.H header file to be included.
atoi() [numvar = atoi](string); Converts an integer value found in string into an integer,
which can be stored in a variable, numvar, or used immediately. Requires the
STDLIB.H header file to be included.
fflush(stdin) fflush(stdin); Removes characters from the input stream (keyboard).
fpurge(stdin) fpurge(stdin); Removes characters from the input stream (keyboard). This
function must be used in Unix rather than fflush(stdin).
getchar() [ch = ]getchar(); Reads a single character from the keyboard. The character is
displayed and, optionally, stored in the char variable ch.
gets() gets(string); Reads a string of text from the keyboard (terminated by the
Enter key). The text is stored in the variable string.
printf() printf(“format”[,var[,var…]]); Displays formatted text according to the format string. Optional values or variables,
var, can be specified to match
placeholders or conversion characters in the format string.
putchar() putchar(ch); Displays the character ch on the
screen, where ch is a single character
(or escape code) in single quotes or the name of a char
variable.
puts() puts(string); Displays the text string on the
screen, where string is a literal string
of text (enclosed in double quotes) or the name of a string
variable.
scanf() scanf(“format”,&var); Reads information from the keyboard according to the conversion
character in the format string. The
information is then stored in the variable var, which must match the type of conversion
character that’s used (int, float, or char, for
example).