Skip to main content

2.4 Operators

Operators in C are used to manipulate and compare variables and constants.

1. Arithmetic operators

These can be applied to real numbers and integers. With one exception (%) the operands can be integers, real numbers or a mixture of the two. If the operands for any of the above operators are two integers, the result will also be an integer; if at least one of the operands is a real number, the result will be a real number. The operators are summarised below.

addition +
subtraction -
multiplication *
division /
remainder %

The % operator is special in the sense that both operands must be integers.

Putting it into Practice:

In this example, write code so that it follows the code given below. This will give some examples of operators in action;
#include <stdio.h>
float res;

int main()
{ printf("The result of 22/4 is %d, the result of 22.0/4.0 is %f \n", 22/4,22.0/4.0); printf("The remainder of 22/4 is %d \n", 22%4); res=15/4*4.0; printf("The result of 15/4*4.0 is %f\n", res); return 0; }
Compile and run this code. The output should be:
  The result of 22/4 is 5, the result of 22.0/4.0 is 5.500000 
The remainder of 22/4 is 2
The result of 15/4*4.0 is 12.000000
The last line of output comes from the integer division being carried out first


Back to top

2. Comparison operators

To compare two variables or constants in C, comparison operators are used. These operators return a value of 0 or 1, with 0 meaning that the comparison statement is false, and 1 meaning that it is true. The operators are summarised below.

equality ==
inequality !=
less than <
greater than >
less than or equal to <=
greater than or equal to >=

Comparison operators are most useful when used in decision and control statements (more of later).

One thing to watch out for: don't use these operators to directly compare two real numbers for equality. Because of how computers treat real numbers, instead define a tolerance, and check if the absolute difference is smaller than this tolerance, e.g.

/* compare the value of a and b */
same = (abs(a-b) < tol);

Back to top

3. Boolean operators

These operators work on truth values (1 for true and 0 for false) and return another truth value. The operators are summarised below.

and &&
or ||
not !

&& returns true iff both operands are true. || returns true if at least one of the operands is true. ! is special in the sense that it takes only one operand, reversing the truth value of this operand.

Some example usage:

(1&&0)==0;

The above is a true statement.


Back to top

4. Shortcut operators

There are a number of operators in C that perform special operations, of the sort you are likely to want to use many times over when writing code. For example, there is a dedicated operator for incrementing the value of a variable by 1. These are operators to be used with care. These operators are summarised below, in the context of application to a variable, x, and an integer, n:

x= x + 1 x++
x = x - 1 x--
x = x + n x+=n
x = x - n x-=n
x = x * n x*=n
x = x / n x/=n
x = x % n x%=n

Putting it into Practice:

In this example, write a program so that it follows the code given below. This will give some examples of these operators in action;
#include<stdio.h>
float myvar;

int main()
{ myvar = 12.5; printf("myvar has value %f\n",myvar); myvar++; printf("and has now been incremented to %f\n",myvar); myvar--; printf("and is now back to orginal value %f\n",myvar); return 0;
}
Compile and run this code. The output should be:
myvar has value 12.500000
and has now been incremented to 13.500000
and is now back to orginal value 12.500000

Back to top

5. Operator precedence

When applying many operators together in one statement, evaluation using certain operators will happen first. The table below should help you understand the order in which these operations are carried out under C.

highest parentheses (, )
  unary !, ++, --, -
  multiplication *, /, %
  addition +, -
  comparison ==, !=, <, >, <=, >=
  and &&
  or ||
lowest assignment =, +=, -=, *=, /=, %=