Programming in C

Data Types, I/O, Decision Making and Loops


This chapter highlights definitions, sizes, operator precedence, control constructs, common pitfalls, and a short debugging example.


Data Concepts in C

  • Constants are fixed values that do not change during execution (literal or defined via #define).
  • Variables are named storage locations declared with a data type before use.
  • Declaration example: int count; or float avg = 0.0f;.

Data Types, Size and Values

Standard fundamental types commonly used in exams are listed below.

Common C types (typical 32-bit system)
  • char — 1 byte, stores a character, range: typically -128 to 127 (signed) or 0 to 255 (unsigned).
  • int — 4 bytes, range implementation-defined; commonly -2,147,483,648 to 2,147,483,647.
  • short — 2 bytes; long — 4 or 8 bytes depending on platform.
  • float — 4 bytes (single precision); double — 8 bytes (double precision).
c-data-sizes

Storage Classes

  • auto — default for local variables (automatic storage duration).
  • static — retains value between calls; internal linkage if file-scope.
  • extern — declares a global variable defined elsewhere.
  • register — hint to keep variable in CPU register (often ignored by modern compilers).

Remember exam questions often ask for scope and lifetime of each storage class.


Operators and Precedence

  • Operator categories: arithmetic, relational, logical, bitwise, assignment, and ternary.
  • Precedence determines evaluation order. Higher precedence operators evaluate first.
Important precedence (high → low)
  • Unary: ++ -- + - !
  • Multiplicative: * / %
  • Additive: + -
  • Relational: < > <= >=
  • Equality: == !=
  • Logical: && then ||
  • Assignment: = and compound assignments
Note: use parentheses to enforce evaluation order when needed.
operator-precedence

Operator associativity is usually left-to-right; assignment and some unary operators associate right-to-left.


Statements and Expressions

  • Declaration statements: introduce variables and optionally initialize them.
  • Expression statements: evaluate expressions and may produce side effects (a = b + c;).
  • Compound statements (blocks): groups of statements enclosed in { and } defining scope.

Input-Output Statements

  • Standard I/O: printf(), scanf(), puts().
  • Format specifiers: %d int, %f float, %c char, %s string.
  • Always pass address with scanf() for non-array variables: scanf("%d", &x).
I/O Example
int x;
printf("Enter a number: ");
scanf("%d", &x);
printf("You entered %d
", x);

Selection Statements

  • if executes a block when condition is true.
  • if-else selects between two branches.
  • switch selects cases based on integer/enum; use break to prevent fall-through.
  • goto exists but is discouraged; used rarely for error handling in legacy code.
if-else example
if (a > b) {
  printf("a is greater
");
} else {
  printf("b is greater or equal
");
}
switch example
switch (ch) {
  case 'a': doA(); break;
  case 'b': doB(); break;
  default: doDefault();
}

Loop Constructs

  • for — initialization; condition; update. Use when count known.
  • while — condition-checked before each iteration.
  • do-while — body executes first, condition checked afterwards.
  • Jumpsbreak, continue (skip), and goto (discouraged).
Loop examples
// for loop
for (int i = 0; i < n; i++) {
  sum += i;
}

// while loop
int i = 0;
while (i < n) {
  sum += i;
  i++;
}

// do-while
int i = 0;
do {
  sum += i;
  i++;
} while (i < n);
loop-flowcharts

Overflow

  • Overflow occurs when a calculation produces a value outside a type's range.
  • Signed integer overflow is undefined behaviour in C and must be avoided.
  • Unsigned overflow wraps modulo 2^n; prefer larger types or checks when needed.

Debugging Exercise

Find and fix the errors
#include <stdio.h>

int main() {
  char s[5];
  printf("Enter string: ");
  scanf("%s", s);   // possible overflow if user types >4 chars
  printf("You entered: %s
", s)
  return 0;
}
Correct points
  • Ensure buffer size and use width specifier: scanf("%4s", s);
  • Add missing semicolon after printf.
  • Prefer fgets for safer input.

Programming Exercise

  • Implement a program that reads n integers and finds their sum and average, using loops and appropriate types.
  • Implement factorial using iterative loop and check for overflow for large n.