the scanf function 4 the scanf function reads input from the standard input device into one or more...
Post on 20-Dec-2015
245 views
TRANSCRIPT
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
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
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!)
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:
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
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
General Form of a C Program
preprocessor directivesmain function heading{
declarationsexecutable statements
}
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
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
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
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)
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
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;
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
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
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
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! */
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!
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
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
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