1 / 21

This presentation includes custom animations.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode.

shiri
Télécharger la présentation

This presentation includes custom animations.

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. This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

  2. printf This lesson describes the syntax and use of the printf library functions: printf() fprintf() sprintf() Vocabulary: ASCII conversion specifier file name extension FILE pointer flag fprintf() placeholder precision printf() result sprintf() stdout text file unicode width Click Tip To use printf, fprintf, or sprintf, you must #include <stdio.h> Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  3. The printf functions are used to produce formatted output. printf sends the output to stdout. stdout is a predefined macro that represents the standard output and is typically the computer monitor although this can be changed by the end user. Click Tip fprintf sends the output to a text file. A text file is one in which every byte is a character from the ASCII, Unicode, or other character set. The extension can be anything the programmer chooses. Click Tip sprintf sends the output to a string variable. The target string must be long enough to hold the entire output + 1 byte for the null. Click Tip /* VARIABLE DECLARATIONS */ char Message[81]; Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  4. syntax diagrams for the printf cousins from Appendix B of the text • int printf (const char *format [, argument, …]); • int fprintf (FILE *stream, const char *format[, argument, …]); • int sprintf (char *buffer, const char *format [, argument, …]); All 3 return the number of bytes (characters) written to output. All 3 include an output string that specifies what to output expressed as: literals + escape sequences + placeholders. All 3 include, for each placeholder, an argument that specifies the value to be used in place of the placeholder. Although the square brackets indicate that the arguments at the end are optional, that is a bit misleading. They are required if there are any placeholders in the output string. There must be one argument (value) provided for each placeholder. Click Tip Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  5. The syntax of the cousins varies only in the required arguments. • int printf (const char *format [, argument, …]); • int fprintf (FILE *stream, const char *format[, argument, …]); • int sprintf (char *buffer, const char *format [, argument, …]); printf has 1 required argument. That required argument is the output string. fprintf has 2 required arguments. The first argument in fprintf identifies the file pointer for the destination file and the 2nd argument is the output string. sprintfhas 2 required arguments. The first argument in sprintfidentifies the destination string variable and the 2nd argument is the output string. Click Tip stdout is treated like a file pointer in C so the following 2 constructions produce the same result: printf("Hello"); fprintf(stdout, "Hello"); Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  6. int printf (const char *format [, argument, …]); • All 3 of the printf functions return a result with a data type of int. • The result is a count of the number of characters in the output. • Example: • NumLetters = printf(“Hello”); NumLetters == 5 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  7. It is not necessary to assign the result to a value – but if the result is assigned, then the variable on the left of the assignment operator MUST be an int. • Example: • int NumLetters; • printf(“Goodbye”); • NumLetters = printf(“Hello”); It is OK to call a function without assigning its value to a variable. If assigned, the result must be stored in an int Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  8. int printf (const char *format [, argument, …]); • const char *format means that you must include an output string that is a set of characters that are to be displayed on the screen along with escape sequences and conversion specifiers (placeholders). What is the result of each of the following statements? (Click each button to check your answer.) Please enter a number printf("Please enter a number"); printf("Thank you. Please visit again."); Thank you. Please visit again. printf("First name: "); First name: printf("Report Menu"); Report Menu Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  9. int printf (const char *format [, argument, …]); Escape sequences are used to express output that cannot be represented on the keyboard or to express output that might be mistaken for C code. • Examples of output that cannot be represented on the keyboard: • \r Carriage Return (move cursor to beginning of current line) • \f Form Feed (new page) • \a Audible Alert (bell) • Examples of output that might be mistaken for C code. • \n Newline • \t Horizontal Tab • \v Vertical Tab • \b Backspace • \\ Backslash • \? Question mark • \' Single quote • \" Double quote • %% Percent sign (honorary escape sequence) Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  10. What is the result of each of the following statements? (Click each button to check your answer.) abc printf("abc"); a b c printf("a\tb\tc"); a b c printf("a\nb\nc"); a b c printf("a\nb\tc"); syntax error!!! printf("a"b"); a"b printf("a\"b"); \c and \d are not defined escape sequences so the \ is ignored. printf("a\c\d"); acd printf("a\\c\\d"); a\c\d printf("a%%c\\d"); a%c\d Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  11. int printf (const char *format [, argument, …]); • Conversion specifiers are placeholders for values are not known at the time the code is written. % type Some common conversion specifiers: for data type use placeholder int %d float %f char %c string %s int (base 10) %i See the full list of conversion specifiers at: http://www.cplusplus.com/reference/clibrary/cstdio/printf.html Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  12. For each placeholder, there must be a value in the argument list. The data type of the value must match or be cast as the value indicated by the format specifier. The order in which the placeholders appear in the output string must exactly match the order of the values in the argument list. • printf(“The answer is %d”, 3); • printf(“She is %d years old.”, age); • printf(“%d + %d = %d”, numA, numB, numA + numB); 1 placeholder 1 value 1 placeholder 1 value 3 placeholder 3 values Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  13. What is the result of each of the following statements? (Click each button to check your answer.) Assume the following code appears before the statements below: int answer; int age; int numA; int numB; answer = 3; age = 22; numA = 14; numB = 12; The answer is 3 printf(“The answer is %d”, 3); She is 22 years old. printf("She is %d years old.", age); 14 + 12 = 26 printf("%d + %d = %d", numA, numB, numA + numB); 14 + 12 = 28 printf("%d + %d = %d", numA, numB, numA + numA); Understand why the computer displays a falsehood. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  14. What is the result of each of the following statements? (Click each button to check your answer.) Assume the following code appears before the statements below: double classAvg = 78.5; char AvgLetter = 'B'; printf(“The class average was %f”, classAvg); The class average was 78.500000 printf(“The average letter grade was %c”, AvgLetter); The average letter grade was B printf(“pi can be expressed as %c, %d, or %f”, PI, 3, 22/7.0); pi can be expressed as π, 3, or 3.142857 Click Tip PI is a predefined keyword in C. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  15. Notice that the arguments can be literals, variables, or any expression that returns a value with the appropriate data type. • printf(“%d %d %d”, 5, NumClients, 6 + 3); literal variable expression Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  16. As in all C code – you must sweat the details! • Sweat it that there is a comma between every pair of arguments - including the output string if it is followed by another argument. printf(“pi can be expressed as %c, %d, or %f” , PI, 3, 22/7.0); printf("Hello World"); printf(“The average letter grade was %c”, AvgLetter); Sweat it that the closing double quote is after the output string - NOT after the last argument! Sweat it that there is a semicolon at the end of the statement. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  17. Sweat it that printf does not automatically add spaces around the inserted arguments. The programmer must include the spaces in the output string. printf("She is%dyears old.", 23); She is23years old. printf("She is %d years old.", 23); She is 23 years old. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  18. Sweat it that the programmer must code every character that should appear on the screen, • … to get commas in a list of values, include the commas at the appropriate place inside the format string printf(“%d %d %d”, 5, NumClients, 6 + 3); 5 123 9 printf(“%d, %d, %d”, 5, NumClients, 6 + 3); 5, 123, 9 The DOS window uses a non-proportional font. Each character uses the exact same amount of horizontal space. Courier New is an example of a non-proportional font. Click Tip Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  19. The printf placeholders may include formatting options. Required [flags] [width] [.prec] [hlL] % % type type Optional Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  20. % [flags] [width] [.prec] [hlL] type [flags] The most common flag is the minus sign, -. It reverses the normal left/right alignment. Normal for text is left aligned, normal for numeric data is right aligned. The width specifies the MINIMUM number of characters to be displayed. If the value requires fewer characters, the width is padded with blank spaces. If the value requires more characters, all the characters are output using a greater width. The specified width is a MINIMUM. For numeric types, the width specifer includes space for the digits (both right and left of the decimal point) and the decimal point itself. [width] [.prec] The precision option specifies the exact number of digits to display to the right of the decimal point. These digits count as part of the width. Also referred to as the length option, this indicates a modification of the data type as short or long. [hlL] In your textbook, see Table 12.2 "Placeholders for printf format strings" The following link is also very helpful. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

  21. What is the result of each of the following statements? (Click each button to check your answer.) *** 12.3*** printf(“ ***%10.1f***”, 12.333); ***12.3330 *** printf(“ ***%-10.4f***”, 12.333); Average cost: 3.400 ! printf(“Average cost: %-8.3f !” , 3.4); ID Amount 1 12.15 2 1.60 3 4500.50 41234567890.01 5 16.37 printf(“%5s%10s\n”, "ID","Amount"); printf(“\n%5d%10.2f”, 1,12.15); printf(“\n%5d%10.2f”, 2,1.6); printf(“\n%5d%10.2f”, 3,4500.5); printf(“\n%5d%10.2f”, 4,1234567890.0123); printf(“\n%5d%10.2f”, 5,16.367); Make sure you understand why the row for ID 4 is out of line with the others. Send me an email telling me how you would correct the problem. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

More Related