2081 3

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]

C ProgrammingChapter 4: Operators and ExpressionArithmetic operator

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:

  1. Arithmetic Operator
  2. Assignment Operator
  3. Increment and Decrement Operator
  4. Relational Operator
  5. Logical Operator
  6. Conditional Operator
  7. Comma Operator
  8. Sizeof Operator
  9. 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.

OperatorMeaningExample
+Unary plus+a
-Unary minus (negation)-a

ii. Binary Arithmetic Operators - operate on two operands.

OperatorMeaningExampleResult (a=10, b=3)
+Additiona + b13
-Subtractiona - b7
*Multiplicationa * b30
/Divisiona / b3
%Modulus (remainder)a % b1

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).

OperatorMeaningExample (a=5, b=3)Result
>Greater thana > b1 (true)
<Less thana < b0 (false)
>=Greater than or equal toa >= 51 (true)
<=Less than or equal toa <= b0 (false)
==Equal toa == b0 (false)
!=Not equal toa != b1 (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.

OperatorMeaningExampleResult (a=5, b=3, c=0)
&&Logical ANDa>b && b>c1 (both true)
||Logical ORa>b || b<c1 (at least one true)
!Logical NOT!c1 (negation of 0)

Truth Table for AND (&&):

Condition 1Condition 2Result
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.