Operators in C
Operators are symbols that can perform different operations on operands.
Types of Operators:- Arithmetic Operators
For Example: int num1=5, num2=2;
Operator Name Example Result + Addition num1+num2 7 - Subtraction num1-num2 3 * Multiplication num1*num2 10 / Division num1/num2 2 % Modulo (Remainder) num1%num2 1 - Relational Operators
For Example: int num1=5, num2=2;
Operator Name Example Result < Less than num1<num2 0 > Greater than num1>num2 1 <= Less than or equal to num1<=num2 0 >= Greater than or equal to num1>=num2 1 == Equal to num1==num2 0 != Not equal to num1!=num2 1 - Logical Operators
For Example: int num1=5, num2=2;
Operator Name Example Result && Logical AND (num1>0) && (num2>0) 1 || Logical OR (num1>0) || (num2<0) 1 ! Logical NOT !(num1>0) 0 - Bit-wise Operators
For Example: int num1=5, num2=2;
Operator Name Example Result & Bitwise AND num1&num2 0 | Bitwise OR num1|num2 7 ^ Bitwise XOR num1^num2 7 << Left Shift num1<<1 10 >> Right Shift num1>>1 2 ~ Bitwise NOT ~num1 -6 - Assignment Operators
For Example: int num1=5, num2=2;
Operator Example Result
(Value of num1)= num1=5 5 += num1+=num2 7 -= num1-=num2 3 *= num1*=num2 10 /= num1/=num2 2 %= num1%=num2 1 - Increment and Decrement Operators
For Example: int num1=5;
Operator Example Result ++ num1++ 6 -- num1-- 4 Increment and decrement operators are used to increase or decrease the value of a variable by 1. These operators can be used in both prefix and postfix forms.
Pre-increment or pre-decrement means increment or decrement the value of the variable before using it in an expression.
For Example:#include <stdio.h>Output:
int main() {
int num1 = 5, num2 = 2;
int preIncrement = ++num1;
int preDecrement = --num2;
printf("num1 value: %d, Pre-incremented value: %d\n", num1, preIncrement);
printf("num2 value: %d, Pre-decremented value: %d\n", num2, preDecrement);
return 0;
}
num1 value: 6, Pre-incremented value: 6
num2 value: 1, Pre-decremented value: 1
Post-increment or post-decrement means use the current value of the variable in an expression and then increment or decrement it.
For Example:#include <stdio.h>Output:
int main() {
int num1 = 5, num2 = 2;
int postIncrement = num1++;
int postDecrement = num2--;
printf("num1 value: %d, Post-incremented value: %d\n", num1, postIncrement);
printf("num2 value: %d, Post-decremented value: %d\n", num2, postDecrement);
return 0;
}
num1 value: 6, Post-incremented value: 5
num2 value: 1, Post-decremented value: 2
- Ternary Operator
Ternary operator is a conditional operator that evaluates a condition and returns one of two values based on the result.
Syntax:condition ? value_if_true : value_if_false;For Example: int num1=5, num2=2;Operator Example Result ?: (num1>num2)?num1:num2 5 - Miscellaneous Operators
These operators are used for various purposes in C programming.
Operator Description , Comma operator sizeof Returns the size of a variable or data type
Common Interview Questions
- Find sum of two numbers without using '+' operator?
- Find largest number without using relational operators?
- How to check given number is even or odd without using '%' operator?
- How to double a given integer without using '*' or '+' operators?