Chapter -1 Operatorsin C Programming
Operators in C are special symbols that perform specific operations on one or
more operands.
They are the building blocks of expressions and are essential for manipulating
data in C programs.
3.
Classification of Operators
Basedon Operations Based on Operands
Arithmetic Operators
Relational Operators
Increment / Decrement
Operators
Conditional Operator
Unary Operators
Binary Operators
Ternary Operator
Logical Operators
Bitwise Operators
Special Operators
4.
Arithmetic Operators
Operation OperatorSyntax Result Example (a=10,b=5)
Multiply * a*b Result=a*b 50
Divide / a/b Result=a/b 2
Addition + a+b Result=a+b 15
Subtraction - a-b Result=a-b 5
Modulus % a%b Result=a%b 0
Arithmetic operators can be applied to any integer/ floating-point number.
The operator % gives reminder of as integer division.
Note- Modulus operator can be applied only to integer operands.
5.
Relational Operators
Also calledas comparison operator, evaluates the relationship between two values.
It returns true or false based on the condition between the operands.
Operation Operator Syntax Example (a=10,b=5)
Greater Than > a>b True
Less Than < a<b False
Greater than or equal to >= a>=b True
Less than or equal to <= a<=b False
equal to == a==b False
Not equal to != a!=b True
6.
Logical Operators
• Clanguage supports three logical operators:
1.Logical AND (&&)
2.Logical OR (||)
3.Logical NOT (!)
• Logical NOT (!) has the highest priority.
• && and || has same priority (whichever comes first , will be evaluated first).
7.
1. Logical AND(&&) :
• Logical AND operator is a binary operator, which simultaneously evaluates
two values or relational expression.
• If both or one of the operands is false, then whole expression evaluates to false.
NOTE – Non- Zero number includes both +ve and –ve numbers.
Example-
OUTPUT-
8.
2. Logical OR(||) :
• Logical OR operator is a binary operator, which simultaneously evaluates
two values or relational expression.
• Returns a false value if both the operands are false.
Example -
OUTPUT-
9.
3. Logical NOT(!) :
• Logical NOT operator takes a single expression and negates the value
of the expression.
Example -
OUTPUT-
10.
Pre- Increment/ Pre-Decrement Operators
• Pre-Increment – First increment, then use it in expression.
• Pre- Decrement – First decrement, then use it in expression.
Post- Increment Operator Example -
Int a=5, b=0;
b= ++a;
a= a+1;
a = 5+1 = 6;
++a;
a=6+1 =7;
Dependent expression
(Look at Pre and Post)
Stand Alone Expression
(Priorities)
a
b
6
5 6 7
11.
Post- Increment/ Post-Decrement Operators
• Post- Increment – First use it in expression, then increment it.
• Post- Decrement – First use it in expression, then decrement it.
Post- Increment Operator Example -
Int a=5, b=0;
b= a++;
a= a+1;
a = 5+1 = 6;
a++;
a=6+1 =7;
Dependent expression
(Look at Pre and Post)
Stand Alone Expression
(Priorities) 5
5 6 7 a
b
12.
Bitwise Operators
& BitwiseAND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Shifts bits to the left side
>> Shifts bits to the right side
Performs operations at bit level
Left shift
• Leftshift operator shifts all bits towards left by certain number of specified
bits.
• Syntax: number << b ( b is number of bits to be shifted).
• Left shift operator is used to multiply the number by 2n
.
Let’s test your knowledge :
2<<1 = ?
4<<1 = ?
2<<3 = ?
-4<<1 = ?
You know what ?
2’s compliment is used to change the sign of a number
Right Shift
• Rightshift operator shifts all bits towards right by certain number of specified
bits.
• Syntax: number >> b ( b is number of bits to be shifted).
• Left shift operator is used to divide the number by 2n
.
Let’s test your knowledge :
2>>1 = ?
4>>1 = ?
2>>3 = ?
Conditional Operator
• Ifcondition is true, exp1 is evaluated. Otherwise, exp2 is evaluated.
• Syntax : condition ? exp1 : exp2
• Shorthand for simple if-else statements
• Example: Find greatest of three numbers,
Output:
22.
Assignment Operator
• Themost common assignment operator is =.
• Assigns the value in right side to the left side.
Operator Example
Same as
= a=b
a=b
+= a+=b
a=a+b
-= a-=b
a=a-b
*= a*=b
a=a*b
/= a/=b
a=a/b
%= a%=b
23.
Special Operator
• SpecialOperator includes two operators such as following
1. Comma operator : used to link related expressions together.
2. sizeof operator : Unary operator , used for finding the size of data type,
Constant, array, structure, etc. Output:
24.
Order of evaluationof operators
• An expression can contain several operators with equal precedence.
• When several such operators appear at the same level in an expression
evaluation proceeds according to the associativity of the operator, either
from right to left or from left to right.
if Statement
Ifthe condition is true the statements inside block will be executed.
If condition is false it skips the statements and continues with next
statements.
If there is only one statement in the if block then braces are not needed, if
no statements in if block then braces are compulsory.
Syntax: if(condition)
{
statements;
}
Example:
If-else Statement
Ifthe condition is true the statement1 is executed and if its false then
statement2 will be executed.
At least one statement will be executed.
Example:
if(condition)
{
statement1;
}
else {
statement2;
}
Next statement;
Syntax:
Nested if elseStatement
if else statement within one or more if else statement
Used when multiple conditions are to be used.
Continued
if(condition)
{
if(conditon1)
statement-1;
else
statement-2;
}
Syntax:
else
{
if(condition2)
statement-3;
else
statement-4 ;
}
Continuation
Continued
Else if ladder
This is used when multiple conditions are to be
checked on the same data, at least one statement will
be executed.
The else block at end is compulsory.
if(condition1)
statement-1;
else if(condition2)
statement-2;
Syntax:
Continued
else if(condition)
statement-3 ;
.
.
else
statement-N;
Continuation
Continued
switch statement
Simplifiedmultiway decision statement evaluating a single variable.
Compares the variable in the switch statement with each
subsequent case value.
Continued
break statement
Thesebreak statements are used when the control
has to be brought out of block
It can be used in the switch and the loop statements.
for(…….)
{
…..
if(condition)
{
break;
……..
}
}
………..
Example:
39.
continue statement
Continuestatement is used when the current iteration has to
be skipped in loops.
Used only inside the loops.
for(… : …. : ….)
{
…..
if(condition)
{
continue ;
……..
}
………..
}
Example:
40.
goto statement
gotostatements can be used for unconditional jump.
There are two types of goto statements,
1.Forward jump
2.Backward jump
Forward jump: Backward jump:
goto Label :
………..
…………..
……….
………
Label:
Label :
………..
…………..
……….
………
goto Label:
Continued
return statement
Itis used to return from the function.
When this statement is used it restores the address stored on stack.
Example :
int add(int x, int y)
{
int sum=x + y;
return sum;
}
int main()
{
int a=5,b=10
printf(“%d”, add(a , b));
return 0;
}
while loop
while(condition)
{
Statements;
Aux_var_update;
}
Syntax
Statement X
Updatethe
condition
expression
Statement Block
Next
Statement
Condition
False
True
The loop continues executing
as long as the condition is
true; once the condition fails,
it exits the loop.
You know ? It is also called as pretest loop or entry controlled loop.
Next_Statements
True
False
45.
The loopkeeps on executing till the condition is true; once
condition fails it comes out of loop.
The loop will be executed at least once even if the condition is
false as you are checking the condition at the end.
Semi colon is used to indicate the end of do while loop.
do while loop
Continued
46.
do while loop
do
{
Statements;
Aux_var_update;
}while(condition);
Syntax
Statement X
Statement Block
Update the
Condition
expression
Condition
Next Statement
true
false
Continuation
You know ?
It is also called exit control loop or post test loop.
47.
For Loop
Thisloop is most used loop, it has three parts one is initialization
where we can initialize
variables, Condition and update part.
Syntax
for( initialization : condition : Update)
{
Statements;
}
Statements;
1
2 3
When condition fails
Continued
4