C programming relies heavily on operators to perform various tasks, such as calculations, comparisons, and logic control. Operators simplify code, making it more efficient and easier to understand. In this post, we’ll explore operators with explanations and examples.
Arithmetic Operators
Arithmetic operators perform basic mathematical operations.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
Example:
#include <stdio.h>
int main() {
int a = 15, b = 4;
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;
}
Relational Operators
Relational operators compare two values and return true (1) or false (0).
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Example:
#include <stdio.h>
int main() {
int x = 5, y = 10;
printf("x == y: %d\n", x == y);
printf("x != y: %d\n", x != y);
printf("x > y: %d\n", x > y);
printf("x < y: %d\n", x < y);
return 0;
}
Logical Operators
Logical operators are used to combine multiple conditions.
Operator | Description | Example |
---|---|---|
&& | Logical AND | (a > b) && (c > d) |
|| | Logical OR | (a > b) || (c > d) |
! | Logical NOT | !(a == b) |
Example:
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30;
printf("(a < b) && (c > b): %d\n", (a < b) && (c > b));
printf("(a > b) || (c > b): %d\n", (a > b) || (c > b));
printf("!(a == b): %d\n", !(a == b));
return 0;
}
Assignment Operators
These operators assign values to variables and can also perform operations while assigning.
Operator | Description | Example |
---|---|---|
= | Assign | a = b |
+= | Add and assign | a += b |
-= | Subtract and assign | a -= b |
*= | Multiply and assign | a *= b |
/= | Divide and assign | a /= b |
%= | Modulus and assign | a %= b |
Example:
#include <stdio.h>
int main() {
int num = 10;
num += 5;
printf("After += operation: %d\n", num);
return 0;
}
Bitwise Operators
Bitwise operators work at the bit level, allowing low-level manipulation of data.
Operator | Description | Example |
---|---|---|
& | AND | a & b |
| | OR | a | b |
^ | XOR | a ^ b |
~ | NOT | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
Increment and Decrement Operators
These operators increase or decrease the value of a variable by 1.
Operator | Description | Example |
---|---|---|
++ | Increment by 1 | a++ or ++a |
-- | Decrement by 1 | a-- or --a |
Example:
#include <stdio.h>
int main() {
int num = 5;
printf("Original: %d\n", num);
printf("After Increment: %d\n", ++num);
printf("After Decrement: %d\n", --num);
return 0;
}
Conditional (Ternary) Operator
This operator is a shorthand for the if-else
statement.
Syntax:
condition ? expression1 : expression2;
Example:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int max = (a > b) ? a : b;
printf("The maximum value is: %d\n", max);
return 0;
}
Special Operators
1. Comma Operator
The comma operator allows you to evaluate multiple expressions in a single statement.
Example:
#include <stdio.h>
int main() {
int a, b, c;
a = (b = 10, c = 20);
printf("a: %d, b: %d, c: %d\n", a, b, c);
return 0;
}
2. sizeof
Operator
The sizeof
operator returns the size of a data type or variable in bytes.
Example:
#include <stdio.h>
int main() {
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of char: %lu bytes\n", sizeof(char));
return 0;
}
Real-Life Example: Simple Calculator
#include <stdio.h>
int main() {
char operator;
double num1, num2;
printf("Enter operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &num1, &num2);
switch(operator) {
case '+':
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);
break;
case '-':
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0)
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
else
printf("Division by zero error!\n");
break;
default:
printf("Invalid operator\n");
}
return 0;
}
Conclusion
Understanding operators in C is fundamental for developing powerful applications. From simple arithmetic to complex logical operations, mastering operators will significantly enhance your programming skills. For more programming tutorials, visit Master Coding Science at mastercodingscience.com.