Naming rules and identifiers
What you'll learn: You will learn the specific naming restrictions in C so you can create valid "identifiers" for your data.
The Label on the Jar
Imagine you are organizing a kitchen. You have many identical jars, so you put labels on them like "Salt," "Sugar," or "Flour." In C, a variable is like one of those jars, and the name you give it is called an identifier.
However, C is a bit pickier than a kitchen label maker. While you might be tempted to name a variable Total $ Amount or 2nd_Place, the C compiler will throw an error. There are three simple rules you must follow to keep the compiler happy.
The Three Golden Rules
To create a valid identifier, you must stick to these requirements:
- Characters allowed: You can only use letters (A-Z, a-z), digits (0-9), and underscores (
_). No spaces, no hashtags, and no emojis. - The first character: A name cannot start with a digit. It must start with a letter or an underscore. For example,
points2is fine, but2pointsis illegal. - Reserved words: You cannot use words that C has already claimed for itself. Words like
int,float, andreturnare "Keywords"—they are the building blocks of the language's logic, so they can't be used as names for your variables.
Case Sensitivity
In C, the compiler is very literal. It treats uppercase and lowercase letters as completely different things. If you create a variable named Score, one named score, and another named SCORE, C sees them as three entirely separate jars. Most programmers stick to lowercase or snake_case (using underscores) to keep things readable.
// Valid names
int player_age = 25;
int _systemStatus = 1;
int health2 = 100;
// Invalid names (these would cause errors)
// int 2ndPlace = 10; <- Starts with a number
// int total-sum = 5; <- Hyphens are not allowed
// int int = 20; <- "int" is a reserved keyword
Key takeaway
Identifiers must start with a letter or underscore and contain only alphanumeric characters, remembering that Apple and apple are two different variables.