Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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, and the perimeter is %d.\n”, area, pm); The printf function is called in order to display program output. Inside the parenthesis are the function’s arguments. The arguments of printf include a format string (given in double quotes) followed by a print list. If the variable area has a value 24 and the variable pm has a value 20, above function call displays the line: The area is 24, and the perimeter is 20.

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 the value of the corresponding item in the print list should be substituted at that point. The character following the % tells the computer what kind of value to expect and how to display it.

4 printf Function Placeholders 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 placeholder. 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 Function Displaying 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 place the cursor immediately following the string displayed, waiting for the user to enter data.

6 printf Function Using 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 here

7 printf Function Formatted 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 Function Formatted Numeric Output
Value Format Printed Output 200 %d 200 200 %5d 200 -3 %5d -3 562 %2d 562 (Note: Field too small) %10.3f  %7.1f 976.3 .342 %5.2f 0.34 99.99 %5.1f 100.0 -.002 %.3f %.3f 97.86 %10.2E 9.79E+01 %.1e -4.3e-02 Note: In scientific notation, four places of field width are taken for the exponent.

9 scanf(“%d%d”, &length, &width);
scanf Function scanf(“%d%d”, &length, &width); The first argument to scanf is the format string (in double 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 (in the memory) into which it 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 is 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) you’ll get an error.

11 scanf Function Recognizing 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 Function Recognizing Data
char a; int n, m; double x, y; input line B 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.


Download ppt "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."

Similar presentations


Ads by Google