In C programming, operators are symbols used to perform operations on variables and values. C provides a wide range of operators, such as arithmetic, relational, logical, bitwise, and more. This tutorial will guide you through the various types of operators in C and how to use them effectively.
C Operators Tutorial
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations on numeric values.
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Sum: %d\n", a + b);
printf("Difference: %d\n", a - b);
printf("Product: %d\n", a * b);
printf("Quotient: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}
2. Relational Operators
Relational operators are used to compare two values. They return either true (1) or false (0).
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Is a > b? %d\n", a > b);
printf("Is a < b? %d\n", a < b);
printf("Is a == b? %d\n", a == b);
printf("Is a != b? %d\n", a != b);
return 0;
}
3. Logical Operators
Logical operators are used to combine conditional statements and return true or false based on the result.
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("a > 5 && b < 10: %d\n", a > 5 && b < 10);
printf("a > 5 || b > 10: %d\n", a > 5 || b > 10);
printf("!(a > b): %d\n", !(a > b));
return 0;
}
4. Bitwise Operators
Bitwise operators are used to perform bit-level operations on integers. These include operators like AND (&), OR (|), XOR (^), and more.
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("a & b: %d\n", a & b);
printf("a | b: %d\n", a | b);
printf("a ^ b: %d\n", a ^ b);
printf("~a: %d\n", ~a);
return 0;
}
5. Assignment Operators
Assignment operators are used to assign values to variables. These include simple assignment and compound assignment operators.
#include <stdio.h>
int main() {
int a = 5;
a += 3; // a = a + 3
printf("a += 3: %d\n", a);
a *= 2; // a = a * 2
printf("a *= 2: %d\n", a);
return 0;
}
6. Increment and Decrement Operators
Increment and Decrement operators are used to increase or decrease a variable's value by 1, respectively.
#include <stdio.h>
int main() {
int a = 5;
printf("a++: %d\n", a++);
printf("++a: %d\n", ++a);
printf("a--: %d\n", a--);
printf("--a: %d\n", --a);
return 0;
}
7. Conditional (Ternary) Operator
The conditional (ternary) operator is a shorthand way to perform a conditional check. It is used as follows: condition ? expr1 : expr2
.
#include <stdio.h>
int main() {
int a = 10, b = 5;
int max = (a > b) ? a : b;
printf("Max: %d\n", max);
return 0;
}
8. Sizeof Operator
The sizeof operator is used to determine the size of a variable or data type in bytes.
#include <stdio.h>
int main() {
int a = 10;
printf("Size of int: %zu\n", sizeof(a));
return 0;
}
9. Comma Operator
The comma operator allows multiple expressions to be evaluated in a single statement, with the result of the last expression being returned.
#include <stdio.h>
int main() {
int a = 5, b = 10;
int result = (a++, b++);
printf("a: %d, b: %d, result: %d\n", a, b, result);
return 0;
}
10. Conclusion
Understanding operators is fundamental to programming in C. They allow you to perform a wide range of operations on variables and values. By mastering these operators, you can write more efficient and effective C programs.