3.operators - Expressions - TypeConversions V 4.0
3.operators - Expressions - TypeConversions V 4.0
Operators
Operators in C is a special symbol used to perform the mathematical and logical computations.
Operators
Types of Operators
Int a = 5;
++a ; // Valid : Increments the variable 'a'
7++; // Invalid : Cannot increment a constant
(a+1)++; // Invalid : Cannot increment an expression
Limitation 2:
Int x = 5;
X=x++ + ++x; // Undefined behaviour : The results may vary depends upon the compiler
4. Cannot be Used with Non-Scalar Data Types: Increment operators can only be applied to scalar data
types (i.e., integers, floating-point numbers, pointers). You cannot use increment operators on arrays,
structures, or functions.
Struct myStruct {
Int a;
Int arr[5] = {1,2,3,4,5};
};
++arr; // Invalid: Cannot increment array
Struct myStruct s;
s++; // Invalid: Cannot increment a
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Structure
Operators
• The relational operators in C are used for the comparison of the two operands.
• All these operators are binary operators that return true or false values as the result of comparison.
num1 = 8;num2 = 8;
printf(“%d”,(num2 <= num1));
If the value of the left operand is less than or
Less than or 1
<= equal to the value of the right operand, then is
equal to num1 = 8;num2 = 4;
True otherwise it is False
printf(“%d”,(num1 <= num2));
0
num1 = 4;num2 = 4;
If the value of the left-side operand is greater printf(“%d”,(num2 >= num1));
than or equal to the value of the right-side 1
>= Greater than or
operand, then condition is True, otherwise it is num1 = 3;num2 = 4;
equal to
False printf(“%d”,(num1 >= num2));
0
• The result of the operation of a logical operator is a Boolean value either true or false.
num1 = 8; num1 = 0
Returns true if the operand is
! Logical NOT printf(“%d”,!num1); printf(“%d”,!num1);
false.
0 1
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators
• The conditional operator, often referred to as the "ternary operator“, takes three operands: a condition,
a value to return if the condition is true, and a value to return if the condition is false.
• Syntax:
• If it is non-zero (true), then the expression expr2 is evaluated, and that is the value of the conditional
expression. Otherwise, expr3 is evaluated, and that is the value. Only one of expr2 and expr3 is
evaluated.
Example:
• The left side operand of the assignment operator is a variable and the right side operand of the
• The value on the right side must be of the same data type as the variable on the left side otherwise the
• The assignment operators can be combined with some other operators in C to provide multiple
operations using single operator. These operators are called compound operators or short-hand
operators
num1 = 8;num1 += 2;
It adds the value of right-side operand to the left-side
printf(“%d”,num1);
+= operand and assigns the result to the left-side operand
10
Note: x += y is same as x = x + y
It subtracts the value of right-side operand from the left- num1 = 8;num1 -= 2;
-= side operand and assigns the result to left-side operand printf(“%d”,num1);
Note: x -= y is same as x = x - y 6
It divides the value of right-side operand with the value of left- num1 = 8;num1 /= 2;
/= side operand and assigns the result to left-side operand printf(“%d”,num1);
Note: x /= y is same as x = x / y 4
num1 = 8;num1 %= 2;
It performs modulus operation using two operands and
printf(“%d”,num1);
%= assigns the result to left-side operand
0
Note: x %= y is same as x = x % y
It performs bitwise AND operation using two operands and num1 = 8;num1 &= 2;
&= assigns the result to left-side operand printf(“%d”,num1);
Note: x &= y is same as x = x & y 0
num1 = 8;num1 ^= 2;
It performs bitwise OR operation using two operands
printf(“%d”,num1);
^= and assigns the result to left-side operand
10
Note: x ^= y is same as x = x | y
sizeof Operator
• The sizeof operator in C is a compile-time operator that is used to determine the size (in bytes) of a
• It evaluates to the size, in bytes, of the operand. The operand can be a variable, a data type, or an
expression.
• Syntax
sizeof(variable/datatype/expression)
• The comma operator can be 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
✓ First assigns the value 10 to x, then assigns 5 to y, and finally assigns 15 to value.
✓ Since comma operator has the lowest precedence of all operators, the parentheses are necessary.
• Member operators are used to reference individual member of structures and unions.
• The dot operator is used to access members of a structure or union.
• The arrow operator is used to access members of a structure or union through pointer. an
object.
Example: Example:
struct Point { struct Point {
int x; int x;
int y; int y;
}; };
struct Point p1; struct Point *p2 = & p1;
p1.x = 10; // Using dot operator to access and set P1->x = 10; // Using dot operator to access and set
value value
p1.y = 20; P1->y = 20;
Cast Operator
• The cast operator is used to convert a variable from one data type to another explicitly.
• It is typically used when you need to perform type conversions, especially between incompatible
data types, or when you want to control how a value is treated during operations.
• Syntax
(type)expression
• ct.
Example: float num = 3.14;
int x = (int) num //Casts float to int, truncating the decimal part (result: 3)
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators
Address-Of (&)
• The Address-Of Operator (&) is used to obtain the memory address of a variable. When applied to
• Syntax
&variable
• Used to access the value stored at the memory address pointed to by a pointer.
• In other words, it "dereferences" the pointer to retrieve or modify the actual data at that memory
address.
• Syntax
*variable
• Bitwise operators are used to perform operations at the bit level and help to manipulate data at the
• These can be done by first converting a decimal value to its binary form. This binary form is nothing
• Bitwise operators perform operations on these bits that has constant time complexity.
• Bitwise operators operate on integers and characters but not on data types float or double.
• Data compression
• Encryption algorithms
• Optimization
• The logical operators work with Boolean data and return a Boolean value, i.e. True or False.
• The bitwise operators in C work with integers, i.e. they take integer inputs, manipulate with their bit
• The bitwise AND, and OR use ‘&’ and ‘|’ as their operators, while the logical AND, and OR use ‘&&’
Example
Operator Operation Description
(a=5, b=6)
Performs bit-by-bit AND operation and
& Bitwise AND a&b:4
returns the result.
Example
Operator Operation Description
(a=5, b=6)
Bitwise First Flips all the set and unset bits on the
~ ~a : -6
Complement number.
Example
int result, X = 5, Y = 6; Output:
X :510 => (101)2 Bitwise AND:(X & Y): 4
result = X & Y;
Y :610 => (110)2
printf(“Bitwise AND:(X & Y): %d", result);
X&Y : (101)2 & (110)2 => (100)2 (which is 410 )
Example
int result, X = 5, Y = 6; Output:
X :510 => (101)2 Bitwise OR:(X | Y): 4
result = X | Y;
Y :610 => (110)2
printf(“Bitwise OR:(X | Y): %d", result);
X | Y :(101)2 | (110)2 => (111)2 (which is 710 )
Example
int result, X = 5, Y = 6; Output:
X :510 => (101)2 Bitwise XOR:(X ^ Y): 3
result = X ^ Y;
Y :610 => (110)2
printf(“Bitwise XOR:(X ^ Y): %d", result);
X ^ Y :(101)2 ^ (110)2 => (011)2 (which is 310 )
• Bitwise NOT is an unary operator that flips the bits of the Truth table for Bitwise NOT operator
X ~X
number i.e., if the ith bit is 0, it will change it to 1 and vice
0 1
versa. 1 0
number.
• Here, logically ~5 = 2 but, the compiler returns the 2’s complement of the input value.
• In C, integers are typically stored in two's complement form, which means that negative numbers
are represented in a binary form where the most significant bit (MSB) is the sign bit (1 for
• When you perform bitwise NOT (~) on a positive integer, you get a result in two's complement
form.
• The value of X is 5, which is represented as 0000 0101 in binary (assuming 8-bit integers for
simplicity).
• When you apply bitwise NOT, you flip each bit: Bitwise NOT of 0000 0101 is 1111 1010.
• Now, if you interpret 1111 1010 as a two's complement signed integer, it represents -6. That's why
• If you were expecting a positive result, you may want to use an unsigned integer type.
#include <stdio.h>
int main() {
unsigned int X = 5;
unsigned int result = ~X;
printf("Bitwise NOT of %u is %u\n", X, result);
return 0; Output:
Bitwise NOT of 5 is 4294967290
}
• The result you're seeing, "4294967290," is the decimal representation of the bitwise NOT operation on
• The left shift operator (<<) is a bitwise operator that shifts the bits of a binary number to the left by
• In other words, it multiplies the number by 2 raised to the power of the shift count.
– Each bit in the binary representation of the number is shifted to the left by the specified number of
positions.
– The leftmost bits that are shifted out (if any) are discarded.
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators
• The binary representation of 5 is 0000 0101, and after left-shifting by 2 positions, it becomes 0001
• The right shift operator (>>) is a bitwise operator that shifts the bits of a binary number to the right
by a specified number of positions.
• In other words, it divides the number by 2 raised to the power of the shift count, discarding the
remainder.
– Each bit in the binary representation of the number is shifted to the right by the specified number
of positions.
– The vacant positions on the left are filled with the sign bit (for signed integers) or with zeros (for
unsigned integers).
– The rightmost bits that are shifted out (if any) are discarded.
Example:
#include <stdio.h>
Original number : 0000 0101 (5 in decimal)
int main() { Right shift by 2 (5 >>2) : 0000 0001 (1 in decimal)
int X = 5;
int result = X >> 2;
printf("Right shift of %d by 2 is %d\n", X, result);
return 0; Output:
} Right shift of 5 by 2 is 1
• The binary representation of 5 is 0000 0101, and after right-shifting by 2 positions, it becomes
• Bitwise operators are commonly employed in low-level programming for direct manipulation of
bits.
• They provide highly efficient operations, making them especially valuable in performance-critical
• Bit Masking is a technique used in programming to manipulate specific bits in a number using
bitwise operations.
• It involves applying a bitwise mask (a binary number composed of 0s and 1s) to select, modify,
• The mask allows you to isolate, set, or clear specific bits without altering the other bits in the
number.
• Here we have to decide what should be the Mask_value, and then take that Mask_value
and do a bitwisewise AND(&) operation with a given data to get the output.
[Output] 000000002
Data is masked with 1 to test LSB of
data
Testing of bit
• Testing a bit refers to checking the value of a specific bit within a binary representation of a number
or data.
• In the context of programming and bitwise operations, testing a bit typically involves checking whether a
Write a program to find out whether a user entered a number is even or odd. Print an appropriate message on
the console. (Use testing of bits logic)
Setting of bit
• To set a particular bit to 1, you can use the OR (|) operator with a mask that has 1 in the position of
the bit you want to set.
#include <stdio.h>
int main() {
int n = 8; // Binary: 00001000
int mask = 1 << 2; // Binary: 00000100 (mask to set the 3rd bit)
n = n | mask; //Binary : 00001100 ( 3rd bit is set)
prinftf(“%d”,n);
Output:
return 0;
} 12
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators
Clearing of bit
• To clear a specific bit (set it to 0), you can use the AND (&) operator with the NOT (~) of a mask that has 1 at the
bit position you want to clear.
#include <stdio.h>
int main() {
int n = 12; // Binary: 00001100
int mask = ~(1 << 2); // Binary: ~(0000100) => 1111011(mask to clear the 3rd bit)
n = n & mask; //Binary : 00001000 ( 3rd bit is cleared)
prinftf(“%d”,n);
Output:
return 0;
}
8
Toggling of bit
• To toggle a specific bit (change its value from 0 to 1 or 1 to 0), use the XOR (^) operator with a mask that has 1
at the bit you want to toggle
#include <stdio.h>
int main() {
int n = 5; // Binary: 00000101
int mask = 1 << 1; // Binary: 00000010 (mask to toggle the 2nd bit)
n = n ^ mask; //Binary : 00000111 ( 2nd bit is toggled)
prinftf(“%d”,n);
return 0; Output:
} 7
Expressions
• An expression in C is any valid combination of tokens like variables, constants and operators.
• An expression may consist of one or more operands, and zero or more operators to produce a
value.
Examples:
a+b*c
(a*b)/(c+d)
++a&&b++||c
Expressions
Expression Evaluation:
specified operations, following rules for operator precedence, associativity, and data types.
not an expression.
# Valid Expressions
(i) 100 (iv) 3.0 + 3.14
(ii) num (v) 23/3 -5 * 7(14 -2)
(iii) num – 20.4 (vi) "Global" + "Citizen"
Expressions
1. Operator Precedence
• It determines which operator is performed first in an expression with more than one operators
2. Operators Associativity
• When two operators of the same precedence appear in an expression, associativity determines
the order of evaluation. Associativity can be either Left to Right or Right to Left.
Postfix increment/decrement (a is a
a++ , a– left-to-right
variable)
Prefix increment/decrement (a is a
++a , –a right-to-left
variable)
* Dereference right-to-left
12 || Logical OR left-to-right
= Assignment right-to-left
Bitwise exclusive/inclusive OR
^= , |= right-to-left
assignment
Expressions – Example 1
Expression:15.0 / 4 + (8 + 3.0)
Solution:
= 15.0 / 4 + (8.0 + 3.0) (Expression within parenthesis is evaluated first)
=15.0 / 4.0 + 11.0 (Performs division operation)
=3.75 + 11.0
=14.75
Expressions – Example 2
Expression: 17 – 8 /4 * 2 + 3 - ++5
Solution:
= 17- 8 / 4 * 2 + 3 - ++5 (Performs Pre increment operation )
= 17- 8 / 4 * 2 + 3 - 6 (Performs division operation – left to right Associativity)
= 17- 2 * 2 + 3 – 6 (Performs multiplication operation – left to right Associativity)
= 17- 4 + 3 – 6 (Performs subtraction operation – left to right Associativity)
= 13 + 3 – 6 (Performs addition operation – left to right Associativity)
= 16 - 6
= 10
Expressions – Example 3
Expression: m = i + ( j = 2 + K) where i = 4, j = 2, k = 6, a = 2
Solution:
m = 4 + ( j = 2 + 6);
m = 4 + ( j = 8);
m=4+8;
m = 12
Type Conversion
• The type conversion is only performed to those data types where conversion is possible, by a
compiler.
• Type conversion is done at compile time and it is also called widening conversion because the
destination data type can’t be smaller than the source data type.
• Implicit Conversion
• Explicit Conversion
• All the data types of the variables are upgraded to the data type
bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long
double
• It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly
converted to unsigned), and overflow can occur (when long is implicitly converted to float).
#include <stdio.h>
int main() {
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
Output:
}
x = 107, z = 108.000000
data type.
Syntax:
(type) expression
converted.
return 0;
} Output:
sum = 2
Quiz
1. What will be output if you execute following c code?
#include<stdio.h>
int main()
{
printf("%d\t",sizeof(2.5));
printf("%d\t",sizeof(2));
printf("%d",sizeof('A'));
return 0;
}
a) 8 4 4 b) 8 4 2
c) 4 4 1 d) 8 4 1
a) 8 4 4
Quiz
2. What will be output if you execute following c code?
#include<stdio.h>
int main(){
int num = - -2;
printf("num = %d", num);
return 0;
}
a) -2 b) Compilation Error
c) Runtime error d) 2
d) 2
Quiz
3. What will be output if you execute following c code?
#include<stdio.h>
int main()
{
int x=3, y=4, z=4;
printf("%d", (z>=y>=x?100:200));
return 0;
}
a) 100 b) 200
c) 0 d) 1
b) 200
Quiz
4. What will be output if you execute following c code?
#include<stdio.h>
int main()
{
int a=30, b=40, x;
x=(a!=10) && (b=50);
printf("%d",x);
return 0;
}
a) 1 b) 2
c) 3 d) 4
a) 1
Quiz
5. What will be output if you execute following c code?
#include<stdio.h>
int main()
{
int i;
(i=8)+=1;
printf("%d",i);
return 0;
}
a) 9 b) 10
c) 32 d) Compilation Error
d) Compilation Error
Quiz
6. What will be output if you execute following c code?
#include<stdio.h>
int main()
{
int a = 2,b = 5;
a = a^b;
b = b^a;
printf("%d %d",a,b);
return 0;
}
a) 5 2 b) 2 5
c) 7 7 d) 7 2
d) 7 2
Quiz
7. What will be output if you execute following code?
#include <stdio.h>
int main() {
int x = 10;
int y = (x++, x++, x++);
printf("%d %d\n", x, y);
return 0;
}
a) 13 12 b) 13 13
d) 13 12
Quiz
8. For a given integer, which of the following operators can be
used to “set” and “reset” a particular bit respectively?
a) | and &
Quiz
9. What will be output if you execute following code?
#include <stdio.h>
int main()
{
printf("%d", 1 << 2 + 3 << 4);
return 0;
}
a) 112 b) 52
c) 512 d) 0
c) 512
Quiz
10. What will be output if you execute following code?
#include <stdio.h>
int main(void)
{
int a = 9;
float x;
x = a / 2;
printf("%f", x);
return 0;
}
a) 4.000000 b) 0
c) 4 d) 4.500000
a) 4.000000
Quiz
11. What will be output if you execute following code?
#include <stdio.h>
int main()
{
int i = -3,j = 2,k= 0 ,m;
m = ++i && ++j || ++k ;
printf("%d %d %d %d",i,j,k,m);
return 0;
}
-2 3 0 1
Quiz
12. What will be output if you execute following code?
#include <stdio.h>
int main()
{
int i = -3,j = 2,k= 0 ,m;
m = ++i || ++j || ++k ;
printf("%d %d %d %d",i,j,k,m);
return 0;
}
-2 2 0 1
Quiz
13. What will be output if you execute following code?
#include <stdio.h>
int main()
{
int i = -3,j = 2,k= 0 ,m;
m = ++i && ++j && ++k ;
printf("%d %d %d %d",i,j,k,m);
return 0;
}
-2 3 1 1
Quiz
14. What will be output if you execute following code?
#include<stdio.h>
void main(){
int i = 2;
int j = i + (1,2,3,4,5);
printf("%d",j);
}