Chapter 7 - C Program Operators and Expressions
Chapter 7 - C Program Operators and Expressions
Chapter 7 - C Program Operators and Expressions
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
7.1. Introduction
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;
1) Arithmetic operators;
2) Relational operators;
3) Logical operators;
4) Assignment operators;
5) Increment and decrement operators;
6) Conditional Operators;
7) Bitwise operators;
8) 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.
The logical operators && and || used when testing more than one condition and make a decision.
if mark >= 80 && mark < 90
An expression combining two or more relational expressions is called logical expression or
compound relational expression and yields the value of either 1 or 0 / true or false.
Out Put
2
4
16
Increment and decrement operators and extensively used in the for and while loops
Rules for ++ and – Operators
They are urinary operators and requires a variable as their operand
When postfix ++ (or -- ) is used with a variable in an expression, the expression is evaluated first
using the original value of the variable and then the variable is incremented (or decremented) by 1
When prefix ++ (or -- ) is used with a variable in an expression, the expression is incremented (or
decremented) first and then the expression is evaluated using the new value of the variable.
The precedence and associatively of ++ and – operators are the same as those of unary + and unary
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
Program example 7.2. Program to illustrate the use of variables in expressions and their
evaluation
#include<string.h>
#include<stdio.h>
#include<conio.h>
#define N 100
#define A 2
main()
{
int a, b, c, d, x, y, z;
a=9;
b = 12;
c = 3;
x = a-b/3 +c*2 -1;
y = a-b/ (3+c) * (2-1);
z = a- (b /(3+c) * 2) -1;
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 7.6. below C operators
precedence and associativity
Example
If (x== 10 +15 && y<10) Then
+ has a higher precedence than the && and the if ( x ==15&&y<10)
relational operators && and < Next determine if x == to 15 and y<10. If x=
20 and y = 5 then
x==25 is false
y<10 is true