4 Operators And Expression

C Programming · Unit 4 · 4 hrs

Operators and Expression

Exam-focused notes for Operators and Expression (C Programming, CSC115): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

Operators and Expression in short

This chapter surveys the operators C provides for building expressions, from ordinary arithmetic to bitwise manipulation, and the precedence and associativity rules that decide the order in which a compound expression is evaluated.

Operators and Expression: notes

Arithmetic Operator

An operator specifies an operation to perform on one or more operands, and together they form an expression. Arithmetic operators perform numeric calculation and come in two forms. A unary arithmetic operator acts on a single operand, as the minus in -y does by changing the sign of y. A binary arithmetic operator acts on two operands; C provides five: +, -, *, / and % (modulus, the remainder of integer division). With a = 17 and b = 4:

printf("Sum = %d\n", a + b);         /* 21 */
printf("Difference = %d\n", a - b);  /* 13 */
printf("Product = %d\n", a * b);     /* 68 */
printf("Quotient = %d\n", a / b);    /* 4  */
printf("Remainder = %d\n", a % b);   /* 1  */

When both operands are float or double, the operation is floating point arithmetic and % cannot be used; with a = 12.4 and b = 3.1, a + b is 15.5 and a / b is 4.0. When one operand is an integer and the other is floating point, the operation is mixed mode arithmetic and the result is promoted to float: with a = 12 (int) and b = 2.5 (float), a / b gives 4.8. This promotion is often used deliberately to force an exact result: 5 / 2 gives 2 because both operands are integers, but 5.0 / 2 or 5 / 2.0 gives 2.5.

Relational Operator

A relational operator compares two expressions and yields 1 if the relation is true or 0 if it is false.

OperatorMeaning
<less than
<=less than or equal to
>greater than
>=greater than or equal to
==equal to
!=not equal to

With a = 9 and b = 5, a < b is 0, a != b is 1, and a >= b is 1. It is important not to confuse the equality operator == with the assignment operator =: = stores a value, while == only compares two values and produces 0 or 1.

Logical or Boolean Operator

A logical operator combines two or more relational or boolean expressions into a single true/false result. C has three: && (AND), || (OR) and ! (NOT), where ! is unary and the other two are binary. Any nonzero value is treated as true and zero as false.

pqp && qp || q
falsefalsefalsefalse
falsetruefalsetrue
truefalsefalsetrue
truetruetruetrue

! inverts its operand: !p is true exactly when p is false. With a = 10, b = 5, the expression (a == 10) && (b < a) is true (value 1) because both conditions hold, and !(a == 10) is false (value 0) since NOT inverts the true inner condition.

What this unit covers

  • Arithmetic operator
  • Relational operator
  • Logical or Boolean operator
  • Assignment Operator
  • Ternary operator
  • Bitwise operator
  • Increment or Decrement operator
  • Conditional operator
  • Special Operators (sizeof and comma)
  • Evaluation of Expression
  • Operator Precedence and Associativity

Arithmetic operator

2081 · 10 marks

List different types of operators.Explain any four of them[2+8]

Show solved answer

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.

Open this answer on its own page →

2080 · 5 marks

List different types of operators and explain any three of them. [5]

An operator specifies an operation to be performed on an operand. The variables and constants can be joined by various operators to form expressions. An operand is a data item on which an operator acts. --- C includes the following types of operators: 1. Arithmetic Operator 2. Assignment Operator...

Open this answer on its own page →

2079 · 5 marks

How do you swap the values of two integers without using the third temporary variable? Justify with the example. [5]

Normally, swapping two variables requires a third temporary variable. However, we can swap two integers without a temporary variable using either: 1. Arithmetic operators (Addition and Subtraction) 2. Bitwise XOR operator --- Step Operation a b ----------------------- Initial -- 10 20 Step 1 a = ...

Open this answer on its own page →

Logical or Boolean operator

2075 · 5 marks

Discuss different logical operations in details. [5]

Logical operators are used to combine two or more conditions (relational expressions) and return a true (1) or false (0) result. They are primarily used in decision-making statements like if, while, and for. --- C provides three logical operators: Operator Symbol Meaning -------------------------...

Open this answer on its own page →

Increment or Decrement operator

2074 · 5 marks

Discuss increment and decrement operator with example. [5]

C has two useful operators: increment (++) and decrement (--). These are unary operators because they operate on only a single operand. - The increment operator (++) increments the value of a variable by 1. - The decrement operator (--) decrements the value of a variable by 1. --- These operators...

Open this answer on its own page →