Skip to content

Programming in C - Complete Revision Guide

Complete theory and examples

Section 1 of 128%

Computer Fundamentals

Understanding the basic building blocks of computers and how they process information.
Memory organized by speed/cost/capacityBinary system uses base-2Software types: System vs ApplicationTwo's complement for signed numbers

Memory Hierarchy

Memory hierarchy is organized based on speed, cost, and capacity. Faster memory is more expensive and has less storage, while slower memory is cheaper with more capacity.

Analogy: Think of it like a library: Your desk (registers) holds books you're reading right now, a nearby shelf (cache) has books you might need soon, the main library (RAM) has all available books, and archives (HDD/SSD) store rarely accessed books.

Registers

Fastest (1 cycle), smallest, inside CPU. Stores immediate data being processed.

Cache (L1, L2, L3)

Very fast (2-10 cycles), small (KB-MB). Stores frequently used data.

RAM (Main Memory)

Fast (100+ cycles), medium size (GB). Volatile - data lost when powered off.

Secondary Storage (HDD/SSD)

Slower, large (TB). Non-volatile - data persists after power off.

Types of Software

Software is divided into two main categories based on their purpose and how they interact with hardware.

System Software

Acts as a bridge between hardware and application software. Manages and controls computer hardware.

  • OS: Windows, Linux, macOS - Manages resources
  • Compilers: Translates high-level code to machine code
  • Device Drivers: Controls hardware devices

Application Software

Designed for end-users to perform specific tasks. Works on top of system software.

  • Productivity: Word, Excel, PowerPoint
  • Browsers: Chrome, Firefox - Access the internet
  • Media: VLC, Photoshop - Entertainment & editing

Binary Number System

Computers use binary (base-2) - only 0s and 1s - because digital circuits have two states: ON (1) and OFF (0). Everything in a computer, from numbers to text to images, is stored as binary.

Key Point: Each binary digit is called a bit. 8 bits = 1 byte. For example: 11010101 is 1 byte (8 bits).

Understanding Decimal to Binary Conversion

To convert decimal to binary, repeatedly divide by 2 and collect remainders from bottom to top.

Example: Convert 13 to binary

13 ÷ 2 = 6remainder: 1
6 ÷ 2 = 3remainder: 0
3 ÷ 2 = 1remainder: 1
1 ÷ 2 = 0remainder: 1
Read remainders upward: 1101

Understanding Binary to Decimal Conversion

Each position has a power of 2. Multiply each bit by its position value and add them up.

Example: Convert 1101 to decimal

1
1
0
1
2⁰
= (1 × 8) + (1 × 4) + (0 × 2) + (1 × 1)
= 8 + 4 + 0 + 1
= 13

Signed Bit Notation (Sign-and-Magnitude)

In signed bit notation (also called sign-and-magnitude), the leftmost bit represents the sign (0 = positive, 1 = negative), and the remaining bits represent the magnitude (absolute value).

How It Works
For 8-bit numbers:
00000101 = +5(sign bit = 0, magnitude = 5)
10000101 = -5(sign bit = 1, magnitude = 5)
01111111 = +127
11111111 = -127
Advantages
  • Simple and intuitive to understand
  • Easy to determine sign (just check MSB)
  • Symmetric range for positive/negative
Disadvantages
  • Two representations of zero (+0 and -0)
  • Arithmetic operations more complex
  • Not commonly used in modern computers

2's Complement

2's complement is the most common method for representing signed numbers in computers. The leftmost bit (MSB - Most Significant Bit) is the sign bit: 0 = positive, 1 = negative. Unlike signed bit notation, it has only one representation of zero and makes arithmetic operations simpler.

Finding 2's Complement
  1. Write binary of positive number
  2. 1's complement: Flip all bits (0→1, 1→0)
  3. 2's complement: Add 1 to 1's complement
Example: -5 in 8-bit
+5 = 00000101
1's comp = 11111010 (flip bits)
2's comp = 11111011 (add 1)
So, -5 = 11111011
Examples (8-bit numbers)
Positive numbers:
00000001 = +1
01111111 = +127(largest positive)
Negative numbers:
11111111 = -1
10000000 = -128(smallest negative)

Range for 8-bit signed: -128 to +127 (MSB is sign bit)

Advantages
  • Only one representation of zero
  • Simpler arithmetic circuits (addition/subtraction)
  • Standard in modern computer systems
  • Efficient for hardware implementation
Disadvantages
  • Asymmetric range (one more negative)
  • Less intuitive than sign-and-magnitude
  • Converting to negative requires bit manipulation

Comparison: Signed Bit vs 2's Complement

Binary (8-bit)Signed Bit2's Complement
01111111+127+127
00000001+1+1
00000000+00
10000000-0-128
10000001-1-127
11111111-127-1
Signed Bit Notation:
  • • Range: -127 to +127
  • • Two representations of zero
  • • Symmetric range
2's Complement:
  • • Range: -128 to +127
  • • One representation of zero
  • • Asymmetric range (one extra negative)

Algorithm & Flowchart

An algorithm is a step-by-step procedure to solve a problem. It's like a recipe - clear instructions that anyone can follow to get the same result.

A flowchart is a visual representation of an algorithm using symbols.

Algorithm Properties

  • Finite: Must end after a finite number of steps
  • Definite: Each step must be clear and unambiguous
  • Input: Can have zero or more inputs
  • Output: Must produce at least one output

Flowchart Symbols

  • Oval: Start/End
  • Rectangle: Process/Action
  • Diamond: Decision/Condition
  • Parallelogram: Input/Output
1 / 12