Loading lessons...
C Identifiers (Variable Names)
C Identifiers (Variable Names)
Every name you invent in C is called an identifier. There are rules about what counts as a valid one.
The naming rules
An identifier must follow these laws:
- Can use letters (
a-z,A-Z), digits (0-9), and the underscore_. - Cannot start with a digit.
1valueis illegal;value1is fine. - Cannot contain spaces or most punctuation (
-,+,?, etc.). - Cannot be a keyword of the language (like
int,if,return). - Case-sensitive:
Scoreandscoreare two different names.
Valid vs invalid
int myNumber; // good
int _temp; // good (though leading _ has special rules)
int value2; // good
int 2value; // BAD: starts with a digit
int my-number; // BAD: dash is not allowed
int int; // BAD: int is a keyword
Best practices
- Use descriptive names that say what the variable holds:
maxSpeed, notm. - Use snake_case for multi-word names:
player_health. - Be consistent - pick a style and keep it.
Reserved keywords
A keyword is a word the language keeps for itself. Some examples:
int, if, else, for, while, return, void, const, break, case
TL;DR
- Identifiers use letters, digits, and
_; they can't start with a digit. - No spaces, no punctuation like
-, no keywords. - C is case-sensitive:
Score!=score. - Prefer clear, descriptive, consistent names.