Chapter 08 printf and scanf format indicators advanced

  • Slides: 24
Download presentation
Chapter 08 - printf and scanf • format indicators • advanced format control –

Chapter 08 - printf and scanf • format indicators • advanced format control – printf – scanf

printf revisited • general form: printf(“format string”, arg 0, arg 1, . . .

printf revisited • general form: printf(“format string”, arg 0, arg 1, . . . , argn); • “format string” contains literal text and format indicators (signalled by % sign). • formatters and arguments are matched from left. • printf does not automatically insert a new line. • Normally doesn’t display any data until newline is issued by program!

Common printf errors • use the wrong format indicator: – e. g. use a

Common printf errors • use the wrong format indicator: – e. g. use a %d with a float variable (what happens? ) • use the wrong number of formatters: – too few formatters - rightmost argument(s) will be ignored – too many formatters - garbage is printed in the place(s) given by the rightmost formatter(s)

Integer formatters %d %i %u %o %x %X signed decimal (not really necessary) unsigned

Integer formatters %d %i %u %o %x %X signed decimal (not really necessary) unsigned decimal unsigned octal unsigned hexadecimal (w/ lower case a-f) unsigned hexadecimal (w/ upper case A-F)

notes on integer formatters • By default, leading zeros are not printed • “

notes on integer formatters • By default, leading zeros are not printed • “ 0 x” is not printed for hexadecimal • chars and shorts are promoted to full ints if displayed via an integer formatter • width of field varies depending on size of integer • %ld, %lu, . . . for long ints – (the ‘l’ is a modifier, as we will see soon)

Real number formatters %f %e %E real number [-]mmm. dddddd (6 fraction digits) scientific

Real number formatters %f %e %E real number [-]mmm. dddddd (6 fraction digits) scientific form [-]m. dddddd e[+/-] xx scientific form [-]m. dddddd E[+/-] xx 7 digits total, with one before decimal point %g %G general purpose formatter (real or scientific) General purpose formatter (real or scientific) will use %f unless value <1. e-04 or >=1. e+07 in which case it uses %e for %g or %E for %G. %Lf, %Le, %Lg for long doubles

Other formatters %c %s %n single character string (print until � hit) prints total

Other formatters %c %s %n single character string (print until hit) prints total number of characters printed with printf to this point %p prints pointer/address (machine dependent) %% just prints a % sign

Advanced control of printf format • The general form of a formatter is: %[flag][width][.

Advanced control of printf format • The general form of a formatter is: %[flag][width][. precision][modifier]format indicator • All [fields] are optional • flag controls justification and other properties • Width gives total length of display field • precision gives # characters in precision field • modifier short, double, and long control

Format flags Flag Operation Default 0 leading zeros leading spaces Output is left justified

Format flags Flag Operation Default 0 leading zeros leading spaces Output is left justified Right justified + Always display sign only - sign <space> prints space or minus sign only - sign Note: + overrides <space> if both are given # adds leading 0 to octal format adds 0 x (0 X) to %x (%X) format forces decimal point in real formats (even if integer)

Field width specifier • Insert total number of characters: – printf(“%20 s”, st 1);

Field width specifier • Insert total number of characters: – printf(“%20 s”, st 1); /* yields a width of 20 */ • the field width is overridden if it is too small to accurately display variable. • adjustable field width is indicated by an * – you must give an argument in the list before the variable to give the (integer) length – printf(“%*s”, 20, st 1); /* equivalent to 1 st example */

. precision specification Indicated by a leading period: %. 5 f %f, %e indicates

. precision specification Indicated by a leading period: %. 5 f %f, %e indicates # of digits to right of. %g indicates total # digits in mantissa %d, . . . indicates minimum # digits printed %s indicates maximum # of characters displayed (the rest is truncated) • %c, %p no effect • • •

Precision comments • When used with field width: %12. 5 f – (field width

Precision comments • When used with field width: %12. 5 f – (field width is NOT necessary) • Variable precision – printf(“%. *f”, num_prec, num); – printf(“%*. *f, num_width, num_prec, num);

Program 8. 1 #include <stdio. h> #define PI 3. 141592654 f int main(void) {

Program 8. 1 #include <stdio. h> #define PI 3. 141592654 f int main(void) { /*demonstrates advanced formatting */ int i; char test[]="Hi World"; for (i=1; i<16; i++) { printf("%*s %+0*ftt", i, test, i, PI); printf("%. *s %+0. *fn", i, test, i, PI); } return 0; }

final printf comments • type modifiers –h short integer –l long integer –L long

final printf comments • type modifiers –h short integer –l long integer –L long double – h modifier exists with printf only for symmetry with scanf; short ints automatically promoted to int. • returned value – printf returns an int with the number of characters successfully printed or EOF (usually -1) for an error

scanf revisited scanf(“format string”, &arg 1, . . . , &argn); • argument addresses

scanf revisited scanf(“format string”, &arg 1, . . . , &argn); • argument addresses must be given • returns an int of the # of successful conversions made (NOT # characters) or EOF (usually -1) if there is an error • The burden of insuring correct input is up to user. • Initial white space ignored, but separates fields. • scanf stops as soon as data is read or error occurs. Any and all unread characters remain for next scan!

Advanced control of scanf format • many of the formatters are identical with printf

Advanced control of scanf format • many of the formatters are identical with printf formatters. We will only describe differences. – %i will recognize any integer format (dec, oct, hex) – %hd must be used to read short ints – %f is only for floats – %lf is for doubles /* common source of errors */

Effect of literal text in format • Except for white space, any literal text

Effect of literal text in format • Except for white space, any literal text in format string must be matched exactly by input source or scanf aborts: – scanf(“Radius = %lf ”, &radius); • Sometimes useful for disk file input, but there is typically a better way to ignore some input

The %* format indicator • Inserting an asterisk between % and any specifier tells

The %* format indicator • Inserting an asterisk between % and any specifier tells scanf to ignore that entry: – scanf(“%*s, %lf”, &radius); • is better than previous alternative

Matching character sets • one can replace %s with a text string that contains

Matching character sets • one can replace %s with a text string that contains a subset of all characters. scanf stops reading when any character outside that string is encountered. – scanf(“%[abcdefghijklmnopqrstuvwxyz]”, name); – reads only lower case letters – can be used to read spaces – can be abbreviated with hyphen – scanf(“%[a-z. A-Z 0 -9]”, name);

Excluding character sets • to exclude characters, use: – %[^char set] – To reject

Excluding character sets • to exclude characters, use: – %[^char set] – To reject lower case letters use: – scanf(“%[^a-z]”, name); – to accept all characters, use – scanf(“%[^n]”, name);

Field width specification • Field width, but not precision, can be specified with the

Field width specification • Field width, but not precision, can be specified with the same approach as printf: – scanf(“%4 d %6 f”, &i, &x); • This fixed format input is most useful with disk data.

Example 8. 2 #include <stdio. h> /****************** * Demonstration of scanf formatting * *

Example 8. 2 #include <stdio. h> /****************** * Demonstration of scanf formatting * * History : Date Reason * * 12/01/14 Created by W. Lawson * ******************/ int main(int argc, char *argv[]) { char name[80]; int test; printf("Input a string after each promptnn"); printf("First, an unformatted scanfnn"); scanf("%s", name); printf("Result: %snn", name); fflush(stdin);

Ex 8. 2 continued printf("Now, with: scanf(" %[^\n] ", name); nn"); scanf("%[^n]", name); printf("Result:

Ex 8. 2 continued printf("Now, with: scanf(" %[^\n] ", name); nn"); scanf("%[^n]", name); printf("Result: %snn", name); fflush(stdin); printf("Now, with: scanf(" %[^a-z] ", name); nn"); scanf("%[^a-z]", name); printf("Result: %snn", name); fflush(stdin); printf("Now, with: scanf(" %[a-z WL] ", name); nn"); scanf("%[a-z WL]", name); printf("Result: %snn", name); fflush(stdin);

Ex 8. 2 continued printf("Now, with: scanf(" %[a-z. WL] ", name); nn"); scanf("%[a-z. WL]",

Ex 8. 2 continued printf("Now, with: scanf(" %[a-z. WL] ", name); nn"); scanf("%[a-z. WL]", name); printf("Result: %snn", name); fflush(stdin); printf("Now, with: scanf(" %%*s, %%i "&test); nn"); scanf("%*s, %i", &test); printf("Result: %inn", test); fflush(stdin); } return 0;