c Programming-chapter 03
c Programming-chapter 03
PRESENTED BY
SHEYONA G
CHAPTER-03
Operators and
Expressions
OPERATORS
Operators are symbols that are used to perform mathematical or logical
operations. The C programming language is rich with built-in operators.
TYPES OF OPERATORS
a. Arithmetic Operators
b. Relational Operators
c. Logical Operators
d. Bitwise Operators
e. Increment and Decrement Operator
f. Assignment Operator
g. Special Operators
h. Conditional / Ternary Operator
ARITHMETIC OPERATORS
Arithmetic Operators are used to performing mathematical calculations like addition (+), subtraction (-),
multiplication (*), division (/) and modulus (%).
Symbol Description Syntax
Relational operators are used to comparing two quantities or values. These are used with
the if condition evaluator. The logic is :
if (condition true)
{
Execute statements in this block
}
else // if not true
{
Execute statements in this block
}
LOGICAL OPERATORS
C provides three logical operators when we test more than one condition to make
decisions. These are:
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming,
bitwise OR operator is denoted by |.
Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by <<.
If a=21 which is 10101 in Binary Form
if “a is left-shifted by 1” i.e a = a << 1
INCREMENT AND DECREMENT OPERATORS
The increment ( ++ ) and decrement (- - ) operators in C are unary operators for incrementing and
decrementing the numeric values by 1 respectively
Increment Operator in C
The increment operator ( ++ ) is used to increment the value of a variable in an expression by 1
Increment Operator can be used in two ways
1. Pre-Increment: ++ m
2. Post-Increment: m++
Decrement Operator in C
The decrement operator ( - -)is used to decrement the value of a variable in an expression by 1
decrement Operator can be used in two ways
3. Pre-decrement: ++ m
4. Post-decrement: m++
Increment Operator decrement Operator
If a= 10 If a= 10
X=++a X=- -a
X=a++ X=a - -
Pre Increment Operation => x=++a Pre decrement Operation => x=- -a
a = 11 a=9
x = 11 x=9
Post Increment Operation=> x=a- -
Post Increment Operation=> x=a++
a = 10
a = 10
x=9
x = 11
ASSIGNMENT OPERATORS
Assignment operators applied to assign the result of an expression to a variable. C has a collection of
shorthand assignment operators
SPECIAL OPERATORS
C supports some special operators
#include <stdio.h>
void main()
{
int i=10;
printf("integer: %d\n",
sizeof(i));
getchar();
}
CONDITIONAL OPERATOR
CONDITIONAL OPERATOR is called as ternary operator which is the conditional
operator to construct conditional expressions.
?: is the conditional operator
Syntax:
Example:
int a=30,b=20,c;
c=a>b?a:b;
OPERATORS PRECEDENCE, EVALUATION OF EXPRESSIONS,
MATHEMATICAL FUNCTIONS
OPERATORS PRECEDENCE IN C
When more than one arithmetic operator appears in an arithmetic expression then it is
important to note which operator is evaluated first, which is operated second and
which operator has the last priority. The order in which the arithmetic operators are
executed is called the hierarchy of operations.
C operators in order of precedence (highest to lowest). Their associatively indicates in
what order operators of equal precedence in an expression are applied.
Example:
k=3/2*4+3/8+3
Stepwise evaluation of this expression is shown below:
k=3/2*4+3/8+3
The “math.h” header file supports all the mathematical related functions in C
language.
All the arithmetic functions used in C language use math.h header file
Function Description