
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
Table of Contents
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.
Operator | Name | Example | Description |
---|---|---|---|
+ | Addition | a + b | Adds two operands. |
- | Subtraction | a - b | Subtracts the second operand from the first. |
* | Multiplication | a * b | Multiplies two operands. |
/ | Division | a / b | Divides the numerator by the denominator. |
% | Modulus | a % b | Returns 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).
Operator | Name | Example | Description |
---|---|---|---|
&& | Logical AND | c1 && c2 | Returns 1 if both conditions c1 and c2 are true. |
|| | Logical OR | c1 || c2 | Returns 1 if at least one of the conditions c1 or c2 is true. |
! | Logical NOT | !c1 | Reverses 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).
Operator | Name | Example | Description |
---|---|---|---|
== | Equal to | a == b | Checks if the two operands are equal. |
!= | Not equal to | a != b | Checks if the two operands are not equal. |
> | Greater than | a > b | Checks if the first operand is greater than the second. |
< | Less than | a < b | Checks if the first operand is less than the second. |
>= | Greater than or equal to | a >= b | Checks if the first operand is greater than or equal to the second. |
<= | Less than or equal to | a <= b | Checks 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.
Operator | Name | Type | Description |
---|---|---|---|
++ | Increment | ++a (Pre-fix) | Increments the value of ‘a’ before using it in the expression. |
++ | Increment | a++ (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. |
-- | Decrement | a-- (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.
Operator | Name | Description |
---|---|---|
& | Bitwise AND | Sets each bit to 1 if both corresponding bits are 1. |
| | Bitwise OR | Sets each bit to 1 if at least one of the corresponding bits is 1. |
^ | Bitwise XOR | Sets each bit to 1 if the corresponding bits are different. |
~ | Bitwise NOT (Complement) | Inverts all the bits of the operand. |
<< | Left Shift | Shifts bits to the left, filling trailing bits with 0s. |
>> | Right Shift | Shifts 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 inx
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.
Shorthand | Equivalent 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 (+
), so2 + 3 * 4
is evaluated as2 + 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
Priority | Operator Type | Operators | Associativity |
---|---|---|---|
1 (High) | Postfix | () [] -> . ++ -- | Left-to-right |
2 | Unary | + - ! ~ ++ -- (type) * & sizeof | Right-to-left |
3 | Multiplicative | * / % | Left-to-right |
4 | Additive | + - | Left-to-right |
5 | Shift | << >> | Left-to-right |
6 | Relational | < <= > >= | Left-to-right |
7 | Equality | == != | Left-to-right |
8 | Bitwise AND | & | Left-to-right |
9 | Bitwise XOR | ^ | Left-to-right |
10 | Bitwise OR | | | Left-to-right |
11 | Logical AND | && | Left-to-right |
12 | Logical OR | || | Left-to-right |
13 | Conditional | ?: | Right-to-left |
14 | Assignment | = += -= *= /= %= 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.