C Fundamental Programming

Explore ESE 124 programming examples, common errors, exercises and solutions, from basic setup to finite state machines and data structures.

Open the original interactive C programming guide · Open C course notes and reference materials

An illuminated blue brain graphic surrounded by digital patterns.

Source code notes

The source guide contains malformed or truncated examples: function pointers show “⊂” and “÷”; the queue example is truncated after “if (count”; the dynamic-memory example and solution stop within a for loop; and several include directives have an extra >. These examples are preserved as supplied. Consult the original course resource for clarification.

Exam downloads

ESE 124 exam resources
Term Exam PDF
Summer 2023 Midterm Open Summer 2023 Midterm (PDF)
Summer 2023 Final Open Summer 2023 Final (PDF)
Summer 2024 Final Open Summer 2024 Final (PDF)
Summer 2025 Midterm Open Summer 2025 Midterm (PDF)
Summer 2025 Final Open Summer 2025 Final (PDF)
Fall 2023 Midterm Open Fall 2023 Midterm (PDF)
Fall 2023 Final Open Fall 2023 Final (PDF)
Fall 2024 Midterm Open Fall 2024 Midterm (PDF)
Fall 2024 Final Open Fall 2024 Final (PDF)
Fall 2025 Midterm Open Fall 2025 Midterm (PDF)
Fall 2025 Final Open Fall 2025 Final (PDF)
Spring 2024 Midterm Open Spring 2024 Midterm (PDF)

Programming examples and solutions

Choose a topic group below. Each group retains the source examples, exercises and complete visible solutions.

Learning topics

Basic Setup

#include <stdio.h>

int main() {
    printf("Hello ESE 124!\n");
    return 0;
}

Common errors

  • Missing #include <stdio.h>
  • Typo in main or missing return 0;

Exercise: Modify the program to ask for the user's name and print "Hello, <name>!".

Solution

#include <stdio.h>

int main() {
    char name[50];
    printf("Enter your name: ");
    scanf("%49s", name);
    printf("Hello, %s!\n", name);
    return 0;
}

Data Types & ASCII

int age;
float temp;
char letter;
printf("ASCII of A is %d\n", 'A');

Common errors

  • Using wrong specifier: %f vs %d.
  • Buffer overflow when reading char name[].

Exercise: Print ASCII values of characters '0' through '9'.

Solution

#include <stdio.h>

int main() {
    for (char c = '0'; c <= '9'; c++) {
        printf("%c = %d\n", c, c);
    }
    return 0;
}

Control Statements

int x;
printf("Enter a number: ");
scanf("%d", &x);

if (x % 2 == 0) {
    printf("Even\n");
} else {
    printf("Odd\n");
}

Common errors

  • Missing braces { } for multi-line blocks.
  • Using = instead of == in conditions.

Exercise: Create a switch-case to print the day of the week for 1–7.

Solution

#include <stdio.h>

int main() {
    int d;
    scanf("%d", &d);
    switch (d) {
        case 1: printf("Monday\n"); break;
        case 2: printf("Tuesday\n"); break;
        case 3: printf("Wednesday\n"); break;
        case 4: printf("Thursday\n"); break;
        case 5: printf("Friday\n"); break;
        case 6: printf("Saturday\n"); break;
        case 7: printf("Sunday\n"); break;
        default: printf("Invalid\n");
    }
    return 0;
}

Arrays

int nums[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
    printf("%d ", nums[i]);
}
printf("\n");

Common errors

  • Index out of range (e.g., i <= 5).

Exercise: Write a function to find the maximum value in an array.

Solution

int find_max(int a[], int n) {
    int mx = a[0];
    for (int i = 1; i < n; i++) {
        if (a[i] > mx) mx = a[i];
    }
    return mx;
}

int main() {
    int arr[] = {3, 7, 2, 9, 5};
    printf("Max = %d\n", find_max(arr, 5));
    return 0;
}

Functions

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

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

Common errors

  • Missing prototype if function defined after main.

Exercise: Implement a recursive function to compute factorial(n).

Solution

long factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    printf("5! = %ld\n", factorial(5));
    return 0;
}

Pointers

int x = 10;
int *p = &x;
printf("Value = %d\n", *p);

Common errors

  • Dereferencing a NULL or uninitialized pointer.

Exercise: Write a function to swap two integers via pointers.

Solution

swap(int *a, int *b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int main() {
    int a = 5, b = 7;
    swap(&a, &b);
    printf("a=%d, b=%d\n", a, b);
    return 0;
}

Function Pointers

#include <stdio.h>

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }

int main() {
    int (*op)(int, int);
    op = &add;
    printf("5 + 3 = %d\n", op(5, 3));
    op = ⊂
    printf("5 - 3 = %d\n", op(5, 3));
    return 0;
}

Common errors

  • Mismatched signature for function pointers
  • Omitting parentheses in calls: op(...)

Exercise: Write mul and divide, select via an array of pointers.

Solution

#include <stdio.h>

int mul(int a, int b) { return a * b; }
int divide(int a, int b) { return a / b; }

int main() {
    int (*ops[2])(int, int) = { &mul, ÷ };
    printf("6 * 7 = %d\n", ops[0](6, 7));
    printf("6 / 2 = %d\n", ops[1](6, 2));
    return 0;
}

File I/O

FILE *f = fopen("data.txt", "w");
fprintf(f, "Hello File\n");
fclose(f);

Common errors

  • Not checking fopen return for NULL.

Exercise: Read integers from input.txt until EOF and print their sum.

Solution

#include <stdio.h>

int main() {
    FILE *f = fopen("input.txt","r");
    if (!f) return 1;
    int sum=0, x;
    while (fscanf(f,"%d",&x)==1) sum += x;
    fclose(f);
    printf("Sum = %d\n", sum);
    return 0;
}

Bubble Sort

bubble(int a[], int n) {
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - i - 1; j++)
            if (a[j] > a[j + 1]) {
                int tmp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = tmp;
            }
}

Common errors

  • Loop bounds off by one.

Exercise: Implement insertion sort.

Solution

insertion(int a[], int n) {
    for (int i = 1; i < n; i++) {
        int key = a[i], j = i - 1;
        while (j >= 0 && a[j] > key) {
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = key;
    }
}

Data Structures

Stack
#define MAX 10
int stack[MAX], top=-1;

push(int v) { stack[++top]=v; }
pop()    { return stack[top--]; }

Common errors

  • Stack overflow/underflow.

Exercise: Using the Queue ADT, enqueue [1,2,3,4,5] then dequeue all and print.

Solution

#include <stdio.h>>
#include "queue.h"

int main() {
    Queue *q = queue_create(5);
    for (int i=1; i<=5; i++) enqueue(q,i);
    for (int i=0; i<5; i++)
        printf("%d ", dequeue(q));
    printf("\n");
    queue_destroy(q);
    return 0;
}

Compile:

gcc main.c queue.c -o a.exe

FSM: “FRFR” Detector (Mealy)

#include <stdio.h>>
#include <string.h>>

enum State { S0,S1,S2,S3,S4 };
enum In { F,R,X };

get_in(char c){
    if(c=='F')return F;
    if(c=='R')return R;
    return X;
}

int main() {
    State st=S0;
    char s[1000]; int cnt=0;
    State nxt[5][3] = {
        {S1,S0,S0},{S1,S2,S0},{S3,S0,S0},{S1,S4,S0},{S3,S0,S0}
    };
    int out[5][3] = {
        {0,0,0},{0,0,0},{0,0,0},{0,1,0},{0,0,0}
    };
    printf("Input: "); scanf("%s",s);
    for(int i=0;s[i];i++){
        int in=get_in(s[i]);
        cnt += out[st][in];
        st = nxt[st][in];
    }
    printf("FRFR count=%d\n",cnt);
    return 0;
}

Exercise: Adapt it to detect “RFRF” instead.

Solution

// Swap transitions and output bits for F↔R in nxt and out arrays.

Dynamic Memory Allocation

#include <stdio.h>>
#include <stdlib.h>>

int main() {
    int n=5;
    int *arr = malloc(n * sizeof *arr);
    for(int i=0;i

Exercise: Allocate 10 strings (20 chars each), read & print names.

Solution

#include <stdio.h>>
#include <stdlib.h>>

int main() {
    int n=10;
    char (*names)[20] = malloc(n * sizeof *names);
    for(int i=0;i
/>

Structures

struct Point { int x,y; };

int main() {
    struct Point p={10,20};
    printf("(%d,%d)\n",p.x,p.y);
    return 0;
}

Exercise: Define a Student struct (name,id,GPA), create & print one.

Solution

struct Student {
    char name[50];
    int id;
    double gpa;
};

int main() {
    struct Student s={"Alice",12345,3.8};
    printf("%s %d %.2f\n",s.name,s.id,s.gpa);
    return 0;
}

Bitwise Manipulation

unsigned a = 5;       // 0b0101
unsigned b = a << 1;  // 0b1010 == 10
unsigned c = a & 3;   // 0b0101 & 0b0011 == 1
printf("b=%u, c=%u\n", b, c);

Exercise: Write a function to check if the nᵗʰ bit of x is set.

Solution

is_bit_set(unsigned x, int n) {
    return (x >> n) & 1;
}

int main() {
    unsigned v=13; // 0b1101
    for(int i=0;i<4;i++){
        printf("bit %d of %u: %d\n", i, v, is_bit_set(v,i));
    }
    return 0;
}

Course note references

Jenny Chen’s original ESE 124 notes include additional explanations, code examples, exercises and solutions. Choose a topic below to open the original resource.

Foundations

Midterm review and basic C · ASCII · Data types · Control statements · Bitwise operations · Arrays and strings · Bubble sort

Functions and memory

Functions and recursion · Pointers · Dynamic memory · Function pointers

Systems and data

Finite state machines · Text file scanning · Data structures · Abstract data types

Practice and reference

Exercises and review · Terminal basic guide · C cheat sheet · Bitwise, math and control statements quiz

Notes on the original resources

  • The control-statements note displays the sine series with a first correction of −x³/3!, while its code updates the first term using (2*k+2)*(2*k+3) with k starting at 1. The formula, code and displayed result are inconsistent.
  • The bitwise note’s Question 9 uses num ^ (1>>n) to toggle a bit; its earlier example uses value ^= (1 << position). These expressions differ.
  • The Functions, vending-machine example and Function Pointers notes contain embedded GitHub resources that returned 404 errors during review. The original notes remain accessible through the topic links above.

These source discrepancies are identified here without changing the original technical examples.