Naming conventions for constants
What you'll learn: How to use the SCREAMING_SNAKE_CASE style to make your unchangeable values stand out from regular variables.
The "Caution Tape" of Coding
In C, variables are like dry-erase boards—you write a value down, rub it out, and write a new one later. Constants, however, are like stone carvings. Once you define a const, its value is locked for the duration of the program.
Because constants behave differently than variables, programmers developed a visual shorthand to tell them apart at a glance. We use a style called SCREAMING_SNAKE_CASE. This means every letter is capitalized, and words are separated by underscores.
Think of it like road signs. A "STOP" sign is bold and loud because the instruction is fixed and vital. A street name sign might be in lowercase because it’s just information, but a "DANGER" sign grabs your attention immediately.
Why shout?
Imagine you are reading a 500-line file. You see a variable named limit. Is it okay to change limit = 100 halfway through the code? If you see MAX_USER_LIMIT in all caps, your brain instantly registers: "This is a constant. I should not (and cannot) try to change this value."
Seeing it in action
Here is how you differentiate between a regular, changeable variable and a fixed constant using proper naming conventions:
#include <stdio.h>
int main() {
// A constant: All caps, underscores between words
const int MAX_OXYGEN_LEVEL = 100;
const float PI_APPROXIMATION = 3.14159;
// A regular variable: Lowercase (camelCase or snake_case)
int currentLevel = 50;
printf("The ceiling is %d.\n", MAX_OXYGEN_LEVEL);
// We can change currentLevel, but we leave the "screaming"
// constants alone to do their job!
currentLevel = 65;
return 0;
}
Consistency is key
While the C compiler won't force you to use uppercase (it will let you name a constant int pi = 3;), doing so makes your code much harder for humans to read. By using SCREAMING_SNAKE_CASE, you provide a "visual map" that helps other developers navigate your logic without accidentally trying to overwrite values that are meant to be permanent.
Key takeaway
Use ALL_CAPS_WITH_UNDERSCORES for constants to clearly signal that their values are permanent and should not be modified.