12
Handling Input & Output Ping Zhang 10/08/2010

Handling Input & Output

  • Upload
    ivan

  • View
    41

  • Download
    1

Embed Size (px)

DESCRIPTION

Handling Input & Output. Ping Zhang 10/08/2010. Simple Input & Output Operations. You can get data from the user (input) and display information to the user (output). However, you must include the library file that contains functions needed for input and output operations. - PowerPoint PPT Presentation

Citation preview

Page 1: Handling Input & Output

Handling Input & Output

Ping Zhang

10/08/2010

Page 2: Handling Input & Output

Simple Input & Output Operations

You can get data from the user (input) and display information to the user (output).

However, you must include the library file that contains functions needed for input and output operations. #include <stdio.h> Includes the necessary printf and scanf functions.

The printf function is used to output text to the screen.

The scanf function is used to get input from the user.

Page 3: Handling Input & Output

Output Operations

The printf function Syntax:

printf (format string); Example:

printf (“This is a line of text”);

The format string is enclosed by double quotes “” and may contain any literal text, placeholders, and format control characters.

Literal text is interpreted literally by the compiler and doesn’t cause the program to perform any action.

Format control characters cause the program to perform and action like moving to the next line.

i. Example:printf (“Move to the next line \n”);

Format control character to create a new line.

Page 4: Handling Input & Output

Output Operations

The printf function continued… The printf function can also take another form, which is used to display the

contents of one or more variables. Syntax:

printf (format string, output list);

The output list consists of a list of variables or values that will replace the corresponding placeholder in the format string.

A placeholder always begins with the symbol % and is used to mark the display position for a variable’s value.

Example:printf (“The distance traveled is %d”, kms);

printf (“The distance traveled is %d and cost of trip is %d”, kms, cost);

Placeholder character to insert the value of variable

kms.

Page 5: Handling Input & Output

Input Operations

The scanf function Syntax:

scanf (format string, input list); Example:

scanf (“%d”, &miles);

The format string is enclosed by double quotes “” and contains the data types of the input to read in. You need to use format specifiers to specify the data types.

The input list is a list of variables that will get values. The order of the variables in this list must correspond with the order of the format specifiers in the format string.

i. The first variable in the input list will get the value of the first format specifier in the format string, the second variable in the input list gets the value of the second format specifier in the format string, etc.

ii. Each variable must be declared before using them in the input list and you must put the ampersand (&) character in front of the variable’s name.

Page 6: Handling Input & Output

Format String Characters

Placeholders – Format Specifiers%c – placeholder used for a char in printf and scanf.%d – placeholder used for an int in printf and scanf.%f – placeholder used for a double in printf.%lf – placeholder used for a long double in printf and scanf.

Special Characters – Format Control Characters\n – used to create a newline.\t – used to add a tab space to the output line.\\ – used to display a backslash in the output.\” – used to display a double quote output.

Page 7: Handling Input & Output

Printf & Scanf Example

#include <stdio.h>

int main ( ) {

int quantity;double price;

printf (“Enter the quantity and price > “);scanf (“%d%lf”, &quantity, &price);

return (0);}

Page 8: Handling Input & Output

Printf & Scanf Example

#include <stdio.h>

int main ( ) {

char first;char middle;char last;

printf (“Enter the initials for your first, middle, and last names > “);scanf (“%c%c%c”, &first, &middle, &last);

printf(“Hello there %c%c%c and welcome to this program.”, first, middle, last);

return (0);}

Page 9: Handling Input & Output

Formatting Output

In C, the output of all numbers will be in their normal notation unless you specify the format.

Format specifiers can have modifiers to control the total number of columns and decimal places to display.

Syntax:field_width.decimal_places

Example:%5.2f%8.3f%5d

Page 10: Handling Input & Output

Examples: Formatting Output

Example 1:printf (“%6.2f”, 4.9653);will display in 6 columns with 2 decimal places and 1 decimal point as ##4.97

Example 2:printf (“%.1f”, 4.9653);will display to 1 decimal place and 1 decimal point as 5.0

Example 3:printf (“%5.1f”, 25.3);

will display in 5 columns with 1 decimal places and 1 decimal point as #25.3

Page 11: Handling Input & Output

Formatting Output

Formatting Values of Type int:printf (“ The average of all exams is %5d”, average);

Value Format Output Display

234 %5d 234

-234 %5d -234

-234 %6d -234

-234 %1d -234

Note: C will expand the field width if it is too small for the integer value.

*** From Table 2.11 on page 85 in our textbook.

Page 12: Handling Input & Output

Formatting Output

Formatting Values of Type double:printf (“ The average of all exams is %5.2f”, average);

Value Format Output Display

85.123474 %5.2f 85.12

2.5421 %4.2f 2.54

3.14159 %3.1f 3.1

3.14159 %5.1f 3.1

-.025 %8.3 -0.025

2. 71828183 %.4f 2.7182

*** See Table 2.13 on page 87 in our textbook.