Question and Answer. From Anna University past year exam papers for Programming in C – CS 3251.




Types of Operators in C
Anna University Exam Question
Q.2. (2 Marks) (Nov/Dec 2022) Unit 1.
What are various types of operators in C?
Answer
- Arithmetic operators (
+,-,*,/,%) - Relational operators (also known as comparison operators) (
==,!=,<,>,<=,>=) - Logical operators (
&&,||,!) - Assignment operators (
=,+=,-=, etc.) - Increment and decrement operators (
++,--) - Conditional operator (also known as ternary operator) (
?:) - Bitwise operators (
&,|, etc.) - Special operators (
sizeof, address operator&, dereference operator*, etc.)
Note: Since this is just a 2-mark question, all this information may not be necessary to write. We may write as much as we remember.
Supplementary Notes
1. Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
| Operator | Name |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulo (Remainder) |
Example
int a = 10, b = 3;a + b is 13a - b is 7a * b is 30a / b is 3a % b is 1
2. Logical Operators
| Operator | Name | Description |
|---|---|---|
| && | Logical AND | True (that is, 1) only if both conditions are true |
| || | Logical OR | True (1) if any of the conditions is true |
| ! | Logical NOT | Reverses the logical state |
Note
C does not have a built-in boolean data type.
- True is represented by
1 - False is represented by
0 - In general, any non-zero value is treated as true.
3. Relational (Comparison) Operators
Relational operators are used to compare two values. The result is either true (1) or false (0).
| Operator | Name | Example | Result |
|---|---|---|---|
| > | Greater than | 11 > 3 | True (1) |
| < | Less than | 11 < 3 | False (0) |
| == | Equal to | 11 == 3 | False (0) |
| != | Not equal to | 11 != 3 | True (1) |
| >= | Greater than or equal to | 10 >= 10 | True (1) |
| <= | Less than or equal to | 10 <= 10 | True (1) |
Note
C does not have boolean (true/false) data type.
1(or any non-zero value) represents true.0represents false.
Relational operators are commonly used in decision-making and looping statements.
4. Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Name | Description |
|---|---|---|
| = | Assignment | Assigns value on right side to variable on left side |
| += | Add and assign | a += b is shorthand for a = a + b |
| -= | Subtract and assign | a -= b is shorthand for a = a - b |
| *= | Multiply and assign | a *= b is shorthand for a = a * b |
| /= | Divide and assign | a /= b is shorthand for a = a / b |
| %= | Modulo and assign | a %= b is shorthand for a = a % b |
5. Increment and Decrement Operators
Increment and decrement operators are used to increase or decrease the value of a variable by 1.
| Operator | Name | Description |
|---|---|---|
| ++ | Increment | Increments the value by 1 |
| — | Decrement | Decrements the value by 1 |
Example
int a = 5, b = 10;a++; // a is now 6b--; // b is now 9
6. Conditional (Ternary) Operator
The conditional (ternary) operator is used to evaluate a condition and return one value if true and another value if false.
Syntax
condition ? expression1 : expression2
Example
int a = 10, b = 20, max;max = (a > b) ? a : b;
Since a > b is false, max will be 20.
Note
The ternary operator takes 3 operands.
- Unary operator takes one operand.
- Binary operator takes two operands.
- Similarly, ternary operator works with three operands.
7. Bitwise Operators
Bitwise operators perform operations on individual bits.
| Operator | Name |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left Shift |
| >> | Right Shift |
8. Special Operators
Special operators are used for various special purposes.
| Operator | Name | Description |
|---|---|---|
| sizeof | Sizeof Operator | Returns the size (in bytes) of a data type or variable (e.g. sizeof(int) is 4.) |
| & | Address-of Operator | Gives the memory address of a variable |
| * | Dereference Operator | Gives the value stored at a memory address |
Leave a comment