Exploring Types C Operators
Arthemetic Operators
+ | Addition Operator |
- | Subtraction Operator |
* | Multiplication Operator |
/ | Division Operator |
% | Modulus |
Example 1: Arithmetic Operators
#include <stdio.h>
int main() {
int a = 10;
int b = 5;
// Addition
int sum = a + b;
printf("Sum: %d\n", sum);
// Subtraction
int difference = a - b;
printf("Difference: %d\n", difference);
// Multiplication
int product = a * b;
printf("Product: %d\n", product);
// Division
int quotient = a / b;
printf("Quotient: %d\n", quotient);
// Modulus
int remainder = a % b;
printf("Remainder: %d\n", remainder);
return 0;
}
Output:
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
In this code, we define two variables a
and b
, and then we use various arithmetic operators to perform calculations and display the results using printf
. The comments indicate which arithmetic operation is being performed in each section, and the output shows the results of those operations.
Assignment Operators
= | Assignment operator |
+= | Add and assign operator |
-= | Subtract and assign operator |
*= | Multiply and assign operator |
/= | Divide and assign operator |
%= | Modulus and assign operator |
Example 2: Assignment Operators
#include <stdio.h>
int main() {
int x = 10, y = 5;
// Assignment operator (=)
int a = x;
printf("a = %d\n", a); // Output: a = 10
// Add and assign operator (+=)
y += x;
printf("y += x: y = %d\n", y); // Output: y += x: y = 15
// Subtract and assign operator (-=)
x -= 3;
printf("x -= 3: x = %d\n", x); // Output: x -= 3: x = 7
// Multiply and assign operator (*=)
y *= 2;
printf("y *= 2: y = %d\n", y); // Output: y *= 2: y = 30
// Divide and assign operator (/=)
x /= 2;
printf("x /= 2: x = %d\n", x); // Output: x /= 2: x = 3
// Modulus and assign operator (%=)
y %= 4;
printf("y %%= 4: y = %d\n", y); // Output: y %= 4: y = 2
return 0;
}
Output:
a = 10
y += x: y = 15
x -= 3: x = 7
y *= 2: y = 30
x /= 2: x = 3
y %= 4: y = 2
In this code, various assignment operators (+=
, -=
, *=
, /=
, and %=
) are used to modify variables and perform arithmetic operations along with assignment. The output demonstrates the values of the variables after applying these assignment operations.
Logical Operators
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Example 3: Logical Operators
#include <stdio.h>
int main() {
int a = 5, b = 10;
// Logical AND (&&)
if (a > 0 && b > 0) {
printf("Both a and b are greater than 0.\n");
} else {
printf("At least one of a or b is not greater than 0.\n");
}
// Logical OR (||)
if (a > 0 || b > 0) {
printf("At least one of a or b is greater than 0.\n");
} else {
printf("Neither a nor b is greater than 0.\n");
}
// Logical NOT (!)
if (!(a > 0)) {
printf("a is not greater than 0.\n");
} else {
printf("a is greater than 0.\n");
}
return 0;
}
Output:
Both a and b are greater than 0.
At least one of a or b is greater than 0.
a is greater than 0.
In this code, we have used the logical operators &&
, ||
, and !
to perform logical operations on the variables a
and b
. The outputs are determined based on the truth values of the conditions involving these operators.
Relational Operators
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example 4: Relational Operators
#include <stdio.h>
int main() {
int num1 = 5;
int num2 = 10;
// Equal to (==)
printf("Is num1 equal to num2? %d\n", num1 == num2);
// Not equal to (!=)
printf("Is num1 not equal to num2? %d\n", num1 != num2);
// Greater than (>)
printf("Is num1 greater than num2? %d\n", num1 > num2);
// Less than (<)
printf("Is num1 less than num2? %d\n", num1 < num2);
// Greater than or equal to (>=)
printf("Is num1 greater than or equal to num2? %d\n", num1 >= num2);
// Less than or equal to (<=)
printf("Is num1 less than or equal to num2? %d\n", num1 <= num2);
return 0;
}
Output:
Is num1 equal to num2? 0
Is num1 not equal to num2? 1
Is num1 greater than num2? 0
Is num1 less than num2? 1
Is num1 greater than or equal to num2? 0
Is num1 less than or equal to num2? 1
Bitwise Operators
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Right shift |
Example 4: Bitwise Operators
#include <stdio.h>
int main() {
unsigned int num1 = 12;
unsigned int num2 = 5;
// Bitwise AND
unsigned int result_and = num1 & num2;
printf("Bitwise AND: %u\n", result_and);
// Bitwise OR
unsigned int result_or = num1 | num2;
printf("Bitwise OR: %u\n", result_or);
// Bitwise XOR
unsigned int result_xor = num1 ^ num2;
printf("Bitwise XOR: %u\n", result_xor);
// Bitwise NOT (complement)
unsigned int result_not = ~num1;
printf("Bitwise NOT: %u\n", result_not);
// Left shift
unsigned int result_left_shift = num1 << 2;
printf("Left Shift: %u\n", result_left_shift);
// Right shift
unsigned int result_right_shift = num1 >> 2;
printf("Right Shift: %u\n", result_right_shift);
return 0;
}
Output:
Bitwise AND: 4
Bitwise OR: 13
Bitwise XOR: 9
Bitwise NOT: 4294967283
Left Shift: 48
Right Shift: 3
In this code, we're using two unsigned integer variables num1
and num2
to demonstrate the bitwise operators. The comments indicate the binary representation of the numbers. The code then performs various bitwise operations using the operators and prints the results. The output values are also provided as comments.