notesonly.in

One notebook for every subject — open it anywhere.

Log in

C Programming: Data Types, Operators and Control Statements

C is a general-purpose programming language created by Dennis Ritchie in 1972, and it remains the foundation of modern computing — operating systems, embedded chips and even other languages are built on it. Every C program is built from three basics: data types that decide what kind of values variables hold, operators that transform those values, and control statements that decide which code runs and how often.

These basics matter because every program you will ever write rests on them. The wrong data type overflows your numbers, misunderstood operators silently compute wrong answers, and sloppy control flow turns logic into spaghetti. Mastering this trio is the biggest step from reading code to writing it.

Key Concepts

  • int: whole numbers; typically 4 bytes, range about −2.1 × 10⁹ to +2.1 × 10⁹.
  • float / double: decimal numbers; float is 4 bytes (~7 significant digits), double is 8 bytes (~15 digits) — use double for precision.
  • char: a single character, 1 byte, stored as its ASCII code ('A' is 65).
  • Operators: arithmetic (+ − * / %), relational (== != < > <= >=), logical (&& || !), bitwise (& | ^ ~ << >>), assignment (=, +=, …), increment/decrement (++ −−), ternary (cond ? a : b), sizeof.
  • Control statements: if / else if / else; switch (needs break); for, while, do-while loops; break exits a loop, continue skips an iteration.

How It Works

How a for loop executes:

  1. Initialisation runs once: i is set to 1.
  2. Condition is tested before every iteration: is i <= 5?
  3. If true, the body runs; then the update (i++) runs and control returns to step 2.
  4. When the condition fails (i = 6), the loop ends. A do-while tests after the body, so it always runs at least once.

Worked Example

Trace this program fragment and find the final value of sum:

int sum = 0;<br>for (int i = 1; i &lt;= 5; i++) sum = sum + i;

  1. i = 1: condition 1 &lt;= 5 true → sum = 0 + 1 = 1
  2. i = 2: true → sum = 1 + 2 = 3
  3. i = 3: true → sum = 3 + 3 = 6
  4. i = 4: true → sum = 6 + 4 = 10
  5. i = 5: true → sum = 10 + 5 = 15
  6. i = 6: condition 6 &lt;= 5 false → loop ends.

Final value: sum = 15. Two classic traps: 7 / 2 gives 3 (integer division truncates), and 2 + 3 * 4 gives 14, not 20, because * binds tighter than +.

Common Misconceptions

  • "= and == are interchangeable." Deadly — = assigns, == compares; if (x = 5) always assigns 5 and is always true.
  • "A switch case ends by itself." Without break, execution falls through into the next case — a classic source of bugs.
  • "float is fine for money." Binary floating point cannot represent 0.1 exactly; financial code uses integers (paise/cents) or decimal types.

Real-World Applications

  • Operating systems: Windows, Linux and macOS kernels are written in C.
  • Embedded systems: microcontrollers in washing machines, cars and IoT devices run C because it is small and predictable.
  • Language foundations: Python's interpreter, Java's JVM and countless compilers are implemented in C.
  • Competitive programming: C's speed and precise control make it a favourite for algorithmic contests.

C programs stand on data types, operators and control statements. Types decide what values you can hold, operators how you compute, control flow what runs when. Get these three right and you can read — and write — almost any program.