Title: Standard Input and Output
1Standard Input and Output
2To OUTPUT you require
- include ltstdio.hgt
- printf(format string, data list)
- Field specifiers are inside the format string
- i.e. Controls how the data looks when printed
3Standard printf statements
include ltstdio.hgt void main( ) / This codes
prints the values of two variables / int a
57 int b 145 printf( "d\nd\n",
a,b) return
4What happened?
- The statements
- int a 57 a 57
- int b 145 b 145
The statement printf( "d\nd\n", a , b) prints
57 145
5Field Specifiers
- ltflaggtltminimum widthgtltprecisiongtltsizegtcode
- Codes
6Specification of Width
7- Precision specification for floats
- 7.2f
- / this prints the float in a field of seven
characters with two characters after decimal - nnnn.dd /
8Another Example of printf
- include ltstdio.hgt
- int main()
- / This codes prints the values of two variables
/ - int months 9
- double salary 145.35
- printf( "d\n7.2f\n", months , salary)
- return 0
-
-
9The output
10Another Example of printf
- include ltstdio.hgt
- int main( )
- / This codes prints the values of two variables
/ - int months 9
- double salary 145.35
- printf( "the number of months is4d\nthe
salary is7.2f\n", months,salary) - return 0
11The output
the number of months is 9 the salary is 145.35
12Input Getting information into memory input
(or reading)
- Requires
- include ltstdio.hgt
- scanf(format string, address list)
- Again Field specifiers inside the format string
13Standard scanf Statements
- scanf(format string, address list)
- scanf(df, age, weight)
- At keyboard type 23 60.75
- Result weight
- age
-
60.75
23
14Field Specifiers
- ltflaggtltmaximum widthgtltsizegtcode
- Remember There is no precision issue here !!!
- Codes
15Rules for scanf formats
- Addresses of a variable are specified with
- variableName
- A variety of rules apply to conversion
16Rules for scanf formats
- Initial whitespace is ignored (except c)
- The conversion operation process until
- End of file is reached
- Maximum characters are processed
- A whitespace character is found after a digit
- An error is detected
17Rules for scanf format strings
- A field specifier for each variable
- Other characters must be exactly matched
- Cannot end format string with whitespace
18Examples of scanf
- scanf(dddc,a,b,c,d)
- scanf(ddd c,a,b,c,d)
- scanf(-8d-8dd,a,b,c)
- -8d / left justify flag /
19Working with the variables
- We want to operate on variables e.g add two
variables or subtract them. - An Expression
- rateOfPayhours tax
20Operators
- e.g., plus
- - minus
- multiply
- / divide
- mod
- You create expressions out of operators.
21Some Expressions
- 35
- 2 3 4
- 23 b 6
- -salary
- Use parenthesis to clarify complicated
expressions - (food drinks)(1gstpst)
22Assignment
- Assignment expressions evaluate to the expression
on the right of the assignment operator.
23Simple Assignment
24Operator Precedence
- Operators have a built in order of precedence
which is over ridden by using parenthesis - 1234 answer 11
- 12 35 answer 17
- -562 answer -28
25Operators precedence (on the same level they are
equal)
- 1. ( ) takes precedence
- 2. unary minus -7 unary plus 3
- 3. Multiply / Divide
- 4 Modulus
- 5. add - subtract
26Examples Operators
- Examples of C statements using these
- The following is a program fragment only
- int i, j ,k, l, m
- i j k
- i j k l / m
- / Here i gets the value j (kl/m) /
- i j k l m
- / Here i gets the value (jk) (lm) /
27Examples Operators
- Examples of C statements using these
- There is one division operator for integer and
float but it means different things. - 2 / 5 ? 0
- 2.0 / 5.0 ? 0.4
- is the modulus (remainder) operation
- 5 2 ? 1
- 2 5 ? 2
28Unary operator and -
- a evaluates to the contents of a
- -a evaluates to the negative contents of a
29Unary Expressions
30Unary operator and --
- Post-increment and decrement
- i i--
- /First use the value then do operation/
- Pre-increment and decrement
- i --i
- /First do operation then use the value /
31Unary operator and --
- Examples for int i , j , k
- i1
- i / i ? 2 /
- j i /j ? 2 and then i ? 3
- j i / i ? 4 and then j ? 4
- j i-- / j ? 4 and then i ? 3
- Note that i and ii1 are equivalent
- Note that i-- and ii-1 are equivalent
32Binary Compound Operators
- Often a variable is changed as follows
- i i 5 / Take the value of i add 5 and put
the result back into the variable i/. - Leads to new assignment operators
- - /
- i i 1 same as i 1
- j j k same as j k
33Compound Assignment
34A Simple Program
- include ltstdio.hgt
- int main( void )
- double x, y,z
- printf("Pls. input two real numbers\n")
- scanf("ff",x,y)
- z x y
- printf("x6.2f y6.2f z 6.3f \n",x,y,z)
- return(0)
35A Simple Program
- The OUTPUT
- gtPlease input two real numbers
- 23.4 45.6
- x 23.40 y 45.60 z69.000
36- / A simple program to average 3 numbers/
- include ltstdio.hgt
- int main(void)
- float number1,number2,number3,sum,average
- printf("\n Please enter number1. ")
- scanf("f",number1)
- printf("\n Please enter number2. ")
- scanf("f",number2)
- printf("\n Please enter number3. ")
- scanf("f",number3)
- sum number1number2number3
- average sum/3.0
- printf("\nthe sum of f f and f is
f",number1, - number2,number3, sum)
- printf("\nthe average is f\n",average)
- return 0
-
37- Whats the problem with the previous program?
- What if you wanted to average 50 numbers?
- It would be very clumsy to repeat 50 times.
- Also you have to change the program each time
you have a different number of numbers.
38Program to calculate tax on a bill
- First you have to know the facts.
- Subtotal food and drink
- PST 0.08
- GST 0.06
- Tax subtotal (PSTGST)
- Total subtotal tax
39- / A simple program calculate restaurant bill
input food and drink amounts - output subtotal, tax, total
bill/ - include ltstdio.hgt
- define PST 0.08
- define GST 0.06
- int main(void)
- float food,drink,sum,total,tax
- printf("\n Please enter food total. ")
- scanf("f",food)
- printf("\n Please enter drink total. ")
- scanf("f",drink)
- sum food drink
- tax sum(GST PST)
- total sumtax
- printf("\nthe food total is 6.2f ",food)
- printf("\nthe drink total is 6.2f ",drink)
- printf("\n ")
- printf("\nthe subtotal is 6.2f ",sum)
- printf("\nthe tax is 6.2f ",tax)
40- Whats the problem with the previous program?
- The rule is not right. If the subtotal is less
than 3.00 the no GST - So we need decision possibility
- So we need CONTROL STRUCTURES
41Unary Operator sizeof
- sizeof is an operator. It is NOT a function
- Evaluates to number of bytes for that item
- sizeof(int)
- sizeof(x)
- sizeof(3.256)