chapter04

# chapter04 - COP 3275: Chapter 04 Jonathan C.L. Liu, Ph.D....

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

COP 3275: Chapter 04 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

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

View Full Document
Operators C emphasizes expressions rather than statements. Expressions are built from variables, constants, and operators. C has a rich collection of operators, including arithmetic operators relational operators logical operators assignment operators increment and decrement operators and many others 2
Arithmetic Operators C provides five binary arithmetic operators: + addition - subtraction * multiplication / division % remainder An operator is binary if it has two operands. There are also two unary arithmetic operators: + unary plus - unary minus 3

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

View Full Document
Unary Arithmetic Operators The unary operators require one operand: i = +1; j = -i; The unary + operator does nothing. It’s used primarily to emphasize that a numeric constant is positive. 4
Binary Arithmetic Operators The value of i % j is the remainder when i is divided by j . 10 % 3 has the value 1, and 12 % 4 has the value 0. Binary arithmetic operators—with the exception of % —allow either integer or floating-point operands, with mixing allowed. When int and float operands are mixed, the result has type float . 9 + 2.5f has the value 11.5, and 6.7f / 2 has the value 3.35. 5

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

View Full Document
The / and % Operators The / and % operators require special care: When both operands are integers, / “truncates” the result. The value of 1 / 2 is 0, not 0.5. The % operator requires integer operands; if either operand is not an integer, the program won’t compile. Using zero as the right operand of either / or % causes undefined behavior. The behavior when / and % are used with negative operands is implementation-defined in C89. In C99, the result of a division is always truncated toward zero and the value of i % j has the same sign as i . 6
Implementation-Defined Behavior The C standard deliberately leaves parts of the language unspecified. Leaving parts of the language unspecified reflects C’s emphasis on efficiency, which often means matching the way that hardware behaves. It’s best to avoid writing programs that depend on implementation-defined behavior. 7

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

View Full Document
Operator Precedence Does i + j * k mean “add i and j , then multiply the result by k ” or “multiply j and k , then add i ”? One solution to this problem is to add parentheses, writing either (i + j) * k or i + (j * k) . If the parentheses are omitted, C uses operator precedence rules to determine the meaning of the expression. 8
Operator Precedence The arithmetic operators have the following relative precedence: Highest: + - (unary) * / % Lowest: + - (binary) Examples: i + j * k is equivalent to i + (j * k) -i * -j is equivalent to (-i) * (-j) +i + j / k is equivalent to (+i) + (j / k) 9

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

View Full Document
Operator Associativity Associativity comes into play when an expression contains two or more operators with equal precedence. An operator is said to be
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/30/2011 for the course COP 3275 taught by Professor Jonathanliu during the Fall '11 term at University of Florida.

### Page1 / 49

chapter04 - COP 3275: Chapter 04 Jonathan C.L. Liu, Ph.D....

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

View Full Document
Ask a homework question - tutors are online