Chapter 7 - C Program Operators and Expressions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

BIT 1102 INTRODUCTION TO PROGRAMMING & ALGORITHM

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

7.2. Arithmetic Operators


C provides all the basic arithmetic operators listed in table 7.1. below
+ Addition of unary plus
- Subtraction of unary minus
* Multiplication
/ Division
% Modulo division
Table 7.1: Basic arithmetic 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.

7.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
7.2.

Intro to Programming – Chapter7: C Operators & Expressions Page 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 7.1 C relational operators

7.3.1. Logical Operators


C supports the following three logical operators
1. && meaning logical AND
2. || meaning logical OR
3. ! meaning logical NOT

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.

7.3.2. Relative precedence of the Relational Operators


The relative precedence of the relational and logical operators are as follows
Highest !
> >= < >=
== !=
&&
Lowest ||

7.4. Assignment operators


Assignment operators are used to assign the results of an expression to a variable. The usual
assignment operator is “=”. C has a set of ‘shorthand’ assignment operators of the form
vop = exp;
Where v is a variable, exp is an expression and op is a C binary arithmetic operator. The operator
op= is known as the shorthand assignment operator. Shorthand operators in C are shown in table
7.3. below

Statement with simple assignment operator Statement with shorthand operator


a = a+1 a+=1
a = a-1 a -+1
a = a*(n+1) a *=n+1
a = a/(n+1) a /= n+1
a = a%b a %=b

Intro to Programming – Chapter7: C Operators & Expressions Page 2


Program Example 7.1: Program to print a sequence of squares of numbers.
/* documentation section*/
#include<string.h>
#include<stdio.h>
#include<conio.h>
#define N 100
#define A 2
main()
{
int a;
a=A;
while (a < N)
{
printf("%d\n", a);
a *= a;
}
getch();
}

Out Put
2
4
16

7.5. Increment and Decrement Operators


C allows two very useful operators not generally found in other languages called increment and
decrement operators ++ and -- operators
1) ++ adds 1 to the operand
2) 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
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

7.6. Conditional Operators


A ternary operator pair “?:” is used in C to construct conditional expressions. The format for using
the conditional operator is;

Intro to Programming – Chapter7: C Operators & Expressions Page 3


exp1 ? exp2 : exp3
where exp1, exp2 and exp3 are expressions. If exp1 is evaluated and found to be nonzero(true) then
expression exp2 is evaluated and becomes the value of the expression. If Exp1 is found to be
nonzero(false) the Exp3 is evaluated.
If exp=true
then exp2
else exp3

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

7.7. Special Operators


C supports some special operators namely, comma operator, size-of operator, pointer operator (&
and *) and member selection operators (. &->) . The pointer and member selection will be
discussed in later chapters.

7.7.1. Comma Operator


Used to link the related expressions together. A comma linked list of expressions is evaluated left to
right and the value of right-most expression is the value of the combined expression
value = (x =10, y =5, x+y);
first assigns 10 to x then assigns 5 to y and finally assigns 15 (10+5) to value

7.7.2. Size of Operators


The sizeof operator is a compile time operator and when used with an operand, it returns the
number of bytes the operand occupies. The operand may be a variable constant or a data type
qualifier. Example
m = sizeof(sum);
n = sizeof(long int);

7.8. Arithmetic Expressions


An arithmetic expression is a combination of variables, constants and operators arranged as per the
syntax of the language. Example
a x b-c = a*b –c
(m+n)(x+y) = (m+n)*(x+y)

Expressions are evaluated using an assignment statement of the form


variable = expression;
example
Intro to Programming – Chapter7: C Operators & Expressions Page 4
x = a * b – c;
y = b/c * a;

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;

printf("x = %f\n", x);


printf("y = %f\n", y);
printf("z = %f\n", z);

getch();
}

Out Put
x = 10.000000
y = 7.000000
z = 4.000000

7.9. Precedence of Arithmetic Operators


An arithmetic expression without parentheses will be evaluated from left to right using the rules of
precedence of operators. There are two distinct priority levels of arithmetic operators in C.
High priority: * / %
Low priority: + -

Intro to Programming – Chapter7: C Operators & Expressions Page 5


Rules for evaluation of Expression
 First parenthesized sub-expression from left to right is evaluated
 If parentheses are nested, the evaluation begins with the innermost sub-expression.
 The precedence rule is applied in determining the order of application of operators in evaluating sub-
expressions.
 The associativity rule is applied when two or more operators of the same precedence level appear in a
sub-expression.
 Arithmetic expressions are evaluated from left to right using the rules of precedence.
 When parentheses are used, the expression within parentheses assume highest priority.

7.9.1. Operator Precedence and Associativity


Each operator in C has a precedence associated to it. This precedence is used to determine how an
expression involving more than one operator is evaluated. There are distinct levels of precedence
and an operator may belong to any of these levels. Operators on the higher precedence level are
evaluated first, operators at the same level are evaluated either from left-to-right or from right-to-
left depending on the level. This is called associativity of the operator.

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

Operator Description Associativity


() Parentheses (function call) (see Note 1) left-to-right
[] Brackets (array subscript)
. Member selection via object name
-> Member selection via pointer
++ -- Postfix increment/decrement (see Note 2)
++ -- Prefix increment/decrement right-to-left
+ - Unary plus/minus
! ~ Logical negation/bitwise complement
(type) Cast (change type)
* Dereference
& Address
sizeof Determine size in bytes
* / % Multiplication/division/modulus left-to-right
+ - Addition/subtraction left-to-right
<< >> Bitwise shift left, Bitwise shift right left-to-right
< <= Relational less than/less than or equal to left-to-right
> >= Relational greater than/greater than or equal to
== != Relational is equal to/is not equal to left-to-right
& Bitwise AND left-to-right
^ Bitwise exclusive OR left-to-right

Intro to Programming – Chapter7: C Operators & Expressions Page 6


| Bitwise inclusive OR left-to-right
&& Logical AND left-to-right
|| Logical OR left-to-right
?: Ternary conditional right-to-left
= Assignment right-to-left
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment
, Comma (separate expressions) left-to-right
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) 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

Chapter review questions


1. Which of the following expressions are true
a). !(5+5>=10)
b). 5 + 5 = =10 || 1+3 ==5.
c). 10! = 15&& !(10<20) || 15>20.
2. Identify unnecessary parentheses in the following arithmetic expressions.
a). ((x-(y/5)+z)%8) +25
b). (m*n) + (-x/y)
c). x/(3*y)
3. Find the output of the following program
main()
{
int x = 100;
printf(“%d/n”, 10 + x++);
printf(“%d/n”, 10 + ++x

Intro to Programming – Chapter7: C Operators & Expressions Page 7

You might also like