BCA TU, C Programming – Unit 4: Operators and Expressions

Operators are the symbols used to perform specific operations on data. Expressions are the combinations of variables, constants, and operators that produce values. Together, they are the core of problem-solving in C programming.


1. Arithmetic Operators

Used for mathematical calculations.

OperatorMeaningExampleResult
+Addition5 + 38
Subtraction5 – 32
*Multiplication5 * 315
/Division5 / 22
%Modulus (remainder)5 % 21

Note: In integer division, decimal part is ignored.


2. Relational Operators

Used to compare values, the result is either true (1) or false (0).

OperatorMeaningExampleResult
==Equal to5 == 30
!=Not equal to5 != 31
>Greater than5 > 31
<Less than5 < 30
>=Greater or equal5 >= 51
<=Less or equal5 <= 61

3. Logical Operators

Used for decision making with boolean values.

OperatorMeaningExampleResult
&&Logical AND(5 > 2 && 5 < 10)1
``Logical OR
!Logical NOT!(5 > 2)0

4. Assignment Operator

Used to assign values.

OperatorMeaningExampleEquivalent
=Assigna = 10Assigns 10
+=Add and assigna += 5a = a + 5
-=Subtract and assigna -= 5a = a – 5
*=Multiply and assigna *= 5a = a * 5
/=Divide and assigna /= 5a = a / 5

5. Increment / Decrement Operator

  • ++ → Increases value by 1.
  • — → Decreases value by 1.

int a = 5;

printf(“%d”, ++a); // Output: 6 (pre-increment)

printf(“%d”, a++); // Output: 6 then a becomes 7 (post-increment)


6. Conditional Operator (?:)

Shorthand for if-else.

Syntax:

condition ? expression1 : expression2;

Example:

int a = 5, b = 10, max;

max = (a > b) ? a : b;

printf(“%d”, max);   // Output: 10


7. Bitwise Operators

Work at the bit level (0s and 1s).

OperatorMeaningExample (a=5=0101, b=3=0011)Result
&ANDa & b → 0101 & 00111
``OR`a
^XORa ^ b → 0101 ^ 00116
~NOT~a → Inverts bits of a-6
<<Left shifta << 1 → 0101 → 101010
>>Right shifta >> 1 → 0101 → 00102

8. Comma Operator

  • Evaluates multiple expressions but returns the value of the last expression.

int x;

x = (1, 2, 3);   // x = 3


9. sizeof Operator

Returns the size of a data type (in bytes).

printf(“%d”, sizeof(int));   // Output: 4 (depends on system)

printf(“%d”, sizeof(char));  // Output: 1


10. Operator Precedence & Associativity

When multiple operators are in an expression, precedence decides priority and associativity decides direction.

Example:

int x = 5 + 3 * 2;   // Multiplication first → 5 + 6 = 11

Precedence Order (Highest → Lowest):

  1. (), [] (Parentheses, Array indexing)
  2. ++, –, !, ~, sizeof
  3. *, /, %
  4. +, –
  5. <, <=, >, >=
  6. ==, !=
  7. &&
  8. ||
  9. ?:
  10. =, +=, -=

11. Expressions and Evaluation

An expression is a combination of variables, constants, and operators.

Example:

int x = (5 + 3) * 2;  // Evaluated as 16


12. Type Casting in Expression

Converting one data type into another.

Implicit Casting: Done automatically.

int a = 5;

float b = a;    // int → float

Explicit Casting: Done manually.

float avg;

int sum = 5, n = 2;

avg = (float) sum / n;  // Correct → 2.5


13. Program Statement

  • Statement: Smallest independent executable unit in C.
  • Types:
    1. Expression Statement → x = 5 + 2;
    2. Compound Statement → { a = 2; b = 3; }
    3. Control Statement → if, for, while, switch

Key Takeaways

  1. Operators are the building blocks of expressions in C.
  2. Categories include arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, comma, and sizeof.
  3. Operator precedence & associativity decide evaluation order.
  4. Expressions can be type casted to avoid errors in calculation.

Understanding operators helps in writing efficient and bug-free programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.