|
Arithmetic Operators |
|
Operation |
Operator |
Type |
Operand |
Result |
|
Addition |
+ |
Binary |
if both integers |
Integer |
|
Subtraction |
- |
Binary |
||
|
multiplication |
* |
Binary |
if one operand not an integer |
Double precision |
|
Division |
/ |
Binary |
||
|
Remainder |
% |
Binary |
Both must be integers |
Integer |
|
Negation |
- |
Unary |
if one integer |
Integer |
|
if one floating point or double precision operand |
Double precision |
|
Rules |
If all operands are integers , the result is an integer.
If any operand is a floating point or double precision , the result is a double precision number.
Two binary arithmetic operator symbols must never be placed side by side. For example 5 * % 12 is invalid.
All expressions enclosed within parentheses are evaluated first .For example (6+4) / ( 2 * 1 ) , the 6 + 4 and 2 * 1 are evaluated first , yielding 10 / 2 . The 10 / 2 is then evaluated to yield 2 .
We can use parentheses inside parentheses . The inner parentheses are evaluated first . For example ( 2 * ( 5+7) ) / 4 = ( 2 *12) / 4 = 24 / 4 =6
Parentheses cannot be used to indicate multiplication . For example (3+1)(4+6) is invalid . We must use the * operator . The correct expression is (3+1) * (4+6) .
Operand : can be a single constant , single variable or valid combination of constants and variables.
The space around the operator does not affect the calculation.
The result of an arithmetic expression is never a floating point number because the computer temporarily converts all floating numbers to double precision numbers .
Some languages have an exponentiation operator (typically ^ or **), but C doesn't. (To square or cube a number, just multiply it by itself.)
|
Division |
When applied to integers, the division operator / discards any remainder, so 1 / 2 is 0 and 7 / 4 is 1. But when either operand is a floating-point quantity (type float or double), the division operator yields a floating-point result, with a potentially nonzero fractional part. So 1 / 2.0 is 0.5, and 7.0 / 4.0 is 1.75.
The modulus operator % gives you the remainder when two integers are divided: 1 % 2 is 1; 7 % 4 is 3. (The modulus operator can only be applied to integers.)
Examples
17 % 2 = 8
17 % 2 = 1
12 % 3 =4
12 % 3 =0 ( No Remainder)
|
Unary Operator ( Negation ) |
The minus sign used in front of a single numerical operand negates the number.
|
Operator Precedence and Associativity |
In the absence of parentheses ,expressions containing multiple operators are evaluated by the priority or precedence of each operator relative to all other operators .
|
Operator |
Associativity |
|
( ) , unary - |
right to left |
|
* , / , % |
left to right |
|
+ , - |
left to right |
Expressions containing operators with the same precedence are evaluated according to their associativity .
When two or more operators have the same precedence and participate next to each other in an expression, the evaluation conceptually proceeds from left to right. For example, 1 - 2 - 3 is equivalent to (1 - 2) - 3 and gives -4, not +2.
For example , in the expression 8 + 5 * 2 / 4 - 1 , the multiplication and the division operators are at higher precedence than + and - and they are of equal priority .
8+5 *2 /4 -1 =
8+10 /4 -1 =
8 + 2-1 =
10 -1 = 9
|
Increment and Decrement Operators |
++c;
printf( “%d”, c );
c++;
printf( “%d”, c );
#include <stdio.h>
int main()
{
int c = 5;
printf( "%d\n", c );
printf( "%d\n", c++ ); /* postincrement */
printf( "%d\n\n", c );
c = 5;
printf( "%d\n", c );
printf( "%d\n", ++c ); /* preincrement */
printf( "%d\n", c );
return 0; /* successful termination */
}