Essential C Programming Concepts and Memory Management

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:

  1. Prevents duplicate definitions
  2. Improves code organization

static

Restricts variable scope to file/function while preserving value between calls.

const

Creates immutable values with multiple applications:

  1. Consatnt declaration: const int MAX=100;
  2. Protecting function parameters
  3. Immutable return values
  4. Pointer control:
    • const int*: Pointer to constant
    • int* const: Constant pointer
    • const int* const: Fully immutable

Memory Segments

  1. Code Segment: Read-only executable instructions
  2. Data Segment: Initialized global/static variables
  3. BSS: Zero-initialized global/static variables
  4. Stack: Automatic storage for function calls (LIFO)
  5. 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)

Division/Modulo

  • Division sign: Single negative → negative result
  • Modulo sign: Matches dividend

String Functions

Key functions from <string.h>:

  1. strlen(): Length excluding null terminator
  2. strcpy(): Copies including null terminator
  3. strcmp(): Lexical comparison (returns difference)
  4. 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.

Tags: c programming Memory Management Data Types Bitwise Operations String Handling

Posted on Wed, 20 May 2026 02:44:17 +0000 by Jami