Computer Storage Fundamentals
The smallest storage unit is a bit, holding either 0 or 1. The basic addressable unit is a byte (8 bits). Storage scales as:
- 1 KB = 1024 bytes
- 1 MB = 1024 KB
- 1 GB = 1024 MB
- 1 TB = 1024 GB
Numeric Representation
Computers store numbers using two's complement:
- Signed types use the highest bit as sign (0=positive, 1=negative)
- Positive numbers: Original, inverse, and complement codes are identical
- Negative numbers:
- Inverse code = original with bits flipped (except sign)
- Complement code = inverse + 1
Example (16-bit system):
1: Original: 0000000000000001
Inverse: 0000000000000001
Complement: 0000000000000001
-1: Original: 1000000000000001
Inverse: 1111111111111110
Complement: 1111111111111111
Variable Scope and Storage Classes
extern
Declares globally defined variables from other files:
- Prevents duplicate definitions
- Improves code organization
static
Restricts variable scope to file/function while preserving value between calls.
const
Creates immutable values with multiple applications:
- Consatnt declaration:
const int MAX=100; - Protecting function parameters
- Immutable return values
- Pointer control:
const int*: Pointer to constantint* const: Constant pointerconst int* const: Fully immutable
Memory Segments
- Code Segment: Read-only executable instructions
- Data Segment: Initialized global/static variables
- BSS: Zero-initialized global/static variables
- Stack: Automatic storage for function calls (LIFO)
- Heap: Dynamic memory allocation (
malloc/free)
Data Types
Size varies by architecture:
| Type | 16-bit | 32-bit | 64-bit |
|---|---|---|---|
| char | 1 | 1 | 1 |
| int | 2 | 4 | 4 |
| long | 4 | 4 | 8 |
| float | 4 | 4 | 4 |
| double | 8 | 8 | 8 |
Operators
Bitwise Operations
- XOR (
^): Bits differ → 1, same → 0 - NOT (
~): Inverts all bits - Shifts:
- Left (
<<): Multiply by 2 (no overflow) - Right (
>>): Divide by 2 (sign-preserving)
- Left (
Division/Modulo
- Division sign: Single negative → negative result
- Modulo sign: Matches dividend
String Functions
Key functions from <string.h>:
strlen(): Length excluding null terminatorstrcpy(): Copies including null terminatorstrcmp(): Lexical comparison (returns difference)strcat(): Concatenates strings
Arrays
2D Array Initialization
int valid[][3] = {1,2,3,4,5,6}; // OK
int invalid[][] = {1,2,3}; // Error
Partial initialization fills remaining elements with 0.