C Language
Operators & Expressions
C is a powerful and widely-used programming language that provides a variety of operators for performing different types of operations on data. Operators in C can be classified into several categories based on their functionality:
Arithmetic Operators:
These operators are used to perform basic arithmetic operations.
| Operators | Meanings | Examples | Results |
|---|---|---|---|
| + | Addition: Adds two operands. | 5+2 | 7 |
| - | Subtraction: Subtracts the right operand from the left operand. | 5-2 | 3 |
| * | Multiplication: Multiplies two operands. | 5*2 | 10 |
| / | Division: Divides the left operand by the right operand. | 5/2 | 2 |
| % | Modulus: Computes the remainder of the division of the left operand by the right operand. | 5%2 | 1 |
Syntax:
int a = 10, b = 5, result; result = a + b; // result contains 15
Relational Operators:
These operators are used for comparing two values.
| Operators | Meanings | Examples | Results |
|---|---|---|---|
| == | Equal to: Checks if two operands are equal. | 5==2 | false |
| != | Not equal to: Checks if two operands are not equal. | 5!=2 | true |
| > | Greater than: Checks if the left operand is greater than the right operand. | 5>2 | true |
| < | Less than: Checks if the left operand is less than the right operand. | 5<2 | false |
| >= | Greater than or equal to: Checks if the left operand is greater than or equal to the right operand. | 5>=2 | true |
| <= | Less than or equal to: Checks if the left operand is less than or equal to the right operand. | 5<=2 | false |
Syntax:
int x = 5, y = 10;
if (x > y) {
// Code here will not execute
} else {
// Code here will execute
}
Logical Operators:
These operators are used to perform logical operations.
| Operators | Meanings | Examples | Results |
|---|---|---|---|
| && | Logical AND: Returns true if both operands are true. | (5<2)&&(5>3) | false |
| || | Logical OR: Returns true if at least one operand is true. | (5<2)||(5>3) | true |
| ! | Logical NOT: Returns true if the operand is false, and false if the operand is true. | !(5>2) | true |
Syntax:
int a = 1, b = 0;
if (a && b) {
// Code here will not execute
} else {
// Code here will execute
}
Assignment Operators:
These operators are used to perform logical operations.
i = 5;
| Operators | Meanings | Examples | Results |
|---|---|---|---|
| = | Assignment: Assigns the value of the right operand to the left operand. | j = 2; i = j; |
Now 'i' value is '2' |
| += | Add and assign: Adds the right operand to the left operand and assigns the result to the left operand. | i+=10 | 15 |
| -= | Subtract and assign): Subtracts the right operand from the left operand and assigns the result to the left operand. | i-=10 | -5 |
| *= | Multiply and assign: Multiplies the left operand by the right operand and assigns the result to the left operand. | i*=10 | 50 |
| /= | Divide and assign: Divides the left operand by the right operand and assigns the result to the left operand. | i/=10 | 0 |
| %= | Modulus and assign: Computes the modulus of the left operand with the right operand and assigns the result to the left operand. | i%=10 | 5 |
Syntax:
int i = 5; i += 10; // meaning i = i + 10
Increment and Decrement Operators:
These operators are used to increment (++) or decrement (--) a variable by 1. For example, ++ is used to increase, and -- is used to reduce the value of an integer or char variable.
Syntax:
int count = 10; count++; // count now contains 11
Bitwise Operators:
These operators perform bitwise operations on integer types, and Bitwise operators are used for low-level operations, such as bit manipulation.
| Operators | Meanings |
|---|---|
| << | Left shift: Shifts the bits to left. |
| >> | Right shift: Shifts the bits to right. |
| ~ | Bitwise NOT: Bitwise inversion (one's complement). |
| & | Bitwise AND: Bitwise logical and. |
| | | Bitwise NOT: Bitwise logical or. |
| ^ | Bitwise XOR: Bitwise logical exclusive or. |
Example:
#include <stdio.h>
int main() {
// Bitwise AND (&) operator
int num1 = 12; // Binary: 1100
int num2 = 6; // Binary: 0110
int result_and = num1 & num2;
// Output: 4 (Binary: 0100)
printf("Bitwise AND: %d\n", result_and);
// Bitwise OR (|) operator
int num3 = 12; // Binary: 1100
int num4 = 6; // Binary: 0110
int result_or = num3 | num4;
// Output: 14 (Binary: 1110)
printf("Bitwise OR: %d\n", result_or);
// Bitwise XOR (^) operator
int num5 = 12; // Binary: 1100
int num6 = 6; // Binary: 0110
int result_xor = num5 ^ num6;
// Output: 10 (Binary: 1010)
printf("Bitwise XOR: %d\n", result_xor);
// Bitwise NOT (~) operator
int num7 = 12; // Binary: 1100
int result_not = ~num7;
// Output: -13 (Binary: 11111111111111111111111111110011)
printf("Bitwise NOT: %d\n", result_not);
// Left shift (<<) operator
int num8 = 8; // Binary: 1000
int result_left_shift = num8 << 2;
// Output: 32 (Binary: 100000)
printf("Left Shift: %d\n", result_left_shift);
// Right shift (>>) operator
int num9 = 16; // Binary: 10000
int result_right_shift = num9 >> 2;
// Output: 4 (Binary: 100)
printf("Right Shift: %d\n", result_right_shift);
return 0;
}
Bitwise AND: 4 Bitwise OR: 14 Bitwise XOR: 10 Bitwise NOT: -13 Left Shift: 32 Right Shift: 4
Conditional (Ternary) Operator:
Conditional operator is used to check a condition and select a value depending on the condition. Normally the selected value will be assigned to a variable which ha the following form.
Syntax:
variable = (condition) ? value 1 : value 2;
Example:
#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 20;
// Using the conditional operator to find the maximum of two numbers
int max = (num1 > num2) ? num1 : num2;
printf("The maximum of %d and %d is: %d\n", num1, num2, max);
return 0;
}
The maximum of 10 and 20 is: 20
What's Next?
We've now entered the finance section on this platform, where you can enhance your financial literacy.