Operators
Operators
Types of Operators.
Arithmetic Operators.
Relational Operators.
Logical Operators.
Bitwise Operators.
Assignment Operators.
1. Arithmetic Operators:
Used to perform basic mathematical operations.
Examples:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus, remainder)
Examples:
== (equal to)
OPERATORS 1
> (greater than)
3. Logical Operators:
Used to combine boolean expressions.
Examples:
|| (logical OR)
! (logical NOT)
4. Bitwise Operators:
Operate at the bit level on binary numbers.
Examples:
| (bitwise OR)
^ (bitwise XOR)
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
5. Assignment Operators:
OPERATORS 2
Used to assign values to variables.
Examples:
= (assignment)
What is Unary?
A unary operator operates on one operand.
Examples:
++ (increment)
-- (decrement)
int x = 5;
x++; // this will increment value of x by one (post
printf("%d",x);
int x = 5;
x--; // this will decrement value of x by one (post
printf("%d",x);
int x = 5;
++x; // this will decrement value of x by one (pre in
OPERATORS 3
printf("%d",x);
int x = 5;
--x; // this will decrement value of x by one (pre de
printf("%d",x);
What is Binary?
A binary operator operates on two operands.
It's used for arithmetic, comparisons, logic, and assignments between two
values.
Examples:
+ (addition)
- (subtraction)
== (equality check)
int a = 10;
int b = 20;
int c = a+b; // this operator will add a and b values
printf("%d",c);
int a = 10;
int b = 20;
int c = b-a; // this operator will subtract value of a from b
printf("%d",c);
int a = 10;
int b = 10;
if(a == b){ // this is check equallity with two operands
OPERATORS 4
printf("Equal");
}
if(5 < 7 && 7 > 5){ // this is check true both sides with &&
printf("True");
}
What is ternary?
A ternary operator operates on three operands.
statements.
int a = 10;
int b = 20;
int max = (a > b) ? a : b; // Ternary operator, returns 20
Operator Precedence In c
Operator
Precedence Description Associativity
. Dot Operator
OPERATORS 5
* Dereference Operator
12 || Logical OR Left-to-Right
14 = Assignment Right-to-Left
Practice Questions
2. What are arithmetic operators, and why are they used in programming?
OPERATORS 6
3. Write a program to perform addition using arithmetic operators.
12. Write a C program using the ternary operator to find the maximum of two
numbers.
OPERATORS 7