Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.

Similar presentations


Presentation on theme: "CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004."— Presentation transcript:

1 CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004

2 Input/Ouput (i/o) in C C has extensive facilities for input and output (i/o) processing. unlike most programming languages the i/o facilities are not part of the core language but part of the standard library. The i/o facilities are character and line i/o formatted i/o (and string versions) formatted file i/o low-level file i/o First we focus on formatted i/o and the library functions printf() and scanf(). You will need to include the header file stdio.h.

3 printf() printf() (print formatted) generates output under the control of a format string (its first argument) which consists of literal characters to be printed and also special character sequences called format specifiers (conversions). Conversions request that other arguments be inserted into the string. Format printf(format-control-string, other-arguments); format-control-string describes output format other-arguments correspond to each conversion specification in format- control-string Each specification begins with a percent sign %.

4 Format Specifiers for printf() Here are format specifiers for printf(): %dprint an int argument in decimal %ldprint a long int argument in decimal %cprint a character %sprint a string %fprint a float or double argument %esame as %f, but use exponential notation %guse %e or %f, whichever is better %oprint an int argument in octal (base 8) %x or %Xprint an int argument in hexadecimal (base 16) %print a single %

5 Format Specifiers for printf() (2) %g prints with no trailing zeros (1.2300 becomes 1.23). %E and %G are similar to %e and %g respectively but they display a capital E when printing in exponential format Here are some other format specifiers that are part of the standatd but they are rarely used: %n corresponding argument must be of type *int, into which gets stored the number of characters read so far. Nothing is actually printed. %p corresponding argument must be of type *void, allows you to display a memory address (in hexadecimal format).

6 Field Width and Precision specify the width and precision of numbers as they are inserted Field width- size of field in which data is printed. (Minus sign uses one character position). If width larger than data, data is right-justified by default; if field width too small it increases to fit data. Width inserted between % and conversion specifier. Example: %4d. Floating point precision- number of digits to appear after decimal (e and f specifier). Format: A dot. followed by precision after the % character, e.g. %.3f specifies a precision of 3. Field width and precision can both be specified, %width.precision, e.g. %5.3f

7 Flags Flags supplement formatting capabilities Place flag immediately to the right of percent sign Here are two commonly used flag: - converted argument left-justified (default is right justification) 0zeros used instead of spaces to pad a field Example: int i = 123; printf("%05d %-5d", i, i) will display " 00123 123 "

8 scanf() Formatting input with scanf() function is used to read information from the keyboard. Format: scanf(format-control-string, other-arguments); format-control-string describes formats of inputs other-arguments - list of pointers to variables where input will be stored. The scanf() needs to know the address of the variables it is using. A scanf() does not display anything, the control string is only for the conversion character representing the variable you want to store.

9 Format Specifiers for scanf() Conversions (format specifiers) similar to printf(), notice though that double is different. %ccharacter %dinteger %ooctal integer %x or %X hexadecimal integer %ffloating point number %lfdouble floating point number %sstring of characters NOTE: Users must press enter key after they type their input.

10 scanf() continued. The control string may also contain white space - matches white space in input stream. Note if several consecutive whitespace characters appear in the control string, the effect is the same as if only one had appeared. ordinary characters (other than % or format specifier) - must match input stream exactly. Return values: printf() returns the number of characters printed. scanf() returns the number of things read, e.g.: count = scanf("%d%d",&x,&y);

11 The scanf() function is notoriously problematic and some C programmers don't use it at all, preferring character and line input (see later)!


Download ppt "CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004."

Similar presentations


Ads by Google