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."— Presentation transcript:
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
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.
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.
printf Function Placeholders in Format Strings Different conversion characters are used in placeholders for different variable types: %cchar %dint %fdouble (used with printf) %lfdouble (used with scanf) %edouble, to print in scientific notation %Edouble, 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.
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 leave the cursor immediately following the string displayed, waiting for the user to enter data.
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
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
printf Function Formatted Numeric Output ValueFormatPrinted Output 200%d200 200%5d200 -3%5d-3 562%2d562 (Note: Field too small) 976.29%10.3f976.290 976.29%7.1f976.3.342%5.2f0.34 99.99%5.1f100.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.
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.
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 or 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 or key again pressed. If wrong type of data (characters when numeric required) have an error.
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).
scanf Function Recognizing Data char a; int n, m; double x, y; input line 2562.9136B 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.