Crash Course in C Programming

C is a powerful, low-level yet portable systems programming language created by Dennis Ritchie in 1972 at Bell Labs. It combines high-level structure with near-assembly efficiency, making it ideal for operating systems (e.g., Unix), embedded devices, and performance-critical software. This crash course covers the essentials—structure, data types, operators, functions, and compilation—without rewriting the classic "K&R" book.

Basic Program Structure

A minimal C program:

#include <stdio.h>   // Include standard I/O library

int main() {          // Entry point; returns int
    printf("Hello, world!\n");  // Print and newline
    return 0;         // Success exit code
}

Data Types

C has built-in types; sizes vary by architecture (typically 32/64-bit):

TypeSize (typical)RangeUse
char1 byte-128 to 127 or 0-255Characters, small numbers
int4 bytes-2^31 to 2^31-1Integers
short2 bytes-32,768 to 32,767Smaller integers
long4-8 bytesPlatform-dependentLarger integers
float4 bytes~6 decimal digitsSingle-precision floating point
double8 bytes~15 decimal digitsDouble-precision

Modifiers: signed/unsigned (for negative range), const (immutable), volatile (hardware registers).

Operators

Control Structures

if (x > 0) {
    // do something
} else {
    // alternative
}

for (int i = 0; i < 10; i++) {
    // loop 10 times
}

while (condition) {
    // repeat while true
}

Functions

Functions modularize code:

int add(int a, int b) {   // Declaration + definition
    return a + b;
}

int main() {
    int sum = add(5, 3);
    printf("%d\n", sum);
    return 0;
}

Prototype before main if defined later: int add(int, int);

Pointers and Arrays

Pointers store memory addresses—key to C's power:

int x = 10;
int *p = &x;    // p points to x
printf("%d\n", *p);  // Dereference: prints 10

Arrays: int arr[5] = {1,2,3,4,5}; arr[0] == *(arr)

Compiling on the Command Line

Use GCC (GNU Compiler Collection) or Clang:

gcc hello.c -o hello      // Compile to executable "hello"
./hello                   // Run on Unix/Linux/macOS
hello.exe                 // On Windows

Flags: -Wall (warnings), -O2 (optimize), -g (debug info).

Key Facts and Tips

This crash course gives you enough to write simple programs and understand vintage/modern C code. Practice with small examples—compile, run, experiment!

Back to Misc


Copyright 2026 - MicroBasement