Lecture3hu - ELE108 lecture 3 Alkar + Demirer 1 Overview C...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ELE108 lecture 3 Alkar + Demirer
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Overview • C Arithmetic Expressions • Formatting Numbers in Program Output • Interactive Mode, Batch Mode, and Data Files • Common Programming Errors • Programming Style
Background image of page 2
3 Arithmetic Expressions • Operators • Data Type of Expression • Mixed-Type Assignment Statement • Type Conversion through Cast • Expressions with Multiple Operators • Writing Mathematical Formulas in C
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Why Arithmetic Expressions • To solve most programming problems, you will need to write arithmetic expressions that manipulate type int and double data. • The next slide shows all arithmetic operators. Each operator manipulates two operands , which may be constants, variables, or other arithmetic expressions. •E x am p l e ±
Background image of page 4
5 C Operators Arithmetic Operator Meaning Examples + ( int , double ) Addition 5 + 2 is 7 5.0 + 2.0 is 7.0 -( int , double ) Subtraction 5 - 2 is 3 5.0 - 2.0 is 3.0 * ( int , double ) Multiplication 5 * 2 is 10 5.0 * 2.0 is 10.0 / ( int , double ) Division 5 / 2 is 2 5.0 / 2.0 is 2.5 % ( int ) Remainder 5 % 2 is 1
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Division : When applied to two positive integers, the division operator (/) computes the integral part of the result by dividing its first operand by its second. ± For example 7.0 / 2.0 is 3.5 but the but 7 / 2 is only 3 ± The reason for this is that C makes the answer be of the same type as the operands. Remainder : The remainder operator (%) returns the integer remainder of the result of dividing its first operand by its second. ± Examples: 7 % 2 = 1, 6 % 3 = 0 ± The value of m%n must always be less than the divisor n. ± / is undefined when the divisor (second operator) is 0.
Background image of page 6
7 Data Type of an Expression • The data type of each variable must be specified in its declaration, but how does C determine the data type of an expression? ± Example: What is the type of expression x+y when both x and y are of type int ? • The data type of an expression depends on the type(s) of its operands. ± If both are of type int , then the expression is of type int . ± If either one or both is of type double , then the expression is of type double . • An expressions that has operands of both int and double is a mixed-type expression.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Mixed-Type Assignment Statement • The expression being evaluated and the variable to which it is assigned have different data types. ± Example what is the type of the assignment y = 5/2 when y is of type double ? • When an assignment statement is executed, the expression is first evaluated; then the result is assigned to the variable to the left side of assignment operator. Warning : assignment of a type double expression to a type int variable causes the fractional part of the expression to be lost. ± What is the type of the assignment
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 55

Lecture3hu - ELE108 lecture 3 Alkar + Demirer 1 Overview C...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online