1 / 12

Input/Output

Input/Output. Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output library. To access it: #include &lt;stdio.h&gt;. printf Function. printf(“The area is %d.<br>”, area);

candy
Download Presentation

Input/Output

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. Input/Output • Input/Output operations are performed using input/output functions • Common input/output functions are provided as part of C’s standard input/output library. To access it: • #include <stdio.h>

  2. printf Function • printf(“The area is %d.\n”, area); • The printf function is called in order to display program output. • Inside the parenthesis are the functions arguments. • The arguments of printf include a format string (given in quotes) followed by a print list. • If the variable area has a value 24, above function call displays the line: • The area is 24.

  3. printf Function format string • The first argument to printf is a format string. It must be enclosed in quotes(“ ”). Most of the format string is displayed literally on the output. • The format string may contain placeholders. A placeholder begins with the symbol % and indicates that a value from the print list should be substituted at that point. After the % is information telling the computer what kind of value to expect and how to display it.

  4. printf FunctionPlaceholders in Format Strings • Different conversion characters are used in placeholders for different variable types: • %c char • %d int • %f double (used with printf) • %lf double (used with scanf) • %e double, to print in scientific notation • %E double, to print in scientific notation • Format strings may contain multiple placeholders. There must be a corresponding value in the print list for each place holder. • printf(“The area is %d, and twice the area is %d.\n”, area, 2 * area); • result: • The area is 24, and twice the area is 48.

  5. printf FunctionDisplaying Prompts • Whenever input data is needed in an interactive program, the program should first display a prompt. This informs the user what data to enter, along with other information the user needs. • Usually a prompting message is simply text and requires no placeholders or print list. • Examples: • printf(“Please enter an integer value for length: ”); • printf(“Enter a code, a-c, or d to quit: ”); • Both of these will leave the cursor immediately following the string displayed, waiting for the user to enter data.

  6. printf FunctionUsing the newline character • Whenever format string contains the newline character (\n), the cursor is advanced to the beginning of the next line. Often the format string will end with the newline character so that subsequent output begins on the next line. However the newline may appear anywhere in the format string. • Example: • printf(“Here are some\nlines of output,\n\nincluding a blank line.\n”); • result: • Here are some • lines of output, • including a blank line. 1 blank line

  7. printf FunctionFormatted Numeric Output • Each placeholder (conversion specification) is introduced by the % and ends with a conversion character. Between them may be: • A digit string specifying minimum field width. The number will be printed in a field at least this wide, and longer if necessary. If longer than needed, number will be right justified and padded with blanks to the left. e.g. %5d • For double values, may additionally or instead have a period and a second digit string specifying the number of digits to be printed to the right of the decimal point. • e.g. %5.2f

  8. printf FunctionFormatted Numeric Output • ValueFormatPrinted Output • 200 %d 200 • 200 %5d 200 • -3 %5d -3 • 562 %2d 562 (Note: Field too small) • 976.29 %10.3f 976.290 • 976.29 %7.1f 976.3 • .342 %5.2f 0.34 • 99.99 %5.1f 100.0 • -.002 %.3f -0.002 • -3.14159 %.3f -3.142 • 97.86 %10.2E 9.79E+01 • -0.0432 %.1e -4.3e-02 • Note: In scientific notation, four places of field width are taken for the exponent.

  9. scanf Function • scanf(“%d%d”, &length, &width); • The first argument to scanf is the format string (in quotes), followed by an input list. Each int, double, or char variable in the input list is preceded by an ampersand (&). The & is the address-of operator. It tells C where to find the variable into which you will store values. • The placeholders in the format string must correspond to the order of the variables in the input list.

  10. scanf Function • The data required by scanf will come from the standard input device. Usually this is the keyboard. • When scanf executed, the program pauses until requested data is entered and the <return> or <enter> key pressed. • If too much data provided, the extra characters will be saved for the next call to scanf. • If not enough data provided, program will continue to pause until more data entered and <return> or <enter> key again pressed. • If wrong type of data (characters when numeric required) have an error.

  11. scanf FunctionRecognizing Data • %c only one input character is used, even if it is a blank. Upper and lower case letters are different. • %d program skips leading blanks, then read digits until it finds non-digit. Puts corresponding number in the int variable. • %lf program skips leading blanks, then looks for characters which can be converted to a double value. May have optional sign, a string of digits possibly with a decimal point, then an optional exponent field containing E or e followed by possibly signed integer. (conversion character e is a synonym for f).

  12. scanf FunctionRecognizing Data • char a; • int n, m; • double x, y; • input line • 25 62.91 36B • scanf(“%d%lf%d%c”,&n,&x,&m,&a); • n  25 • x  62.91 • m  36 • a  ‘B’ • scanf(“%lf%d%c%d%lf”, &x,&n,&a,&m,&y); • x  25.0 • n  62 • a  ‘.’ • m  91 • y  36.0 • Note: the character B is left for the next scanf request.

More Related