In the previous lesson, we dismantled the greatest program in the world, claiming that mastering it means you have grasped 60% of C language. In this lesson, we will discuss some key points related to C language.
Review of Last Lesson’s Assignment
In the last class, we left an assignment. I wonder if everyone completed it. It was simply to type out the Hello World program and replace “Hello World” in the printf() function with your own name.
If my name is Zhang San, the program would look like this:
After compiling and running the program, the result would be as follows:
Tokens in C
Tokens are the building blocks of C language. A token can be a keyword (we will discuss 32 keywords later, but there are actually more than 32), an identifier, a constant, a string value, or a symbol, space, or even a comment, all belong to tokens.
1. Identifiers
C identifiers are used to name variables, functions, or any other user-defined items. Identifiers must start with an English character or an underscore (_), and can be followed by any number of letters, digits, or underscores.
If you do not understand what identifiers are used for, that’s okay; it will be explained in detail later. Just remember it for now.
Punctuation characters are not allowed in C identifiers, such as @, $ etc. Also, identifiers are case-sensitive, meaning A and a are two different identifiers.
Let’s look at a few examples:
C aaa _e k498 45 &de char
Which of the above identifiers are correct?
From our analysis, it seems like all except 45 and &de are correct. But that’s not the case; char is not a valid identifier because it is one of the 32 reserved keywords in C language, which has a special meaning and cannot be used as an identifier.
Someone might ask, what if I just started learning and don’t know which are keywords? What if I accidentally conflict with them?
Haha, I had the same question when I first started. Actually, you don’t need to worry about it, because generally, when a keyword is used, your compiler will detect it and highlight it in a special color. For example, in the last lesson, the keyword int was displayed in blue.
Even if there is no special color, it’s okay; the compiler will report an error during compilation. We can follow the hints in the build log to find the error and change it to something else.
2. Spaces
A line that contains only spaces is called a blank line, which may contain comments, and the C compiler will completely ignore it.
For example, in int main(); there is a space between int and main, which separates the parts of the statement. Just like reading an English article, spaces help the compiler identify where one element (like int) ends and the next begins.
How to Write Good Programs
We must always remember one truth — programs are meant to be read by people.
Many people don’t understand; they write programs for themselves to accomplish a task, focusing on the outcome, not caring about how others see it. But that’s incorrect. A good program should be readable; it is merely used to execute tasks.
Many people around me (including myself) sometimes look back at their previous programs and the first reaction is confusion: Wow, what on earth did I write back then?
This is a typical case of not having proper comments, leading to forgetting what was written. Therefore, comments are very important because sometimes they are not just for you. For example, if a team is working on a project, which is divided into smaller tasks, everyone has their role. When it’s time to compile everything, they need to look at the code written by their colleagues. If you find that one colleague’s code is hard to understand, with no comments and a messy format, what can you do?
Since you can’t understand it, you have to ask them what that part does. Sometimes everyone is busy and you feel frustrated looking at someone else’s messy code, which can significantly affect the team’s work efficiency and mood.
So, the format of the code and comments are very important.
Here are a few suggestions:
1. One statement per line: Once you finish writing a statement and add a semicolon, move to the next line. This is the most basic requirement.
2. Use more spaces: Spaces give a sense of simplicity. If your code is cramped like an English essay, no one will want to read it. For example: ①int a = 0; ②int a=0; both define a variable a, but the first method is much better. You might not see it with one line, but let’s look at an example.
Clearly, both programs calculate a + b, but the readability of the code in the first image is much better than in the second image. The second image has asymmetrical code, and the presence of code after the semicolon makes it look redundant. The lack of space around the “=” sign makes it feel crowded. Using spaces to make the code look good, isn’t that nice?
The space bar is long for a reason; it’s meant to be used!
3. Naming identifiers: This is particularly important. Many beginners tend to define a variable with arbitrary names, like a, b, ss, qwer, etc. This is fine if the program is simple, but if it’s complex, it’s not recommended. Especially when the identifier is used frequently, it can get confusing, and with many variables, you end up lost.
So, try to name clearly. For example, if you are defining a counting variable, you can name it count. If it’s for a condition check, you can name it condition_flag. Using English words is the best approach. Of course, if you don’t like underscores (_), you can use camel case. In any case, it’s much better than meaningless identifiers like qwer or df.
4. Look at how experts write code: We can learn from good code, see how well-written code is structured, and develop our own style.
5. Practice, practice, practice! This is the only way to write good code; the more you practice, the better you understand how to write it.
In summary, we must maintain good habits and gradually change bad ones.
Conclusion
Thus, we have reached the end of the first chapter. We have parsed the largest program in the world and understood the basic structure of a program while introducing what a good program is. Of course, there are still some things I haven’t covered, but don’t worry, take it slow; you can’t eat a whole fat man in one bite. For example, we will explain what int and the keyword char mentioned in this and the last lesson are for in the next chapter. Stay tuned!
Assignment for This Lesson
Among the identifiers listed below, which do you think are valid, which are invalid, and which are better identifiers?
Char, ¥god, MoveStutas, 87sd, fs, h, hello_flag, _suc, int, voe
Reminder: There is a strange link in the first lesson; remember to save it!