Operators
Operators
Operators
An operator is a symbol that tells the compiler to perform a specific
operation on data or variables. Operators are used to manipulate variables and
values in various ways, like performing arithmetic, comparisons, or logical
operations.
Types of Operators
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Bitwise Operators
Conditional Operator
Special Operator
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on
variables and data.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation
#include <stdio.h>
void main() {
int a = 9,b = 4, c; Output
c = a+b; //Addition a+b = 13
printf("a+b = %d \n",c); a-b = 5
c = a-b;//Subtraction a*b = 36
printf("a-b = %d \n",c); a/b = 2
c = a*b; //Multiplication Remainder when a divided by b=1
printf("a*b = %d \n",c);
c = a/b; //Division
printf("a/b = %d \n",c);
c = a%b; //Modulus
printf("Remainder when a divided by b = %d \n",c);
}
C Dennis Ritchie
Increment and Decrement Operators
++ increases the value of the operand by 1
-- decreases it by 1
These operators can be applied in two ways:
· Prefix (before the variable): ++x or --x
· Postfix (after the variable): x++ or x--
1. Prefix Increment/Decrement (++x, --x)
· Increment (++x): Increases the value of the variable first and then uses
the updated value in the expression.
· Decrement (--x): Decreases the value of the variable first and then uses
the updated value in the expression.
Example:
int x = 5;
int y = ++x; // x is incremented to 6, and then y is assigned the value 6.
printf("x = %d, y = %d\n", x, y); // Output: x = 6, y = 6
2. Postfix Increment/Decrement (x++, x--)
· Increment (x++): The variable is used first in the expression and then
incremented.
· Decrement (x--): The variable is used first in the expression and then
decremented.
Example:
int x = 5;
int y = x++; // y is assigned the value 5, and then x is incremented to 6.
printf("x = %d, y = %d\n", x, y); // Output: x = 6, y = 5
#include <stdio.h>
void main() {
Output
int a = 10, b = 100;
++a = 11
float c = 10.5, d = 100.5;
--b = 99
printf("++a = %d \n", ++a);
++c = 11.500000
printf("--b = %d \n", --b);
--d = 99.500000
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
}
C Dennis Ritchie
Assignment Operators
a=3
a+=10
can be written has a=3+10
a = a + 10
a=13
Assignment Addition
#include <stdio.h>
void main()
{
int a = 5, c=10;
c = a;
printf("c = %d\n", c);
c += a;
printf("c = %d\n", c);
c -= a;
printf("c = %d\n", c);
c *= a;
printf("c = %d\n", c);
c /= a;
printf("c = %d\n", c);
c %= a;
printf("c = %d\n", c);
}
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
C Dennis Ritchie
Relational Operators
A relational operator is used to check the relationship between two operands
If the relation is true, it returns 1 whereas if the relation is false, it returns 0
Operator Meaning
== Is Equal to
!= Not equal to
> Greater than
< Lesser than
>= Greather than or equal to
<= Lesser than or equal to
Operator Examples
5==5 It is evaluated to 1
==
5==2 It is evaluated to 0
!= 5!=5 It is evaluated to 0
5!=2 It is evaluated to 1
#include <stdio.h>
void main()
{
int a = 5, b = 5; 5 == 5 is 1
printf("%d == %d is %d \n", a, b, a == b); 5 > 5 is 0
printf("%d > %d is %d \n", a, b, a > b); 5 < 5 is 0
printf("%d < %d is %d \n", a, b, a < b); 5 != 5 is 0
printf("%d != %d is %d \n", a, b, a != b); 5 >= 5 is 1
printf("%d >= %d is %d \n", a, b, a >= b); 5 <= 5 is 1
printf("%d <= %d is %d \n", a, b, a <= b);
}
C Dennis Ritchie
Logical Operators
Logical operators are used to check whether an expression is true or false.
If the expression is true, it returns 1 whereas if the expression is false,
it returns 0.
Operator Meaning
|| Logical OR
! Logical NOT
AND OR NOT
a b a&&b a b a||b a !a
0 0 0 0 0 0 0 1
0 1 0 0 1 1 1 0
1 0 0 1 0 1
1 1 1 1 1 1
#include <stdio.h>
void main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
| Binary OR
^ Binary XOR
~ Binary NOT
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
C Dennis Ritchie
#include <stdio.h>
void main() {
int a = 5, b = 3;
Output:
a&b=1
a|b=7
a^b=6
~a = -6
a << 1 = 10
a >> 1 = 2
C Dennis Ritchie
Conditional Operator
The conditional operator (also known as the ternary operator) in C is a
shorthand way to write an if-else statement. It evaluates a condition and returns
one of two values depending on whether the condition is true or false.
Syntax:
Example:
How it works:
· Condition: (a > b) checks if a is greater than b.
· If true, it assigns a to max.
· If false, it assigns b to max.
· In this case, since a is 10 and b is 20, max will be 20.
void main() {
int number = 5,result;
result = (number % 2 == 0) ? "Even" : "Odd";
printf("The number %d is %s.\n", number, result);
}
C Dennis Ritchie
Special Operator
1. Sizeof Operator
2. Comma Operator
· Example:
int x = (1, 2, 3); // x will be assigned the value 3
printf("%d\n", x); // Output: 3
#include <stdio.h>
void main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Output
o Example:
int a = 5;
float b = a; // Implicitly converts int to float
printf("%f\n", b); // Output: 5.000000
o Example:
float x = 5.7;
int y = (int)x; // Explicitly converts float to int
printf("%d\n", y); // Output: 5
#include <stdio.h>
int main() {
int a = 10;//implicit type casting
float b = a; // int to float (automatic)
printf("Implicit casting: %f\n", b); // Output: 10.000000
float c = 9.99; // explicit type casting
int d = (int)c; // float to int (manual) Output:
printf("Explicit casting: %d\n", d); // Output: 9 Implicit casting: 10.000000
Explicit casting: 9
return 0;
}
C Dennis Ritchie
Associativity
It refers to the order in which operators of the same precedence are
evaluated in an expression. It determines how expressions are grouped when
there are multiple operators of the same precedence.
Types of Associativity
1. Left-to-Right Associativity:
o Operators are evaluated from left to right.
o Most arithmetic operators, comparison operators, and logical
operators have left-to-right associativity.
o Example:
int a = 10, b = 5, c = 2;
int result = a - b + c; // Evaluated as (a - b) + c
// Step 1: a - b = 5
// Step 2: 5 + c = 7
printf("%d\n", result); // Output: 7
2. Right-to-Left Associativity:
o Operators are evaluated from right to left.
o The assignment operator (=) and the conditional operator (?:) have
right-to-left associativity.
o Example:
int x, y, z;
x = y = z = 5; // Evaluated as x = (y = (z = 5))
// Step 1: z = 5 (z is now 5)
// Step 2: y = 5 (y is now 5)
// Step 3: x = 5 (x is now 5)
printf("%d %d %d\n", x, y, z); // Output: 5 5 5
+, -, *, /, % Left-to-right
==, !=, <, >, <=, >= Left-to-right
= (assignment) Right-to-left
?: (conditional) Right-to-left