Operators in C are special symbols used to perform operations on variables and values. C provides a wide variety of operators for performing arithmetic, logical, relational, bitwise, and other operations. There are many types of Operators.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.
Example:
#include <stdio.h>
int main() {
int a = 20;
int b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n",a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}
Output:
Subtraction: 15
Multiplication: 100
Division: 4
Modulus: 0
2. Relational Operators
Relational operators are used to compare two values. They return true (1) if the condition is met, or false (0) if it is not.
Example:
#include <stdio.h>
int main() {
int a = 20;
int b = 5;
printf("%d\n", a == b); // Output: false
printf("%d\n", a != b); // Output: true
printf("%d\n", a > b); // Output: true
printf("%d\n", a < b); // Output: false
printf("%d\n", a >= b); // Output: true
printf("%d\n", a <= b); // Output: false
return 0;
}
Output:
1
1
0
1
0
3. Logical Operators
Logical operators are used to perform logical operations on conditions. They return true (1) or false (0) based on the result of the operation.
Example:
#include <stdio.h>
#include <stdbool.h>
int main() {
bool a = true;
bool b = false;
printf("%d\n", a && b); // Output: False (Both must be true)
printf("%d\n", a || b); // Output: True (At least one is true)
printf("%d\n", !a); // Output: False (Reverses the value)
return 0;
}
Output:
1
0
4. Assignment Operators
Assignment operators are used to assign values to variables.
Example:
#include <stdio.h>
int main() {
int a = 20;
a += 10; // a = a + 10 => num = 30
a -= 5; // a = a - 5 => a = 25
a *= 2; // a = a * 2 => a = 50
a /= 5; // a = a / 5 => a = 10
printf("%d\n",a); // Output: 10
return 0;
}
Output:
5. Ternary Operator
The ternary operator is a shorthand version of the if-else statement. It has the following form.
Syntax:
condition ? expression_if_true : expression_if_false
Example:
#include <stdio.h>
int main() {
int age = 19;
char *result = (age >= 18) ? "Adult": "Not Adult";
printf("%s",result); // Output: Adult
return 0;
}
Explanation: I changed the variable result to be a char* (pointer to char) because you are dealing with strings, not individual characters.
Output:
6. Unary Operators
These operators operate on a single operand.
Example:
#include <stdio.h>
int main() {
int x = 20;
printf("%d\n",++x); // Output: 21 (Pre-increment)
printf("%d\n",x++); // Output: 21 (Post-increment)
printf("%d",x); // Output: 22 (x is now 22)
return 0;
}
Output:
21
22
C Operators – Interview Questions
Q 1: What are operators in C?
Q 2: Name different types of operators in C.
Q 3: What is the difference between = and ==?
Q 4: What are logical operators in C?
Q 5: What is the use of the ternary operator?
C Operators – Objective Questions (MCQs)
Q1. Which of the following is the assignment operator in C?
Q2. What will be the output of the expression 5 + 2 * 3 in C?
Q3. Which operator is used to find the remainder in C?
Q4. What is the result of the expression a++ in C?
Q5. Which of the following is a logical AND operator in C?