25 May 2014

Simple C – Chapter 4, Operators




In the C program, we use operators for performing a specific task. This task can be a mathematical or arithmetic task (like calculating the Interest), comparison of values, assignment, logical manipulations etc. in other words operators are a combination of symbols that we put in a C expression (a combination of operands and operators is called an expression) between operands to get a desired result. An operand can be a variable, a constant or even another expression. For example consider the statement below:

A + 60 = B

Where A and B are variables (operands), 60 is a constant (operand) and the symbol "+" and "=" are operators. The whole statement together is an expression which tells the compiler to add 60 to the value of A and then assign the result to the variable B. in the C programming language mainly eight operators exist which are:

  • Arithmetic
  • Relational
  • Assignment
  • Logical
  • Increment/Decrement
  • Conditional
  • Bitwise
  • Special


For now, we are going to talk about the first 6 operators and we leave the last two (Bitwise and Special) for their corresponding chapter.

Arithmetic Operators

The Arithmetic operators are used for arithmetic operations such as addition, subtraction, multiplication, and division. The five arithmetic operators are

Operator
Meaning
Example
*
Multiplication
A * B
/
Division
A / B
%
Modulus
A % B
+
Addition
A + B
-
Subtraction
A - B

The Modulus operator returns the value of the remainder after division for example 10 % 3 will return the value 1. If one of or both of the operands in an arithmetic expression is of type real then the result of the expression is also real. If the both operands are integer, the result is an integer and we cannot use the Modulus (%) operator in a mixed mode arithmetic expression when one operand is an integer and the other one is of real type.

A = 20.5 (real number)

B = 6 (integer number)

A + b = 26.5 (result is a real number)


Relational operators

The Relational operators are used to check the relationship between two operands (variables) or compare their value with each other. These operators always return a Boolean value (True or False). When we use a relational operator between two operands, we create a logical or relational expression. They are mainly used in if else and loop statements to create or check a certain condition. There are 6 relational operators are available in the C language which are:

Operator
Meaning
Example
Less than
A < B
<=
Less than and equal to
A <= B
Greater than
A > B
>=
Greater than and equal to
A >=
==
Equal to
A == B
!=
Not equal to
A != B

Logical operators

As the name suggests, these operators are used for Logical or compound relational expressions. A compound relational expression simply is an expression with more than one relational expression. Like the relational operators, the logical operators are also used in control (if else) or looping (for, while etc.) statements. A logical expression returns a Boolean value which either True (1) or False (0). The three logical operators in the C are:

Operator
Meaning
Example
&&
AND
A && B
||
OR
A || B
!
NOT
A ! B

Consider the compound relational expression example:

A = 5

B = 10

C = 15

(A > b && A > c)

The AND operator will return the value True if the both expressions or values are True otherwise it will return False. The OR operator will return the value True if either one of the expressions or both of them equal to True otherwise it will return False. Check the Truth table below:

A
B
A && B
A || B
0
0
0
0
1
0
0
1
0
1
0
1
1
1
1
1

In case of the NOT operator, the operator will return True if the expression evaluates to False and it will return False if the expression evaluates to True.


A
!A
0
1
1
0

Assignment operators

To assign an expression or constant to a variable we use the Assignment operator (=). We have already seen the use of the assignments operator in arithmetic expressions like:

A = 9

B = 6 + A

In the example above we assigned the constant value 9 to the variable A using the equal sign (=) or the assignment operator and in the next statement we are adding the value 6 to A and then assign the result to the variable B. we can use two types of the assignment operator in the C program, Simple (like the examples above) and Shorthand (Arithmetic) assignment. A shorthand or arithmetic assignment operator is the equal sign with a Prefix arithmetic operator, for example:

A = A + 1           A +=1

The first expression uses a simple assignment operator but the second expression uses a shorthand assignment operator. The both examples are equals and perform the same task which is adding 1 to A.


Conditional operators

The Conditional operators or ?: can be used to create a conditional expression just like an if else statement. The general syntax is

Expression1 ? Expression2 : Expression3;

If the expressin1 evaluate to True, then the expression2 will be executed but if the expression1 evaluate to False then the expression3 will be executed, for example

(A > B) ? (A + C) : (A – C)

In the example above if the variable A is greater than the B then its value will be added to the C but if it is less than B (which is False) then its value will be subtracted from the C.


Increment and Decrement operators

The Increment operator (++) increases the value of the variable by one and the Decrement operator (--) decreases its value by one. These operators mainly used in the loop structures which we will see in the later chapters. The Increment/Decrement operators are of two types, Prefix and Postfix. In prefix increment operator, the value of the operator will be incremented/decremented then the result will assign to the variable. In the postfix increment/decrement operators, first the value will assign to the variable then it will be incremented/decremented. To understand the concept better, consider the example below:

A = 10

B = ++A  (prefix)           B = A++  (postfix)

In the first prefix operator, after the executing the statement, the value of the A will be 11 and the value of B will be 11 as well. But in the second postfix operator, after executing the statement, the value of A will be 10 and the value of B will be 11 (we get the same result in the case of decrement operator but the value will be decremented instead).


Operators Precedence

Another important concept when we write a C program that we should consider is the order of Precedence of the Arithmetic, Logical, and Relational expressions. It determines that in which order the operations will be executed by the C compiler. If we assume an expression such as A + B * C, it would important for us to know which operation performed first (the addition operator or the multiplication). The table below gives you the precedence and associatively between the operators in the C language. Remember the operator with the highest Priority (the lesser the number the higher the priority) will execute first and those with equal priority will execute associatively (left to right or right to left).

Operator
Priority
Associatively
Description
()
1
Left to Right
Parenthesis
[]
1
Left to Right
Square Bracket
+
2
Right to Left
Unary Plus
-
2
Right to Left
Unary Minus
++
2
Right to Left
Increment
--
2
Right to Left
Decrement
!
2
Right to Left
Logical NOT
~
2
Right to Left
Ones Compliment
*
2
Right to Left
Indirection
&
2
Right to Left
Address
Size of
2
Right to Left
Size of an Object
(Type)
2
Left to Right
Type Cast
*
3
Left to Right
Multiplication
/
3
Left to Right
Division
%
3
Left to Right
Modulus
+
4
Left to Right
Addition
-
4
Left to Right
Subtraction
<< 
5
Left to Right
Left Shift
>> 
5
Left to Right
Right Shift
6
Left to Right
Less than
<=
6
Left to Right
Less than Equal to
6
Left to Right
Greater than
>=
6
Left to Right
Greater than Equal to
==
7
Left to Right
Equality
!=
7
Left to Right
Inequality
&
8
Left to Right
Bitwise AND
^
9
Left to Right
Bitwise XOR
|
10
Left to Right
Bitwise OR
&&
11
Left to Right
Logical AND
||
12
Left to Right
Logical OR
?:
13
Right to Left
Conditional Expression
=
14
Right to Left
Assignment
,
15
Left to Right
Comma Operator