Course contentsShow
← C
Lesson 801 of 1,21911. The Preprocessor and BuildFree lesson

Naming conventions for large projects

Naming conventions for large projects

What you'll learn: How to use prefixing and consistent naming to prevent variable name collisions when building projects with multiple files.

The "Town of Three Johns" Problem

In a small, single-file program, naming a variable count is fine. You know exactly what it’s counting. But as your project grows into dozens of .c files, you might have one file handling Network connections and another handling User profiles. If both files define a global variable or function named update(), the compiler will get confused, and the linker will throw a "multiple definition" error.

Think of it like a small town. If there is only one person named John, you just call him "John." If the town grows and three more Johns move in, you have to start calling them "Baker John," "Fisher John," and "Smith John." In C, we do this using prefixes.

The Prefix Strategy

To keep your code organized and "clash-proof," pick a short, unique 2–4 letter prefix for each module (file) in your project. If you are writing a module for managing a motor, every public function and global variable should start with MOT_ or motor_.

This makes it immediately obvious where a piece of code comes from when you see it used elsewhere in the project.

// --- file: player.h ---
// We use the "PLY_" prefix for all Player module members
void PLY_init(int health);
void PLY_update_position(int x, int y);

// --- file: enemy.h ---
// We use the "ENM_" prefix for all Enemy module members
void ENM_init(int difficulty);
void ENM_update_position(int x, int y);

// --- file: main.c ---
int main() {
    // Even though both "update" the position, there is no confusion
    PLY_update_position(10, 10);
    ENM_update_position(50, 50);
    return 0;
}

Why This Matters

When you use a library written by someone else, you’ll notice they do this constantly. The OpenGL library prefixes everything with gl (e.g., glVertex3f), and the SDL library prefixes everything with SDL_.

By adopting this habit early, you ensure that your "Module A" can never accidentally overwrite a function in "Module B." It also makes your code searchable; if you want to find every function related to the player, you simply search your project for PLY_.

Key takeaway

Use unique, module-specific prefixes (like UI_ or NET_) for all public functions and global variables to prevent naming conflicts in large projects.