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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
This presentation includes custom animations.
To view the animations, you must view the presentation in Slide Show modeand 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.
printf
Christine S. WolfeOhio University Lancaster2008-Aug-01
This lesson describes the syntax and use of the printf library functions:printf()fprintf()sprintf()
Vocabulary:
To use printf, fprintf, or sprintf, you must #include <stdio.h>ClickTip
ASCIIconversion specifier file name extensionFILE pointerflagfprintf()placeholderprecision
3Christine S. WolfeOhio University Lancaster2008-Aug-01
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.
ClickTip
ClickTip
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.
sprintf sends the output to a string variable.
ClickTip
fprintf sends the output to a text file.
/* VARIABLE DECLARATIONS */char Message[81];
The target string must be long enough to hold the entire output + 1 byte for the null.
4Christine S. WolfeOhio University Lancaster2008-Aug-01
syntax diagrams for the printf cousins from Appendix B of the text
All 3 include, for each placeholder, an argument that specifies the value to be used in place of the placeholder.
All 3 include an output string that specifies what to output expressed as: literals + escape sequences + placeholders.
All 3 return the number of bytes (characters) written to output.
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.
ClickTip
int printf (const char *format [, argument, …]);
int fprintf (FILE *stream, const char *format [, argument, …]);
int sprintf (char *buffer, const char *format [, argument, …]);
5Christine S. WolfeOhio University Lancaster2008-Aug-01
The syntax of the cousins varies only in the required arguments.
sprintf has 2 required arguments. The first argument in sprintf identifies the destination string variable and the 2nd 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.
printf has 1 required argument. That required argument is the output string.
stdout is treated like a file pointer in C so the following 2 constructions produce the same result: printf("Hello"); fprintf(stdout, "Hello");
ClickTip
int printf (const char *format [, argument, …]);
int fprintf (FILE *stream, const char *format [, argument, …]);
int sprintf (char *buffer, const char *format [, argument, …]);
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. WolfeOhio University Lancaster2008-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”);
If assigned, the result must be stored in an int
It is OK to call a function without
assigning its value to a variable.
Christine S. WolfeOhio University Lancaster2008-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).
Christine S. WolfeOhio University Lancaster2008-Aug-01
printf("First name: ");
printf("Thank you. Please visit again.");
printf("Please enter a number"); Please enter a number
First name:
Thank you. Please visit again.
printf("Report Menu"); Report Menu
What is the result of each of the following statements? (Click each button to check your answer.)
9Christine S. WolfeOhio University Lancaster2008-Aug-01
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.
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.
ClickTip
19
The printf placeholders may include formatting options.
Required
type%
Optional
Christine S. WolfeOhio University Lancaster2008-Aug-01
In your textbook, see Table 12.2 "Placeholders for printf format strings"
The following link is also very helpful.Christine S. WolfeOhio University Lancaster2008-Aug-01
[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.
[width] 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.
[.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.
[hlL] Also referred to as the length option, this indicates a modification of the data type as short or long.