Skip to content

Module 9 · Bitwise Operations, Common Errors and Applications — topic 3 of 3

Practical Applications of C

Bringing the course together: how C is used in operating systems, embedded devices, databases and tools, and a complete worked mini-application — a record manager — that combines structs, files, functions and dynamic memory.

Beginner 18 min read·Lesson 37 of 37 in C Programming

Learning objectives

  • Describe where C is the right tool and where it is not
  • Design a small application as data structures plus operations
  • Combine structs, dynamic arrays and files in one program

Definition

Practical application of C means combining its core features — types, control flow, functions, arrays and strings, pointers and dynamic memory, structures and file I/O — to build complete programs that solve real problems reliably.

Introduction

You now know enough C to build real software. This lesson steps back to look at where C is used professionally, then walks through a complete program that uses nearly every topic in the course.

The same pattern — define the data with structs, write small functions for each operation, keep data in a growable array, save and load it from a file — is the backbone of the library, bank, inventory and hospital projects in the project library.

Why it matters

Knowing C's strengths (speed, control, portability, tiny runtime) and weaknesses (manual memory, no bounds checks) lets you choose it — or another language — wisely.

Seeing a complete, well-structured program is the bridge between exercises and projects.

Detailed explanation

DomainWhy CExamples
Operating systemsdirect hardware access, no runtimeLinux, Windows NT kernel, FreeRTOS
Embedded / IoTtiny memory, predictable timingcar ECUs, pacemakers, washing machines
Databases and storageperformance and control of memorySQLite, PostgreSQL, Redis
Language runtimesspeed, portabilityCPython, Ruby MRI, Lua
Networkinglow-level packet handlingnginx, curl, OpenSSL
Toolsfast start-up, portablegit, make, coreutils

Design approach: 1. Identify the entities and represent each as a struct. 2. List the operations (add, list, search, update, delete, save, load). 3. Write one function per operation with clear inputs and outputs. 4. Store records in a dynamic array that grows with realloc. 5. Persist to a file so data survives restarts.

When not to use C: for quick scripts, data analysis or web front ends, languages with automatic memory management and rich libraries (Python, Java, JavaScript) are usually more productive. C++ adds abstractions while keeping C's performance.

Syntax

C
typedef struct { int id; char name[32]; double marks; } Record;
typedef struct { Record *items; size_t count, cap; } Table;
int table_add(Table *t, Record r);
Record *table_find(Table *t, int id);
int table_save(const Table *t, const char *path);
int table_load(Table *t, const char *path);
void table_free(Table *t);
  • Each operation is a function taking a pointer to the table.
  • The table owns its array and frees it in table_free.
  • Functions return status codes so callers can handle errors.

Code examples

Embedded-style control loop (simulated)

C
#include <stdio.h>
/* Simulated sensor readings; on a microcontroller these would come from an ADC register. */
static const int readings[] = {22, 24, 27, 31, 29, 25};
int main(void) {
int fan_on = 0;
for (int t = 0; t < 6; t++) {
int temp = readings[t];
if (!fan_on && temp >= 30) fan_on = 1; /* switch on above 30 */
else if (fan_on && temp <= 26) fan_on = 0; /* hysteresis: off below 26 */
printf("t=%d temp=%d fan=%s\n", t, temp, fan_on ? "ON" : "off");
}
return 0;
}
Output
t=0 temp=22 fan=off
t=1 temp=24 fan=off
t=2 temp=27 fan=off
t=3 temp=31 fan=ON
t=4 temp=29 fan=ON
t=5 temp=25 fan=off

A tiny text-processing tool

C
#include <stdio.h>
#include <ctype.h>
int main(void) {
const char *text = "C is small, fast and everywhere.";
int letters = 0, spaces = 0, punct = 0;
for (const char *p = text; *p; p++) {
if (isalpha((unsigned char)*p)) letters++;
else if (isspace((unsigned char)*p)) spaces++;
else if (ispunct((unsigned char)*p)) punct++;
}
printf("letters=%d spaces=%d punctuation=%d\n", letters, spaces, punct);
return 0;
}
Output
letters=25 spaces=5 punctuation=2

More examples

A complete record manager

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct { int id; char name[32]; double marks; } Record;
typedef struct { Record *items; size_t count, cap; } Table;
int table_add(Table *t, Record r) {
if (t->count == t->cap) {
size_t cap = t->cap ? t->cap * 2 : 4;
Record *tmp = realloc(t->items, cap * sizeof *tmp);
if (!tmp) return 0;
t->items = tmp;
t->cap = cap;
}
t->items[t->count++] = r;
return 1;
}
Record *table_find(Table *t, int id) {
for (size_t i = 0; i < t->count; i++)
if (t->items[i].id == id) return &t->items[i];
return NULL;
}
int table_save(const Table *t, const char *path) {
FILE *fp = fopen(path, "wb");
if (!fp) return 0;
int ok = fwrite(&t->count, sizeof t->count, 1, fp) == 1 && fwrite(t->items, sizeof *t->items, t->count, fp) == t->count;
fclose(fp);
return ok;
}
int table_load(Table *t, const char *path) {
FILE *fp = fopen(path, "rb");
if (!fp) return 0;
size_t n;
int ok = fread(&n, sizeof n, 1, fp) == 1;
for (size_t i = 0; ok && i < n; i++) {
Record r;
ok = fread(&r, sizeof r, 1, fp) == 1 && table_add(t, r);
}
fclose(fp);
return ok;
}
void table_free(Table *t) { free(t->items); t->items = NULL; t->count = t->cap = 0; }
void table_print(const Table *t) {
for (size_t i = 0; i < t->count; i++)
printf("%3d %-10s %6.1f\n", t->items[i].id, t->items[i].name, t->items[i].marks);
}
int main(void) {
Table t = {0};
table_add(&t, (Record){1, "Asha", 88.5});
table_add(&t, (Record){2, "Vikram", 74.0});
table_add(&t, (Record){3, "Lakshmi", 91.0});
Record *r = table_find(&t, 2);
if (r) r->marks = 79.5; /* update through a pointer */
if (!table_save(&t, "records.dat")) { table_free(&t); return 1; }
table_free(&t); /* prove the file holds the data */
Table loaded = {0};
if (!table_load(&loaded, "records.dat")) return 1;
printf("loaded %zu records\n", loaded.count);
table_print(&loaded);
table_free(&loaded);
return 0;
}
Output
loaded 3 records
  1  Asha         88.5
  2  Vikram       79.5
  3  Lakshmi      91.0

Step-by-step explanation

  • Record models one entity; Table is a dynamic array that owns its records.
  • table_add grows the array by doubling with realloc and checks for failure.
  • table_find returns a pointer into the array, so the caller can update a record in place.
  • table_save writes the count followed by the records; table_load reads them back, re-using table_add.
  • Freeing the table before loading proves the data really came back from the file.
  • (Record){1, "Asha", 88.5} is a compound literal (C99) — a temporary struct value.

Expected output

Running “A complete record manager” prints exactly:

TextOutput
loaded 3 records
1 Asha 88.5
2 Vikram 79.5
3 Lakshmi 91.0

Practical applications

  • SQLite — the most widely deployed database — is a single C library.
  • Every phone's baseband and Wi-Fi firmware contains C.
  • The project library's management systems extend this exact design with menus, search and deletion.

Best practices

  • Design data structures first, then operations.
  • Give every resource an owner and a cleanup function.
  • Test save/load round trips as part of every file-based project.

Common mistakes

Returning pointers that a later realloc invalidates

Avoid

Record *r = table_find(&t, 2);
table_add(&t, x); /* may move the array */
r->marks = 0; /* r may dangle */

Prefer

table_add(&t, x);
Record *r = table_find(&t, 2);
r->marks = 0;

realloc can move the block, invalidating every pointer into it. Look elements up again after adding.

Writing structs containing pointers to a file

Avoid

typedef struct { char *name; } Rec; /* fwrite saves only the address */

Prefer

typedef struct { char name[32]; } Rec; /* fwrite saves the characters */

A pointer's value is meaningless in another run of the program. Store data inline or serialise it explicitly.

Important notes

  • The binary format above depends on the compiler's struct layout; for files shared between machines, write a text format such as CSV.
  • Large applications often wrap C cores with higher-level languages (Python's C extensions, Java's JNI).

Key takeaways

  • C excels where performance, control and small footprints matter.
  • Model entities as structs; implement each operation as a function.
  • Dynamic arrays plus file persistence form the core of most C applications.

Try it yourself

  1. 1
    Extend the record manager with table_delete(Table *t, int id) that removes a record and shifts the rest.
  2. 2
    Add a text export that writes the table as CSV and a function that reads it back.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

3 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhich area is C most widely used in?
  • ModerateA record manager keeps a pointer to an element of a dynamic array, then adds more records with realloc. Why might the saved pointer become invalid?
  • AdvancedWhy are structs written with `fwrite` not a good format for files shared between different machines?
Practise this topic