The Foundations of C

A comprehensive guide to low-level programming: Memory, Logic, and Efficiency.

C is a powerful general-purpose programming language that has been the backbone of modern computing for decades. Often referred to as a "mid-level" language, it provides the low-level access of assembly with the structured readability of high-level languages.

Understanding C is essential for mastering how computers manage memory and execute logic at a fundamental level.


1. C Basics

Every C program starts with a main() function and typically includes headers for standard input/output.

Data Types & Variables

Unlike Python, C is statically typed. You must declare the type of a variable before using it.

#include <stdio.h>

int main() {
    int age = 25;           // Integer
    float salary = 5500.50; // Floating point
    char grade = 'A';       // Character
    double pi = 3.14159;    // Double precision float

    printf("Age: %d\n", age);
    return 0;
}

2. Control Statements

Decision making in C is handled via if-else blocks and switch statements.

int score = 85;

if (score >= 90) {
    printf("Grade: A\n");
} else if (score >= 80) {
    printf("Grade: B\n");
} else {
    printf("Grade: C\n");
}

// Switch statement
switch(grade) {
    case 'A': printf("Excellent!\n"); break;
    case 'B': printf("Well done.\n"); break;
    default: printf("Keep trying.\n");
}

3. Iteration & Loops

C provides three primary ways to repeat blocks of code: for, while, and do-while.

// For loop
for (int i = 0; i < 5; i++) {
    printf("%d ", i);
}

// While loop
int j = 0;
while (j < 5) {
    printf("%d ", j++);
}

// Do-While loop (always executes at least once)
int k = 0;
do {
    printf("%d ", k++);
} while (k < 5);

4. Memory Allocation

One of C's most powerful features is manual memory management using the heap.

Stack vs Heap

  • Stack: Automatic allocation, fast, but limited size. Variables are cleared when they go out of scope.
  • Heap: Manual allocation using malloc(), larger size, but requires explicit free().
#include <stdlib.h>

int* ptr = (int*)malloc(sizeof(int)); // Allocate on heap
if (ptr != NULL) {
    *ptr = 100;
    printf("Value: %d\n", *ptr);
    free(ptr); // Always free your memory!
}

5. Arrays in C

Arrays are contiguous blocks of memory holding elements of the same type.

int numbers[5] = {10, 20, 30, 40, 50};
printf("First element: %d\n", numbers[0]);

// Multidimensional arrays
int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

6. Standard Libraries & Vector

C has a minimalist standard library. To use more complex structures like a dynamic array (Vector), you often implement them yourself.

Implementing a Simple Vector

typedef struct {
    int *data;
    int size;
    int capacity;
} Vector;

void vector_init(Vector *v) {
    v->size = 0;
    v->capacity = 4;
    v->data = malloc(sizeof(int) * v->capacity);
}

void vector_push(Vector *v, int value) {
    if (v->size == v->capacity) {
        v->capacity *= 2;
        v->data = realloc(v->data, sizeof(int) * v->capacity);
    }
    v->data[v->size++] = value;
}

Key Standard Libraries

  • stdio.h: Input and output operations.
  • stdlib.h: Memory management, process control.
  • string.h: String manipulation (strcpy, strlen).
  • math.h: Common mathematical functions (sin, sqrt).

7. File Operations

Reading and writing to files is handled through the FILE pointer.

FILE *fp;
fp = fopen("notes.txt", "w"); // Open for writing

if (fp != NULL) {
    fprintf(fp, "Hello C Programming!\n");
    fclose(fp); // Close the file
}

// Reading from a file
fp = fopen("notes.txt", "r");
char buffer[100];
if (fp != NULL) {
    while (fgets(buffer, 100, fp)) {
        printf("%s", buffer);
    }
    fclose(fp);
}