CSC115 ยท 2081 ยท 10 marks
List different types of operators.Explain any four of them[2+8]
List different types of operators.Explain any four of them[2+8]
Quick answer
An operator is a symbol that specifies an operation to be performed on operands. C includes the following types of operators: Arithmetic Operator; Assignment Operator; Increment and Decrement Operator.
Types of Operators in C
List of Operators [2 marks]
An operator is a symbol that specifies an operation to be performed on operands. C includes the following types of operators:
- Arithmetic Operator
- Assignment Operator
- Increment and Decrement Operator
- Relational Operator
- Logical Operator
- Conditional Operator
- Comma Operator
- Sizeof Operator
- Bitwise Operator
Explanation of Any Four Operators [8 marks]
1. Arithmetic Operators
Arithmetic operators are used for numeric calculations. They are of two types:
i. Unary Arithmetic Operators - operate on a single operand.
| Operator | Meaning | Example |
|---|---|---|
| + | Unary plus | +a |
| - | Unary minus (negation) | -a |
ii. Binary Arithmetic Operators - operate on two operands.
| Operator | Meaning | Example | Result (a=10, b=3) |
|---|---|---|---|
| + | Addition | a + b | 13 |
| - | Subtraction | a - b | 7 |
| * | Multiplication | a * b | 30 |
| / | Division | a / b | 3 |
| % | Modulus (remainder) | a % b | 1 |
Example:
int a = 10, b = 3;
printf("%d", a % b); // Output: 1
printf("%d", a / b); // Output: 3
2. Relational Operators
Relational operators are used to compare two values or expressions. The result of a relational operation is either true (1) or false (0).
| Operator | Meaning | Example (a=5, b=3) | Result |
|---|---|---|---|
| > | Greater than | a > b | 1 (true) |
| < | Less than | a < b | 0 (false) |
| >= | Greater than or equal to | a >= 5 | 1 (true) |
| <= | Less than or equal to | a <= b | 0 (false) |
| == | Equal to | a == b | 0 (false) |
| != | Not equal to | a != b | 1 (true) |
Example:
int a = 5, b = 3;
if (a > b)
printf("a is greater"); // Output: a is greater
3. Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by 1. They are of two types:
i. Prefix (operator written before operand)
++x: value is incremented first, then used in expression.--x: value is decremented first, then used in expression.
ii. Postfix (operator written after operand)
x++: value is used in expression first, then incremented.x--: value is used in expression first, then decremented.
Example:
int x = 5, y;
y = ++x; // x becomes 6 first, then y = 6
printf("%d %d", x, y); // Output: 6 6
x = 5;
y = x++; // y = 5 first, then x becomes 6
printf("%d %d", x, y); // Output: 6 5
4. Logical Operators
Logical operators are used to combine two or more relational expressions and return a true (1) or false (0) result. They are commonly used in decision-making.
| Operator | Meaning | Example | Result (a=5, b=3, c=0) |
|---|---|---|---|
| && | Logical AND | a>b && b>c | 1 (both true) |
| || | Logical OR | a>b || b<c | 1 (at least one true) |
| ! | Logical NOT | !c | 1 (negation of 0) |
Truth Table for AND (&&):
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True (1) | True (1) | True (1) |
| True (1) | False (0) | False (0) |
| False (0) | True (1) | False (0) |
| False (0) | False (0) | False (0) |
Example:
int a = 5, b = 3, c = 0;
if (a > b && b > c)
printf("Both conditions true"); // Output: Both conditions true
if (!c)
printf("c is zero"); // Output: c is zero
Summary: Operators are fundamental building blocks in C programming. Arithmetic operators perform calculations, relational operators compare values, increment/decrement operators modify values by 1, and logical operators combine conditions for decision-making.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.