Standard Input and Output - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Standard Input and Output

Description:

Standard Input and Output. To OUTPUT you require #include stdio.h ... Field specifiers are inside the format string. i.e. Controls how the data looks when printed ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 42
Provided by: morr3
Category:
Tags: bb | input | output | standard

less

Transcript and Presenter's Notes

Title: Standard Input and Output


1
Standard Input and Output
2
To 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

3
Standard 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
4
What happened?
  • The statements
  • int a 57 a 57
  • int b 145 b 145

The statement printf( "d\nd\n", a , b) prints
57 145
5
Field Specifiers
  • ltflaggtltminimum widthgtltprecisiongtltsizegtcode
  • Codes

6
Specification 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 /

8
Another 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

9
The output
  • 9
  • 145.35

10
Another 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

11
The output
the number of months is 9 the salary is 145.35
12
Input Getting information into memory input
(or reading)
  • Requires
  • include ltstdio.hgt
  • scanf(format string, address list)
  • Again Field specifiers inside the format string

13
Standard scanf Statements
  • scanf(format string, address list)
  • scanf(df, age, weight)
  • At keyboard type 23 60.75
  • Result weight
  • age

60.75
23
14
Field Specifiers
  • ltflaggtltmaximum widthgtltsizegtcode
  • Remember There is no precision issue here !!!
  • Codes

15
Rules for scanf formats
  • Addresses of a variable are specified with
  • variableName
  • A variety of rules apply to conversion

16
Rules 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

17
Rules for scanf format strings
  • A field specifier for each variable
  • Other characters must be exactly matched
  • Cannot end format string with whitespace

18
Examples 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 /

19
Working with the variables
  • We want to operate on variables e.g add two
    variables or subtract them.
  • An Expression
  • rateOfPayhours tax

20
Operators
  • e.g., plus
  • - minus
  • multiply
  • / divide
  • mod
  • You create expressions out of operators.

21
Some Expressions
  • 35
  • 2 3 4
  • 23 b 6
  • -salary
  • Use parenthesis to clarify complicated
    expressions
  • (food drinks)(1gstpst)

22
Assignment
  • Assignment expressions evaluate to the expression
    on the right of the assignment operator.

23
Simple Assignment
24
Operator 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

25
Operators 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

26
Examples 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) /

27
Examples 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

28
Unary operator and -
  • a evaluates to the contents of a
  • -a evaluates to the negative contents of a

29
Unary Expressions
30
Unary 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 /

31
Unary 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

32
Binary 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

33
Compound Assignment
34
A 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)

35
A 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.

38
Program 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

41
Unary 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)
Write a Comment
User Comments (0)
About PowerShow.com