Table of Contents
What are Operators in C?
Operators are special symbols that perform operations on one or more operands (variables or values).Example:
Here, + is the operator, while a and b are operands.int a = 5, b = 3;
int sum = a + b;
Types of Operators in C
C provides different types of operators based on functionality. Let’s break them down one by one.Arithmetic Operators
These operators are used to perform basic mathematical operations on numeric values.Common Arithmetic Operators:
- Addition (+): This operator adds two operands and returns their sum.
- Subtraction (-): This operator subtracts the second operand from the first operand.
- Multiplication (*): This operator multiplies two operands to produce a product.
- Division (/): This operator divides the first operand by the second operand and returns the quotient.
- Modulus (%): This operator returns the remainder when the first operand is divided by the second operand.
// C program to show arithmetic operators
#include stdio.h
// Main function
int main()
{
int a = 10, b = 3;
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:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
Relational Operators
These operators are used to compare two values and return a boolean result (1 for true, 0 for false).Common Relational Operators:
- Equal to (==): This operator checks whether both operands are equal.
- Not equal to (!=): This operator checks whether the operands are not equal.
- Greater than (): This operator checks whether the first operand is greater than the second operand.
- Less than (): This operator checks whether the first operand is less than the second operand.
- Greater than or equal to (=): This operator checks whether the first operand is greater than or equal to the second operand.
- Less than or equal to (=): This operator checks whether the first operand is less than or equal to the second operand.
// C program to show relational operators
#include stdio.h
// Main function
int main()
{
int a = 5, b = 10;
printf("%d\n", a b);
printf("%d\n", a b);
printf("%d\n", a == b);
return 0;
}
Output:
1
0
0
Logical Operators
These operators are used to combine or modify conditional expressions.Common Logical Operators:
- Logical AND (&&): This operator returns true only if both conditions are true.
- Logical OR (||): This operator returns true if at least one of the conditions is true.
- Logical NOT (!): This operator reverses the truth value of a condition.
// C program to show logical operators
#include stdio.h
// Main function
int main()
{
int a = 5, b = 10;
printf("%d\n", (a b && b 0));
printf("%d\n", (a b || b 0));
printf("%d\n", !(a b));
return 0;
}
Output:
1
1
0
Assignment Operators
These operators are used to assign values to variables and update them.Common Assignment Operators:
- = (Simple assignment): This operator assigns the value of the right operand to the left operand.
- += (Add and assign): This operator adds the right operand to the left operand and assigns the result.
- -= (Subtract and assign): This operator subtracts the right operand from the left operand and assigns the result.
- *= (Multiply and assign): This operator multiplies the left operand by the right operand and assigns the result.
- /= (Divide and assign): This operator divides the left operand by the right operand and assigns the result.
// C program to show assignment operators
#include stdio.h
// Main function
int main()
{
int a = 5;
a += 3;
printf("%d\n", a);
a *= 2;
printf("%d\n", a);
return 0;
}
Output:
8
16
Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by one.Types:
- Pre-increment (++a): This operator increases the value of the operand before it is used in an expression.
- Post-increment (a++): This operator uses the current value of the operand and then increments it.
- Pre-decrement (–a): This operator decreases the value of the operand before it is used in an expression.
- Post-decrement (a–): This operator uses the current value of the operand and then decrements it.
// C program to show increment and
// decrement operators
#include stdio.h
// Main function
int main()
{
int a = 5;
printf("%d\n", ++a);
printf("%d\n", a++);
return 0;
}
Output:
6
6
Bitwise Operators
These operators perform operations at the bit level on integer values.Common Bitwise Operators:
- Bitwise AND (&): This operator sets each bit to 1 only if both corresponding bits are 1.
- Bitwise OR (|): This operator sets each bit to 1 if at least one corresponding bit is 1.
- Bitwise XOR (^): This operator sets each bit to 1 if the corresponding bits are different.
- Left Shift (): This operator shifts the bits of the operand to the left by a specified number of positions.
- Right Shift (): This operator shifts the bits of the operand to the right by a specified number of positions.
// C program to show bitwise operators
#include stdio.h
// Main function
int main()
{
int a = 5, b = 3;
printf("%d\n", a & b);
printf("%d\n", a | b);
printf("%d\n", a ^ b);
return 0;
}
Output:
1
7
6
Ternary Operator
This operator is used as a compact form of conditional (if-else) statements.(?:): This operator evaluates a condition and returns one value if true and another value if false.
Example:
// C program to show Ternary operator
#include stdio.h
// Main function
int main()
{
int a = 10, b = 20;
int max = (a b) ? a : b;
printf("Max: %d\n", max);
return 0;
}
Output:
Max: 20
Sizeof Operator
This operator is used to determine the memory size of a variable or data type in bytes.sizeof: This operator returns the size of its operand in bytes at compile time.
Example:
// C program to show sizeof operator
#include stdio.h
// Main function
int main()
{
printf("%lu\n", sizeof(int));
printf("%lu\n", sizeof(float));
return 0;
}
Output:
4
4
Comma Operator
This operator allows multiple expressions to be evaluated in a single statement., (Comma): This operator evaluates multiple expressions from left to right and returns the value of the last expression.
Example:
// C program to show comma operator
#include stdio.h
// Main function
int main()
{
int a;
a = (1, 2, 3);
printf("%d\n", a);
return 0;
}
Output:
3
Operator Precedence and Associativity
When multiple operators are used in a single expression, C follows a defined order of execution.- Precedence: This determines which operator is evaluated first in an expression.
- Associativity: This determines the direction of evaluation when operators have the same precedence.
// C program to show operator precedence
// and associativity
#include stdio.h
// Main function
int main()
{
int result = 10 + 5 * 2;
printf("%d\n", result);
return 0;
}
Output:
Multiplication happens before addition due to higher precedence.20
Conclusion
Operators in C form the backbone of all computations and decision-making in programs. They allow you to manipulate data, evaluate conditions, and control program flow efficiently.What this really means is that strong command over operators directly improves your problem-solving ability in C. Focus on understanding how each operator behaves and how precedence affects expressions, and you’ll write cleaner, more predictable code.
Frequently Asked Questions
1. What is the difference between = and == in C?2. What does the % operator do?The = operator assigns a value to a variable, while == compares two values for equality.
3. What is operator precedence?The % operator returns the remainder after dividing one number by another.
4. Can multiple operators be used in one expression?Operator precedence defines the order in which operators are evaluated in an expression.
5. What is the ternary operator used for?Yes, multiple operators can be used, and their execution depends on precedence and associativity rules.
The ternary operator is used as a short form of if-else to make decisions in a single line.
0 Comments