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

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

1 ELE108 lecture 3 Alkar + Demirer

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

View Full Document
2 Overview • C Arithmetic Expressions • Formatting Numbers in Program Output • Interactive Mode, Batch Mode, and Data Files • Common Programming Errors • Programming Style
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

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

View Full Document
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 ±
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

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

View Full Document
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.
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.

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

View Full Document
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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online