Operators and Expressions: Familiarization with various operators and expressions in C.
Operators and Expressions: Familiarization with various operators and expressions in C.
Operators and expressions are fundamental components of programming languages like C. They allow you to perform various operations, manipulate data, and make decisions in your code. Here's a brief description of some commonly used operators and expressions in C, along with examples:
1. Arithmetic Operators:
- Addition (`+`): Adds two operands together.
```c
int sum = 10 + 5; // sum = 15
```
- Subtraction (`-`): Subtracts the second operand from the first.
```c
int difference = 10 - 5; // difference = 5
```
- Multiplication (`*`): Multiplies two operands.
```c
int product = 10 * 5; // product = 50
```
- Division (`/`): Divides the first operand by the second.
```c
int quotient = 10 / 5; // quotient = 2
```
- Modulus (`%`): Returns the remainder of the division between the first and second operands.
```c
int remainder = 10 % 3; // remainder = 1
```
2. Relational Operators:
- Equality (`==`): Checks if two operands are equal.
```c
int a = 5, b = 10;
if (a == b) {
printf("a is equal to b\n");
} else {
printf("a is not equal to b\n");
}
```
- Inequality (`!=`): Checks if two operands are not equal.
```c
int a = 5, b = 10;
if (a != b) {
printf("a is not equal to b\n");
} else {
printf("a is equal to b\n");
}
```
- Greater than (`>`): Checks if the first operand is greater than the second.
```c
int a = 5, b = 10;
if (a > b) {
printf("a is greater than b\n");
} else {
printf("a is not greater than b\n");
}
```
- Less than (`<`): Checks if the first operand is less than the second.
```c
int a = 5, b = 10;
if (a < b) {
printf("a is less than b\n");
} else {
printf("a is not less than b\n");
}
```
- Greater than or equal to (`>=`): Checks if the first operand is greater than or equal to the second.
- Less than or equal to (`<=`): Checks if the first operand is less than or equal to the second.
3. Logical Operators:
- AND (`&&`): Checks if both the first and second conditions are true.
```c
int a = 5, b = 10;
if (a > 0 && b > 0) {
printf("Both a and b are positive\n");
}
```
- OR (`||`): Checks if either the first or second condition is true.
```c
int a = 5, b = -2;
if (a > 0 || b > 0) {
printf("Either a or b is positive\n");
}
```
- NOT (`!`): Negates the value of a condition.
```c
int a = 5;
if (!(a > 10)) {
printf("a is not greater than 10\n");
}
```
4. Assignment Operators:
- Simple assignment (`=`): Assigns the value on the right to the variable on the left.
```c
int a;
a = 10; // a is assigned the value 10
```
- Compound assignment (e.g., `+=`, `-=`, `*=`, `/=`, `%=`): Performs an operation between the variable on the left and the value on the right, and assigns the result back to the variable.
```c
int a = 5;
a += 2; // Equivalent to a = a + 2; (a is assigned the value 7)
```
5. Conditional (Ternary) Operator:
- The conditional operator (`?:`) allows you to make decisions based on a condition.
```c
int a = 5, b = 10;
int max = (a > b) ? a : b; // If a > b, max = a; otherwise, max = b;
```
These are just a few examples of operators and expressions in C. Operators can be used in combination with variables, constants, and other expressions to perform complex operations and control the flow of your program.
Comments
Post a Comment