Operators in C can be categorized into several types based on their functionality. Here's an overview of different types of operators in C:
1. Arithmetic Operators:
- Perform mathematical operations on numeric values.
- Examples:
+
(addition),-
(subtraction),*
(multiplication),/
(division),%
(modulo).
int a = 10, b = 3;
int sum = a + b; // 13
int difference = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3
int remainder = a % b; // 1
2. Relational Operators:
- Compare values and return a Boolean result (true or false).
- Examples:
==
(equal to),!=
(not equal to),<
(less than),>
(greater than),<=
(less than or equal to),>=
(greater than or equal to).
int x = 5, y = 8;
int isEqual = (x == y); // false (0)
int isNotEqual = (x != y); // true (1)
int isLessThan = (x < y); // true (1)
3. Logical Operators:
- Perform logical operations on Boolean values.
- Examples:
&&
(logical AND),||
(logical OR),!
(logical NOT).
int a = 1, b = 0;
int logicalAnd = (a && b); // false (0)
int logicalOr = (a || b); // true (1)
int logicalNot = !a; // false (0)
4. Assignment Operators:
- Assign values to variables.
- Examples:
=
(assignment),+=
(add and assign),-=
(subtract and assign),*=
(multiply and assign),/=
(divide and assign),%=
(modulo and assign).
int x = 10, y = 5;
x += y; // x is now 15 (equivalent to x = x + y)
y *= 2; // y is now 10 (equivalent to y = y * 2)
5. Bitwise Operators:
- Perform bit-level operations on integers.
- Examples:
&
(bitwise AND),|
(bitwise OR),^
(bitwise XOR),~
(bitwise NOT),<<
(left shift),>>
(right shift).
int a = 5, b = 3;
int bitwiseAnd = a & b; // 1
int bitwiseOr = a | b; // 7
int bitwiseXor = a ^ b; // 6
6. Unary Operators:
- Operate on a single operand.
- Examples:
+
(unary plus),-
(unary minus),++
(increment),--
(decrement),!
(logical NOT).
int x = 5;
int unaryPlus = +x; // 5
int unaryMinus = -x; // -5
int increment = ++x; // x is now 6
7. Conditional (Ternary) Operator:
- Provides a concise way to write conditional expressions.
- Syntax:
condition ? expr1 : expr2
int a = 5, b = 10;
int max = (a > b) ? a : b; // max is 10
8. Sizeof Operator:
- Determines the size, in bytes, of a data type or an object.
- Syntax:
sizeof(type)
orsizeof(expression)
int sizeOfInt = sizeof(int); // Size of int in bytes
9. Comma Operator:
- Evaluates multiple expressions separated by commas.
- Returns the value of the last expression.
int a = 5, b = 10, c = 15;
int result = (a += 2, b += 5, c += 10); // result is 25
10. Address-of (&) and Dereference (*) Operators:
&
gets the memory address of a variable.*
is the dereference operator, used to access the value at a memory address.
int number = 42;
int* ptr = &number; // ptr holds the address of 'number'
int valueAtPtr = *ptr; // valueAtPtr is 42
11. Pointer Operators (->):
- Accesses a member of a structure or union through a pointer.
struct Point {
int x;
int y;
};
struct Point p = {10, 20};
struct Point* ptr = &p;
int xValue = ptr->x; // Accessing 'x' through the pointer
12. Conditional Compilation (#) Operator:
- Used in preprocessor directives to control compilation.
- Examples:
#define
,#ifdef
,#ifndef
,#if
,#else
,#elif
,#endif
.
#define DEBUG 1
#if DEBUG
// Code for debugging
#else
// Code for release
#endif
13. Miscellaneous Operators:
,
(comma operator): Separates expressions in a statement.sizeof
: Returns the size of a type or an object.->
: Accesses a member of a structure or union through a pointer.
These operators, combined with the ones previously mentioned, cover the majority of operators in the C programming language. They provide powerful tools for performing various operations, managing memory, and controlling program flow. Understanding how to use these operators effectively is key to writing expressive and efficient C code.
No comments:
Post a Comment