
In the last lesson, we discussed the “past and present” of the C language and the art of “white space” in code. Today, we will explore a more fundamental and extremely important concept—identifiers.
When you hear the term “identifier”, does it sound a bit academic and hard to grasp? Don’t worry! Simply put, it is the “art of naming” in the world of C language.
Every function name and variable name you write, such as the well-known printf, is actually an identifier. Why can’t we name a variable 007? Why is _name allowed, but -name is not?
There is a simple yet strict set of “rules of the trade” behind this. In this lesson, let me use an analogy you absolutely wouldn’t expect to help you thoroughly understand the naming conventions in C language, so that your code can be “properly named” from now on!
01
Why Do Codes Need Names?
Imagine going to a hospital for treatment. If there were no names like “Radiology” or “Ultrasound Room”, only “Department A” and “Department B”, could you tell which one is for X-rays and which one is for examinations? Definitely not.
Similarly, in the programming world, if every function were just a string of machine code like 010101, programmers wouldn’t be able to work.
// Without names, calling the "print" function might look like this:// 0101010111001010(...); Who knows what that is!
This would be a disaster! Just like we call our friends by their names instead of reciting their long ID numbers, programming also needs to use human-understandable “names” to refer to various functions and data.
printf is such a name. When we write printf, both the compiler and we know: oh, something is going to be output to the screen!
Deepening Understanding
This “name” has a unified technical term in C language and almost all programming languages, called identifier. Its root is identify (to recognize). As the name suggests, the role of an identifier is to give each entity in the program (variables, functions, types, etc.) a unique and recognizable name.
02
C Language Naming Rules
Naming things in code is not as casual as choosing a username for ourselves.The C language international organization has established a set of rules that must be followed for naming identifiers.
Let’s see what the official Microsoft documentation says. Don’t be afraid of those complex syntax diagrams,we’ll translate it into plain language: there are three rules:
What must the “surname” be?: The first character of the name must be a letter (A-Z, a-z) or an underscore (_).
What can the “name” include?: From the second character onward, it can be letters, underscores (_), or digits (0-9).
Keywords are not allowed!:The C language reserves some names (like int, return, if, else, etc.), which are “protected names” that we cannot use.
Let’s look at some examples:
// ✅ Correct namingint age;int user_name;int _temp;int level9;// ❌ Incorrect namingint 007agent; // Cannot start with a digitint user-name; // Cannot contain hyphens (-) or other special charactersint return; // Cannot use keywords
03
“Hundred Family Surnames” Naming Method
Feel like the rules are a bit dry? Don’t worry, let me show you a big trick!
We can imagine the naming rules of C language as the “surname + name” structure of Chinese names.
-
Surname(first character): Just like the first character of a C language identifier. In our “Hundred Family Surnames”, we have “Zhao, Qian, Sun, Li”, but there is no surname “1” or “@”.
The “Hundred Family Surnames” in C language is even simpler, consisting of only two types: letters (a-z, A-Z) and underscores (_).
-
Name(subsequent characters): Just like the part of our name. In names, numbers like “two” and “three” can appear (for example, “Zhang San”, “Wang Ermazi”), but have you ever seen anyone with the surname “two”? No.
The “name” part in C language can include digits (0-9), and of course, letters and underscores can still be used.
With this correspondence, doesn’t it become clear instantly?
| Rule | “Hundred Family Surnames” Analogy | C Language Example |
| Must be a surname from the “Hundred Family Surnames” | Must be a-z, A-Z, _ | |
| Names can include digits | Can be a-z, A-Z, _, 0-9 |
When you want to name a variable 2players, you can immediately realize: “No, there is no surname ‘2’ in the Hundred Family Surnames!” This way, you will never make a mistake.
Professional Supplement and Tips
The minus sign (-), which looks similar to an underscore, is absolutely not allowed in identifiers. In C language, it is used as a subtraction operator. user-name would be interpreted by the compiler as “variable user minus variable name”, which usually leads to syntax errors.
Additionally, the underscore _ is a completely legal “surname”. Therefore, naming like _my_var is perfectly correct.
04
Summary
Alright, today’s “art of naming class” ends here. Through a series of vivid analogies, we have thoroughly understood the seemingly profound concept of identifiers.
Remember these points, and your code naming level will directly improve:
-
Everything has a name: An identifier is the “name” given to variables, functions, etc., aimed at making the code more readable and understandable.
-
Rules are paramount:The naming rules in C language are simple and strict, and must be followed.
-
Skillfully use the “Hundred Family Surnames”: Remembering our “Hundred Family Surnames” analogy will help you easily master all naming rules, avoiding compilation failures due to naming errors.
