Knowlet

Unit 4: Data Types & Operators

1. C Data Types: Range and Size

Data types in C define the type of data a variable can hold and how much space it occupies in memory.

Data Type Size (typical) Range
char 1 Byte -128 to 127
int 2 or 4 Bytes -32,768 to 32,767 (for 2 bytes)
float 4 Bytes 3.4E-38 to 3.4E+38
double 8 Bytes 1.7E-308 to 1.7E+308

2. Arithmetic & Relational Operators

Arithmetic operators perform mathematical calculations, while relational operators compare values.

Arithmetic Operators

  • + (Addition): Adds two operands.
  • - (Subtraction): Subtracts the second operand from the first.
  • * (Multiplication): Multiplies two operands.
  • / (Division): Divides numerator by denominator.
  • % (Modulus): Returns the remainder of an integer division.

Relational Operators

Used to check the relationship between two operands. Results are always true (1) or false (0).

  • == (Equal to), != (Not equal to).
  • > (Greater than), < (Less than).
  • >= (Greater than or equal to), <= (Less than or equal to).

3. Logical & Conditional Operators

Logical Operators

Used to combine multiple relational expressions.

  • && (Logical AND): True only if both operands are true.
  • || (Logical OR): True if at least one operand is true.
  • ! (Logical NOT): Reverses the logical state of its operand.

Conditional (Ternary) Operator

Syntax: condition ? value_if_true : value_if_false;

It is the only operator in C that takes three operands.

4. Bitwise & Assignment Operators

Bitwise Operators

These operators perform operations at the bit level (binary).

  • & (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR).
  • ~ (Bitwise NOT), << (Left Shift), >> (Right Shift).

Assignment Operators

Used to assign values to variables. C also supports "compound assignment".

  • = (Simple Assignment).
  • +=, -=, *=, /=, %= (Compound Assignments).

5. Unary vs Binary Operators

Operators are categorized by the number of operands they require.

  • Unary Operators: Require only one operand (e.g., ++, --, -, !).
  • Binary Operators: Require two operands (e.g., +, -, *, /).

6. Exam Focus Enhancements

Exam Tips
  • Modulus Rule: The % operator only works with int data types. Using it with float will cause a compile error.
  • Division: Remember that integer division (e.g., 5/2) results in 2, not 2.5. To get a decimal, at least one operand must be a float.
  • Short-circuit: In Logical AND (&&), if the first expression is false, the second is not even checked.
Common Mistakes
  • = vs ==: Confusing the assignment operator (=) with the equality comparison operator (==) in if statements.
  • Range Overflow: Assigning a value larger than 32,767 to a standard 16-bit int.
  • Division by Zero: Always ensure the denominator is not zero before performing a division.
Frequently Asked Questions

Q: What is the size of an int in C?
A: It is compiler-dependent, usually 2 bytes on 16-bit systems and 4 bytes on 32/64-bit systems.

Q: Why do we use Bitwise operators?
A: They are used for low-level programming like device drivers or cryptography where direct bit manipulation is needed.

Did this resource help you study?

Share feedback or report issues to help improve this resource.