C_Formatted IO
Home Up ComputingConcepts Definitions Characters & Strings C_Formatted IO C_Structs_Enums_Files FunctionsAndMenus Arrays and Cmd Line Args Arrays & Structures Structs, Enums, Ptr Variables Ptrs. to External Files Miscellaneous Random Files

 

 C Programming

FORMATTED INPUT/OUTPUT

All input and output is dealt with in streams--sequences of characters organized into lines. Each line consists of 0 or more characters and ends with a newline character.
Normally the standard input stream is connected to the keyboard, and the standard output stream is connected to the screen.
Operating systems often allow the standard input and standard output streams to be redirected to other devices.
The printf format control string describes the formats in which the output values appear. This string consists of conversion specifiers, flags, field widths, precisions, and literal characters.
Integers are printed with the following conversion specifiers: d or i for optionally signed integers, o for unsigned integers in octal form, u for unsigned integers in decimal form, and x or X for unsigned integrs in hexadecimal form.  The modifier h or l is prefixed to the preceding conversion specifiers to indicate a short or long integer respectively.
Floating-point values are printed with the following conversion specifiers: or E for exponential notation, f for regular floating-point notation, and g or G for either e, E,  or f notatiion.
Precision for the g specifier indicates the maximum number of significant digits printed.
The c conversion specifier prints a character.
The s conversion specifier prints a string of characters ending in the null character.
The conversion specifier p displays a pointer address in an emplementation-defined manner (on many systems, hexadecimal notation is used.)
The conversion specifier n stores the number of characters already output in the current printf statement.  The corresponding argument is a pointer to an integer.
The conversion specification %% causes a literal % to be output.
If the field width is larger than the object being printed, the object is normally right-justified in the field.
Field widths can be used with all conversion specifiers.
Precision used with integer conversion specifiers indicates the minimum number of digits printed. If the value contains fewer digits than the precision specified, zeros are prefixed to the printed value until the number of digits is equivalent to the precision.
Precision used with floating-point conversion specifiers e, E, and f indicates the number of digits that appear after the decimal point.
Precision used with floating-point conversion specifiers g and indicates the number of significant digits to appear.
Precision used with the conversion specifier s indicates the number of characters to be printed.
The field width and the precision can be combined by placing the field width followed by the precision between the % and the conversion specifier.
Field width and precision can be specified through integer expressions in the argument list following the format control string. To do this, insert an * in place of the field width or precision. The matching argument in the argument list is eveluated and used in place of the asterisk. (The value of the argument can be negative for field width but must be positive for precision.
Precise input-formatting is accomplished with the scanf library function.
Integers are input with the conversion specifiers d and i for optionally signed integers, and o, u, x, or X  for unsigned integers. The modifiers h and l are placed before an integer conversion specifier to input a short or long integer respectively.
Floating point values are inpt with the conversion specifiers e, E, f, g,  or G. The modifiers l and L are placed before any of the floating point conversion specifiers to indicate that the input value is a double  or long-double  value respectively.
Characters are input with the conversion specifier c.
Strings are input with the conversion specifier s.
A scan set scans the characters in the input looking only for those characters that match characters contained in the scan set. When a character is matched, it is stored in a character array. The scan set stops inputting characters when a character not contained in the scan set is encountered.
Address values are input with the conversion specifier p.
Conversion specifier n stores the number of characters input previously in the current scanf. The corresponding argument is a pointer to int.
The conversion specifier %% matches a single character % character in the input.
The assignment suppression character is used to read data from the input stream and discard the data.
A field width is used in a scanf to read a specific number of characters from the input stream.
 

The next page will contain information about Structures, Unions, Bit Manipulations, Enumerations, and File Processing with the C language. There are many more subjects that could be covered, but due to lack of time and space they are not included in this web site.

If you have comments or suggestions, email me at MarliJordan@ispchannel.com

Back Home Up Next
this page last updated 9/6/00