1 / 24

Chapter 08- printf and scanf

Chapter 08- printf and scanf. format indicators advanced format control printf scanf. printf revisited. general form: printf (“format string”,arg0,arg1,..., argn ); “format string” contains literal text and format indicators ( signalled by % sign).

luellac
Download Presentation

Chapter 08- printf and scanf

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 08- printf and scanf • format indicators • advanced format control • printf • scanf

  2. printf revisited • general form: printf(“format string”,arg0,arg1,...,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!

  3. 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)

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

  5. notes on integer formatters • By default, leading zeros are not printed • “0x” 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)

  6. Real number formatters %f real number [-]mmm.dddddd(6 fraction digits) %e scientific form [-]m.dddddd e[+/-] xx %E scientific form [-]m.dddddd E[+/-] xx 7 digits total, with one before decimal point %g general purpose formatter (real or scientific) %G 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

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

  8. 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

  9. 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 0x (0X) to %x (%X) format forces decimal point in real formats (even if integer)

  10. Field width specifier • Insert total number of characters: • printf(“%20s”,st1); /* 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,st1); /* equivalent to 1st example */

  11. .precision specification • Indicated by a leading period: %.5f • %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

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

  13. Program 8.1 #include <stdio.h> #define PI 3.141592654f int main(void) { /*demonstrates advanced formatting */ inti; char test[]="Hi World"; for (i=1;i<16;i++) { printf("%*s %+0*f\t\t",i,test,i,PI); printf("%.*s %+0.*f\n",i,test,i,PI); } return 0; }

  14. 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

  15. scanf revisited scanf(“format string”,&arg1,...,&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!

  16. 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 */

  17. 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

  18. 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

  19. 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-zA-Z 0-9]”,name);

  20. 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);

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

  22. Example 8.2 #include <stdio.h> /*********************************** * * * Demonstration of scanfformatting * * * * History : Date Reason * * 12/01/14 Created by W. Lawson * * * ***********************************/ int main(intargc, char *argv[]) { char name[80]; int test; printf("Input a string after each prompt\n\n"); printf("First, an unformatted scanf\n\n"); scanf("%s",name); printf("Result: %s\n\n",name); fflush(stdin);

  23. Ex 8.2 continued printf("Now, with: scanf(\" %[^\\n] \",name);\n\n"); scanf("%[^\n]",name); printf("Result: %s\n\n",name); fflush(stdin); printf("Now, with: scanf(\" %[^a-z] \",name);\n\n"); scanf("%[^a-z]",name); printf("Result: %s\n\n",name); fflush(stdin); printf("Now, with: scanf(\" %[a-z WL] \",name);\n\n"); scanf("%[a-z WL]",name); printf("Result: %s\n\n",name); fflush(stdin);

  24. Ex 8.2 continued printf("Now, with: scanf(\" %[a-zWL] \",name);\n\n"); scanf("%[a-zWL]",name); printf("Result: %s\n\n",name); fflush(stdin); printf("Now, with: scanf(\" %%*s, %%i \"&test);\n\n"); scanf("%*s, %i",&test); printf("Result: %i\n\n",test); fflush(stdin); return 0; }

More Related