C Operators and Expressions (CT 101): Complete IOE Notes & Examples
Operators and Expressions
Operators and Expressions
4 hours | 4 marks

COMPUTER PROGRAMMING (CT 101) – Chapter 3

In C, operators and expressions are the fundamental tools for performing calculations and making logical decisions. An operator is a symbol that performs an operation on one or more operands, while an expression is a combination of these operators and operands that results in a single value. Mastering them is essential for writing effective C programs.

Chapter Information

Chapter 3: Operators and Expressions (4 hours) – 4 marks

Course: Computer Programming (CT 101), I Year I Part

Description: This guide provides a complete overview of C Operators and Expressions, covering arithmetic, logical, bitwise operators, precedence, associativity, and more, as per the IOE syllabus.

Credit: Sujan Karki

Detailed Chapter Notes

3.1 Operators

Operators are symbols that perform operations on variables and values (operands). C operators can be classified by the number of operands they take:

  • Unary: Takes one operand (e.g., -a, ++a).
  • Binary: Takes two operands (e.g., a + b).
  • Ternary: Takes three operands (e.g., the conditional operator ? :).

3.1.1 Arithmetic Operators

Used for mathematical calculations.

OperatorNameExampleDescription
+Additiona + bAdds two operands.
-Subtractiona - bSubtracts the second operand from the first.
*Multiplicationa * bMultiplies two operands.
/Divisiona / bDivides the numerator by the denominator.
%Modulusa % bReturns the remainder of an integer division.

3.1.2 Logical Operators

Used to combine or evaluate logical conditions. The result is always 1 (true) or 0 (false).

OperatorNameExampleDescription
&&Logical ANDc1 && c2Returns 1 if both conditions c1 and c2 are true.
||Logical ORc1 || c2Returns 1 if at least one of the conditions c1 or c2 is true.
!Logical NOT!c1Reverses the logical state; returns 1 if c1 is false, and 0 if true.

3.1.3 Relational Operators

Used to compare two values. The result is 1 (true) or 0 (false).

OperatorNameExampleDescription
==Equal toa == bChecks if the two operands are equal.
!=Not equal toa != bChecks if the two operands are not equal.
>Greater thana > bChecks if the first operand is greater than the second.
<Less thana < bChecks if the first operand is less than the second.
>=Greater than or equal toa >= bChecks if the first operand is greater than or equal to the second.
<=Less than or equal toa <= bChecks if the first operand is less than or equal to the second.

3.1.4 Assignment Operator

Used to assign a value to a variable.

  • = : Assigns the value from the right side operand to the left side operand. Example: c = a + b;

3.1.5 Increment & Decrement Operators

Used to change a value by one.

OperatorNameTypeDescription
++Increment++a (Pre-fix)Increments the value of ‘a’ before using it in the expression.
++Incrementa++ (Post-fix)Uses the current value of ‘a’ in the expression, then increments it.
--Decrement--a (Pre-fix)Decrements the value of ‘a’ before using it in the expression.
--Decrementa-- (Post-fix)Uses the current value of ‘a’ in the expression, then decrements it.

3.1.6 Bitwise Operators

Used to perform operations on the binary (bit-level) representation of integer data.

OperatorNameDescription
&Bitwise ANDSets each bit to 1 if both corresponding bits are 1.
|Bitwise ORSets each bit to 1 if at least one of the corresponding bits is 1.
^Bitwise XORSets each bit to 1 if the corresponding bits are different.
~Bitwise NOT (Complement)Inverts all the bits of the operand.
<<Left ShiftShifts bits to the left, filling trailing bits with 0s.
>>Right ShiftShifts bits to the right.

3.1.7 Special Operators

  • sizeof operator: A unary operator that returns the size of a variable or data type in bytes. Example: sizeof(int) typically returns 4.
  • Comma operator (,): Links multiple expressions. They are evaluated from left to right, and the value of the entire expression is the value of the rightmost expression. Example: x = (a=1, b=2, a+b); will result in x being assigned the value 3.
  • Conditional operator (? :): The only ternary operator in C. It’s a shorthand for an if-else statement.
    Syntax: condition ? value_if_true : value_if_false;
    Example: max = (a > b) ? a : b;

3.2 Expressions

An expression is a combination of operators and operands that yields a single value.

3.2.1 Arithmetic Expressions & Shorthand Assignment

An arithmetic expression involves numeric values and arithmetic operators. Shorthand assignment operators provide a concise way to modify a variable.

ShorthandEquivalent to
a += b;a = a + b;
a -= b;a = a - b;
a *= b;a = a * b;
a /= b;a = a / b;
a %= b;a = a % b;

3.2.2 Evaluation of Expressions

C evaluates expressions based on a set of rules. The most important rules are precedence and associativity.

3.2.3 Precedence & Associativity of Operators

  • Precedence: Determines which operator is performed first in an expression with multiple operators. For example, multiplication (*) has higher precedence than addition (+), so 2 + 3 * 4 is evaluated as 2 + 12.
  • Associativity: Determines the order of evaluation for operators with the same precedence. Most operators are left-to-right associative (e.g., 100 / 10 / 2 is evaluated as (100 / 10) / 2). Assignment and unary operators are right-to-left associative.

Operator Precedence and Associativity Table

PriorityOperator TypeOperatorsAssociativity
1 (High)Postfix() [] -> . ++ --Left-to-right
2Unary+ - ! ~ ++ -- (type) * & sizeofRight-to-left
3Multiplicative* / %Left-to-right
4Additive+ -Left-to-right
5Shift<< >>Left-to-right
6Relational< <= > >=Left-to-right
7Equality== !=Left-to-right
8Bitwise AND&Left-to-right
9Bitwise XOR^Left-to-right
10Bitwise OR|Left-to-right
11Logical AND&&Left-to-right
12Logical OR||Left-to-right
13Conditional?:Right-to-left
14Assignment= += -= *= /= %= etc.Right-to-left
15 (Low)Comma,Left-to-right

PDF Notes

×

Disclaimer

The educational materials provided on this website are intended as supplementary resources to support your learning journey. These study materials are sample documents designed to help students understand complex concepts in C Programming.

We have made every effort to ensure the accuracy of the content. However, we recommend students to refer to standard textbooks and consult with professors for authoritative explanations. These materials should be used as references only.

Scroll to Top