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

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

 

Increment operator (++)

    Can be used instead of c+=1

 

Decrement operator (--)

    Can be used instead of c-=1

 

Preincrement

    Operator is used before the variable (++c or --c)

  1. Change the variable.

  2. Then evaluate the expression.

 

Postincrement

    Operator is used after the variable (c++ or c--)

  1. Executes the expression .

  2. Then the variable is changed.

 

If c equals 5, then

  printf( "%d", ++c );

        Prints 6

  printf( "%d", c++ );

        Prints 5

In either case, c now has the value of 6

 

When variable not in an expression

Preincrementing and postincrementing have the same effect

++c;

printf( “%d”, c );

Has the same effect as

c++;

printf( “%d”, c );

Examples

#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 */
}