Next: Assignment Expressions, Previous: Lexical Syntax, Up: Top [Contents][Index]
Arithmetic operators in C attempt to be as similar as possible to the abstract arithmetic operations, but it is impossible to do this perfectly. Numbers in a computer have a finite range of possible values, and non-integer values have a limit on their possible accuracy. Nonetheless, except when results are out of range, you will encounter no surprises in using ‘+’ for addition, ‘-’ for subtraction, and ‘*’ for multiplication.
Each C operator has a precedence, which is its rank in the grammatical order of the various operators. The operators with the highest precedence grab adjoining operands first; these expressions then become operands for operators of lower precedence. We give some information about precedence of operators in this chapter where we describe the operators; for the full explanation, see Binary Operator Grammar.
The arithmetic operators always promote their operands before operating on them. This means converting narrow integer data types to a wider data type (see Operand Promotions). If you are just learning C, don’t worry about this yet.
Given two operands that have different types, most arithmetic
operations convert them both to their common type. For
instance, if one is int
and the other is double
, the
common type is double
. (That’s because double
can
represent all the values that an int
can hold, but not vice
versa.) For the full details, see Common Type.
• Basic Arithmetic | Addition, subtraction, multiplication, and division. | |
• Integer Arithmetic | How C performs arithmetic with integer values. | |
• Integer Overflow | When an integer value exceeds the range of its type. | |
• Mixed Mode | Calculating with both integer values and floating-point values. | |
• Division and Remainder | How integer division works. | |
• Numeric Comparisons | Comparing numeric values for equality or order. | |
• Shift Operations | Shift integer bits left or right. | |
• Bitwise Operations | Bitwise conjunction, disjunction, negation. |
Next: Assignment Expressions, Previous: Lexical Syntax, Up: Top [Contents][Index]