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.

OperatorName
+Addition
Subtraction
*Multiplication
/Division
%Modulo (Remainder)
Example
int a = 10, b = 3;
a + b is 13
a - b is 7
a * b is 30
a / b is 3
a % b is 1

2. Logical Operators
OperatorNameDescription
&&Logical ANDTrue (that is, 1) only if both conditions are true
||Logical ORTrue (1) if any of the conditions is true
!Logical NOTReverses 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).

OperatorNameExampleResult
>Greater than11 > 3True (1)
<Less than11 < 3False (0)
==Equal to11 == 3False (0)
!=Not equal to11 != 3True (1)
>=Greater than or equal to10 >= 10True (1)
<=Less than or equal to10 <= 10True (1)
Note

C does not have boolean (true/false) data type.

  • 1 (or any non-zero value) represents true.
  • 0 represents false.

Relational operators are commonly used in decision-making and looping statements.


4. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorNameDescription
=AssignmentAssigns value on right side to variable on left side
+=Add and assigna += b is shorthand for a = a + b
-=Subtract and assigna -= b is shorthand for a = a - b
*=Multiply and assigna *= b is shorthand for a = a * b
/=Divide and assigna /= b is shorthand for a = a / b
%=Modulo and assigna %= 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.

OperatorNameDescription
++IncrementIncrements the value by 1
DecrementDecrements the value by 1
Example
int a = 5, b = 10;
a++; // a is now 6
b--; // 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.

OperatorName
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left Shift
>>Right Shift

8. Special Operators

Special operators are used for various special purposes.

OperatorNameDescription
sizeofSizeof OperatorReturns the size (in bytes) of a data type or variable (e.g. sizeof(int) is 4.)
&Address-of OperatorGives the memory address of a variable
*Dereference OperatorGives the value stored at a memory address

Posted in

Leave a comment

Is this your new site? Log in to activate admin features and dismiss this message
Log In