Special Operator in C
0 159
Special operators in C are those that offer unique functionalities beyond standard arithmetic or logical operations.
They provide programmers with powerful tools to manipulate data, control program flow, and optimize code.
Some Special operators in C
1Bitwise Operators:
Bitwise operators manipulate individual bits of integer operands, enabling low-level manipulation of data at the binary level.
These operators are essential for tasks such as bit manipulation, setting or clearing specific bits, and optimizing code for memory usage and performance.
& (Bitwise AND): Performs bitwise AND operation.
| (Bitwise OR): Performs bitwise OR operation.
^ (Bitwise XOR): Performs bitwise XOR (exclusive OR) operation.
~ (Bitwise NOT): Performs bitwise complement (inverse) operation.
<< (Left Shift): Shifts bits to the left.
>> (Right Shift): Shifts bits to the right.
// Program for bitwise operator in C #include<stdio.h>int main() { int G = 5; // 0101 in binary int H = 3; // 0011 in binary printf("G & H = %d\n", G & H); // Bitwise AND printf("G | H = %d\n", G | H); // Bitwise OR printf("G ^ H = %d\n", G ^ H); // Bitwise XOR printf("~G = %d\n", ~G); // Bitwise NOT printf("G << 1 = %d\n", G << 1); // Left Shift printf("H >> 1 = %d\n", H >> 1); // Right Shift return 0; }
Output:
G & H = 1 G | H = 7 G ^ H = 6 ~G = -6 G << 1 = 10 H >> 1 = 1
2 sizeof() Operator:
The sizeof() operator provides the size, measured in bytes, of a variable or data type.
// program for sizeof() operator in C #include<stdio.h>int main() { int x; double y; printf("Size of int: %zu bytes\n", sizeof(x)); printf("Size of double: %zu bytes\n", sizeof(y)); return 0; }
Output:
Size of int: 4 bytes Size of double: 8 bytes
3 Ternary Operator (Conditional Operator):
A ternary operator is a conditional operator that evaluates a condition and returns one of two values based on whether the condition is true or false.
// program for ternary operator in C #include<stdio.h>int main() { int x = 5; int y = (x > 0) ? 10 : -10; // If x is greater than 0, y = 10, else y = -10 printf("Value of y: %d\n", y); return 0; }
Output:
Value of y: 10
4Comma Operator:
The comma operator allows multiple expressions to be evaluated in a single statement.
It evaluates each expression from left to right and returns the value of the last expression.
// program for comma operator in C #include<stdio.h>int main() { int a = 1, b = 2, c = 3, sum; sum = (a + b, b + c); // Evaluates (a + b) first, then (b + c) printf("Sum: %d\n", sum); // Output will be 5 return 0; }
Output:
Sum: 5
Share:
Comments
Waiting for your comments