Chapter Five
Chapter Five
Chapter Five
Chapter Objectives
By the ends of this chapter the learner should be able to;
Describe the C program operators and their classifications
Use C program operators appropriately and correctly in a C Program
Describe the C program expressions
Use C program Expression appropriately in a C program
1.1. Overview
C supports a rich set of built-in operators. An operator is a symbol that tells the computer to perform
certain mathematical or logical manipulations on data or variables. Categories of C operators includes;
Arithmetic operators; Relational operators; Logical operators; Assignment operators; Increment and
decrement operators; Conditional Operators; Bitwise operators; Special operators
Integer division truncates any fractional part. The modulo division operation produces the remainder of an
integer division. Example
a-b a+b a*b a/b a%b -a*b
Here a and b are variables and are known as operands.
1.3. Relational Operators and Logical operators
We often compare two quantities and depending on their relation make certain decision. The comparison
is done using relational operator. C supports six relational operators shown in table 5.1;
< Is less than
<=` Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
Table 5.1 C relational operators
Out Put
2
4
16
1.5. Increment and Decrement Operators
C allows two very useful operators not generally found in other languages called increment and
decrement operators ++ and -- operators
++ adds 1 to the operand
-- Subtracts 1 from the operand
Format ++x or --x
Examples
++m is the same as m = m+1 (or m +=1;)
--m is equivalent to m = m-1 (or m -+1);
Increment and decrement operators and extensively used in the for and while loops
example
a= 10
b = 15;
x = (a>b)? a:b;
x will be assigned value of b
if (a>b)
x=a
else
x=b
The sizeof operator is normally used to determine the lengths of arrays and structures when their sizes are
not known to the programmer
getch();
}
Out Put
x = 10.000000
y = 7.000000
z = 4.000000
C operators in order of precedence (highest to lowest). Their associativity indicates in what order
operators of equal precedence in an expression are applied. Table 5.3. below C operators precedence and
associativity
* / % Multiplication/division/modulus left-to-right
+ - Addition/subtraction left-to-right
|| Logical OR left-to-right
= Assignment right-to-left
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment
Note 1:
Parentheses are also used to group sub-expressions to force a different precedence; such
parenthetical expressions can be nested and are evaluated from inner to outer.
Note 2:
Postfix increment/decrement have high precedence, but the actual increment or decrement of
the operand is delayed (to be accomplished sometime before the statement completes
execution). So in the statement y = x * z++; the current value of z is used to evaluate the
expression (i.e., z++ evaluates to z) and z only incremented after all else is done. See
postinc.c for another example.
Example
If (x== 10 +15 && y<10)
+ has a higher precedence than the && and the relational operators && and <
Then
if ( x ==15&&y<10)
Next determine if x == to 15 and y<10. If x= 20 and y = 5 then
x==25 is false
y<10 is true