![Page 1: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/1.jpg)
The scanf Function
The scanf function reads input from the standard input device into one or more variables
Example: scanf(“%lf”, &miles);– Reads a real number that (by default) the user
has typed in into the variable miles
Each variable must be preceded by the ampersand (&) address-of operator
![Page 2: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/2.jpg)
The scanf Function
We can read values into multiple variables with a single scanf as long as we have correpsonding format strings for each variable
Example: scanf(“%c%c%c”, &letter_1, &letter_2, &letter_3);
The user must press the return or enter key to cause the value(s) to be read
![Page 3: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/3.jpg)
The scanf Function
For numeric values (associated with a %lf or %d placeholder) blank spaces are skipped
For character values (associated with a %c placeholder) blank spaces are not skipped (since a space is a character!)
![Page 4: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/4.jpg)
The scanf Function
Example: scanf(“%c%c%c”,&x,&y,&z);– input: cat
• result:
– input: c a t• result:
Example: scanf(“%d%d”,&n1,&n2);– input: 12 3
• result:
![Page 5: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/5.jpg)
The return Statement
The statement- return(0); - transfers control from your program to the OS– It should be the last statement in your program.
(Why?)– 0 is the result of the execution of function main
and signifies that the program completed without error
![Page 6: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/6.jpg)
General Form of a C Program
At this point, we have learned enough of the elements of a C program to be able to combine them into a working program
Figure 1.9 shows an example of a working C program. Note the order in which the elements occur in this program
![Page 7: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/7.jpg)
General Form of a C Program
preprocessor directivesmain function heading{
declarationsexecutable statements
}
![Page 8: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/8.jpg)
Program Style
It is important to develop a consistent approach to programming style
For starters, follow the style illustrated in the text
Use spaces and indentation consistently Use meaningful comments
– A header section given info. on the programmer and program should be used
![Page 9: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/9.jpg)
Arithmetic Expressions
Arithmetic expressions are formed of operators and operands
The C arithmetic operators are: +, -, *, /, and % (modulo or remainder)
The operands in an arithmetic expression can be either numeric literals (numbers) or numeric variables
![Page 10: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/10.jpg)
Arithmetic Expressions
The division operator (/) can be applied to integers values, real values, or a combination– When applied to reals or mixed reals and
integers, the result is a real value– When applied to two integers, the result is the
integral part of the result• Example: 7/2 is 3
• 7.0/2 is 3.5
![Page 11: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/11.jpg)
Arithmetic Expressions
The modulo operator can be applied to integers and it gives the remainder of integer division– Example: 7 % 2 is 1– Example: 8 % 2 is 0
Division by zero will cause a run-time error (execution of the program will be halted at that point)
![Page 12: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/12.jpg)
Arithmetic Expressions
C allows mixed-type expressions in which the two operands are of different types– C defines numerical data types int and double
(among others)– If both operands are of type int, the result is
also an int– Otherwise, the result is a double
![Page 13: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/13.jpg)
Arithmetic Expressions
An expression may involve multiple arithmetic operators
Operators may be either unary or binary Unary operators are unary + and - Examples:
– x = -y;– p = +x * y;
![Page 14: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/14.jpg)
Arithmetic Expressions
What is the value of 5 + 7 * 2? How about 10 / 5 * 2
In order to know, we must examine how C evaluates expressions
There are three rules C uses to evaluate expressions– If there are parentheses, evaluate the
subexpression within the parentheses first
![Page 15: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/15.jpg)
Arithmetic Expressions
• Example 3 * (5 + 2)
• Parentheses can also be nested 2 * ( 5 * (2 + 1))
• In this case, evaluate the most deeply nested subexpression first
– The second rule involves operator precedence• We evaluate the operator which has highest
precedence first
• C precedence: (unary) + and - have highest precedence; *, /, and % are next; (binary) + and - have the lowest precedence
![Page 16: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/16.jpg)
Arithmetic Expressions
– If we have multiple operators with the same precedence we use associativity rules
• Unary + and - are evaluated using right associativity (from right to left)
• The binary operators are evaluated using left associativity (from left to right)
In order to better understand evaluation of complex expressions, use evaluation trees
![Page 17: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/17.jpg)
Formatting Numbers in Output
Unless otherwise instructed, C displays numeric values in printf statements using a default notation
To change the default notation for integers, add a numeric field width between the ‘%’ and ‘d’– printf(“%3d %4d\n”, 11, 1); /* Note the use of
a numeric literal in the print list! */
![Page 18: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/18.jpg)
Formatting Numbers in Output
Real numbers can be formatted by specifying both a field width and the number of decimal places to be displayed– Printf(“%5.2f %3.2f %4.1f\n”, 3.14,3.14,3.14);– Note that if the field width isn’t wide enough
for the numeric value, it will be expanded– This doesn’t work with character variables!
![Page 19: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/19.jpg)
Interactive and Batch Modes
So far, we have seen programs which employ interactive mode– In this mode, input comes from the keyboard
and output goes to the monitor
In batch mode, input and output are to and from files– To work in this mode, we need to redirect
input, output or both
![Page 20: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/20.jpg)
Interactive and Batch Modes
In DOS and UNIX, redirect input by following the name of your program with a ‘<‘ and the name of the file you want to read data from (one line for each scanf)– prog1 < myinput.dat
Redirect output by following the name of your program with a ‘>‘ and the name of the file you want to write data to– prog1 > myoutput.dat
![Page 21: The scanf Function 4 The scanf function reads input from the standard input device into one or more variables 4 Example: scanf(“%lf”, &miles); –Reads a](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d4d5503460f94a2b464/html5/thumbnails/21.jpg)
Types of Errors
There are three categories of programming errors– Syntax errors are discovered by the compiler– Run-time errors cause execution of the program
to be halted– Logic errors cause the program to give
incorrect results