Recommended reading: The interview with Huan Dad in “Sanlian Life Weekly” titled “Slow is Fast: Mathematical Olympiad, a Mental Gymnastics”, WeChat public account reading link “How to Learn the Demonized Mathematical Olympiad?” This article discusses Huan Dad’s experience with Mathematical Olympiad and his views on it.
The first class in C language programming, most students with no background will copy from the textbook and run their first program.
After the first program, I said: Raise your hand if you passed on the first try!
Many students raised their hands slightly smugly.
My next sentence was clearly unexpected by them: Unfortunately, you have missed many opportunities to learn and improve.
Programming is a course that highly integrates theory and practice; only through practice and wrestling with mistakes can one grow quickly.
This semester, I am responsible for teaching this course, and there are always some common mistakes among beginners. As the course deepens, I plan to summarize and explain the common mistakes of students, hoping to help everyone.
In terms of error types, they are mainly divided into two categories: compilation errors and runtime errors, while link errors will be temporarily ignored.
Compilation errors are basically syntax errors, and the compiler can generally provide correct prompts; everyone just needs to learn to read the error messages gradually. Runtime errors are more troublesome; some belong to language-level type issues, while more are program logic problems.
(1) Writing main as mian
This is one of the most typical spelling errors. The main() function is the entry point of executable programs in C language. If you only compile on platforms like Linux, there is actually no problem. However, if you want to generate an executable file without a main function, it will report an error. On Windows platforms, you will see errors similar to the one below.

(2) Forgetting to write a semicolon at the end of a statement
Every statement in C language must end with a semicolon; forgetting to write a semicolon will result in errors similar to the one below.

(3) Mismatched parentheses or quotes
In C language, parentheses (), braces {}, and quotes “” must appear in pairs. If one is missing, the compilation will report an error.
For example, the following program is missing a }:
The compiler will give the following prompt:
To avoid this type of error, it is essential to develop a good coding style; indent where necessary so that matching is clear at a glance.
(4) Using a variable without declaring it
Any identifier (including variable names, function names, etc.) must be declared before use. If the variable fahr is not declared in the program, an error similar to the one below will occur.
At this point, we need to add a statement like the one below:
More often, it is because we defined a variable but made a spelling error when using it; for example, defined int fahr; but wrote farh when using it.
Note: There is a difference between declaration and definition, which will not be elaborated here.
(5) Errors caused by the division operator /
After executing the following program, celsius is not the expected value of 37.8 degrees, but celsius=0.000000. Why is that?
The reason lies in the division operation /.
Here, an important rule of C language operations is involved:
For any binary operator, the types of the two operands must be the same.
In the expression celsius=5/9*(fahr-32) above, the first step executes 5/9, where both operands are integers. For integers, the / operator performs integer division, and the result is still an integer, hence it is 0. Consequently, the value of the right side of the expression is 0. Finally, the assignment operation = also requires the data types on both sides to be consistent; at this point, a default type conversion occurs, converting the integer 0 on the right to a decimal before assigning it to celsius.
To correct the program, it is simple; just change at least one of 5 or 9 to a decimal, for example, changing 5 to 5.0 will yield the correct result.
Similar errors exist in the following code that calculates 1-1/3+1/5-1/7+… Can you find it?
Switching to Chinese input mode in the source code and entering characters, commonly seen with commas, semicolons, parentheses, and quotes, such as the semicolon at the end of the first line and the rightmost parenthesis of the second line in the code below.
At this point, the compiler will prompt similar errors, where characters like \243 are non-standard ASCII character codes indicated by escape characters.
Correcting the error is simple; just switch back to English input mode.
(7) Common errors in printf()
The f in printf refers to formatted, i.e., formatted output. This function has several parameters, with the first parameter providing the format of the output string, which includes ordinary characters and format control symbols starting with %, such as %d, %f, %c, etc. I like to call them placeholders.
In principle, for every % placeholder that appears, there must be a corresponding parameter behind it to replace this placeholder, and the types must match; otherwise, runtime will produce relatively “strange” results.
For example, the following code:
The reason is that celsius is of float type, but it is output as %d in printf(), which means that the 32 bits stored in celsius in floating-point format are interpreted as an integer for output, resulting in strange results.
To deeply understand this, it is necessary to say a few more words about data types.
All data is stored in binary form in computers, but each data has its corresponding data type.
The data type determines the following aspects:
(1) The size of the storage space occupied by the data;
(2) The encoding and storage format of the data;
(3) The operations that can be performed on the data.
Floating-point numbers have their encoding and storage format, integers have their encoding and storage format, strings have their storage format, and the output format must match the storage format; otherwise, incorrect output will occur.
In the example above, to get the correct result, simply change celsius=%d to celsius=%f, as shown below:
Another typical error is that the quotes directly include the entire string, as shown below:
In this case, the compiler will not report an error, but at runtime, unexpected results will occur, such as:
This printf() function clearly lacks two parameters; why can it still run (although incorrectly)?
To explain this, it is necessary to understand the stack and the way the printf() function is called; if interested, you can search for it yourself, and the results will certainly yield great insights.
(8) Common errors in scanf()
Similar to printf(), scanf() is formatted input; the first parameter of the function is similar to the first parameter of printf(), used to provide the format of the input string, and the subsequent parameters are used to specify where to store the formatted input data.
During the use of this function, there will be many common errors.
The first point, like printf(), is that the type indicated by the preceding % must match the variable type for storing data behind it; otherwise, a similar data type interpretation error will occur.
The second error is the same as printf(); if the quotes encompass the entire string, the compiler will not report an error, but the runtime result will be incorrect. To explain this, it is also necessary to understand the stack and the way the scanf() function is called; if interested, you can search for it yourself.
The third typical error is forgetting to write the address-of operator &; for example:
This situation will also not cause a compilation error, but running the program will often directly pop up an error. To understand this, it is necessary to know the meaning of &.
When we define a variable with int fahr;, we allocate a block of storage space for this variable in memory. scanf() indicates that it will input an integer from the keyboard and place it in the location of fahr, so scanf() needs us to tell it the address of the fahr variable. The address-of operator & is used to get the address of fahr. The & operator yields an address, which is essentially an integer, while fahr itself represents the content of the fahr variable, whose value is also an integer; therefore, the compiler does not report an error. However, if we do not use &, the input data will be placed in a location we do not expect, and many times, this location is a place where writing is not allowed, thus triggering the operating system’s protection mechanism and terminating the program’s execution.
The fourth typical error is writing ordinary characters in the first string of scanf() but not writing them during input.
For example, in the code above, if the input is “80,100”, it can be correctly input, but if the input is “80 100”, the result will not be as expected.
The reason is that the first string of scanf() specifies the format of the input, and if there is a comma in it, we must input it exactly as it is.
Conversely, errors can also occur. If the program is scanf(“%d%d”, &lower, &upper); but the input has an extra comma, becoming “80, 100”, the input data will also be incorrect.
Therefore, when using scanf(), the input data must strictly match the formatted input string.
This is a common logical error in programming, and it is very subtle.
For example, in the code below, if the input is 8 3, what will the output be?
If you think the output is 8/3=2, then you are wrong.
Because in b=0, the = is not a relational operator but an assignment operator, meaning that after executing if(b=0), 0 is assigned to b, and the value of this assignment expression b=0 is the value of b, which is 0. Therefore, this condition does not hold, and the print statement in else will be executed.
The problem is, why does it not output 8/3=2?
This is because at this point, b has already become 0, and a/b executes 8/0, which obviously will cause an error.
The solution is simple; just change b=0 to b==0.
(10) Errors related to for statements
The first error is adding a semicolon directly after for.
For example, running the following program will yield the result 1+2+…+100=101.
Why is that? It is because a semicolon after for indicates the end of a statement. As mentioned before, the semicolon signifies the end of a statement. Therefore, sum=sum+i is executed after the for statement ends.
The above statement’s i++ will execute 100 times, but the loop body is empty, meaning it only updates the loop variable without doing any actual work. When it ends, the value of the loop variable i will be 101, hence the output will be 101.
Another typical error is using a semicolon instead of a comma in the parentheses of the for statement, similar to this:
Fortunately, the compiler will report an error, as shown below:
Therefore, for for statements, the two semicolons inside the parentheses cannot be omitted, and the semicolon at the end of the parentheses should be used cautiously (unless you intend to leave the loop body empty).
For the three statements in the parentheses of the for statement, each can be left empty, but the semicolon cannot be omitted. For example, in the following, initializing i=1 can be done outside the loop, and updating the loop variable i=i+1 can also be done inside the loop.
(11) Using uninitialized variables
If local variables are not initialized, some compilers will default to initializing them to 0, while others will not, resulting in the variable storing a random value, so caution is needed. Unfortunately, we often do not notice this, leading to results that may not match our expectations.
For example, if we run the following code, the output will likely be 0 instead of the expected 10! = 362880. The reason lies in the fact that we did not initialize product; if the compiler initializes product to 0, the result will remain 0.
Correcting this is simple; initialize product to 1 before use.
In the above program, if we calculate 30! instead of 10!, what will happen?
Running the program will show negative results, which is obviously incorrect.
The reason is that product is defined as an int type, and this data type is 32 bits, with a range of -231 to 231-1, which clearly exceeds the range for 30!.
This problem can only be partially solved; for example, changing product to double type and running this program yields the following result. Of course, the double type also has a range, and results exceeding its range will also cause errors.

When compiling and running programs using an integrated development environment, there is often an error message encountered, as shown below:
This prompt tells us that it cannot open the output file of a certain .exe. This error occurs because the previous execution of this executable program has not yet finished, and the window has not closed. Therefore, this .exe file is already open, and now when trying to compile the source program again, it attempts to write to this .exe file, leading to a Permission denied error. This is because the operating system does not allow writing to an executable program that is currently open.
Simply close the previous unfinished running window and compile this program again to avoid errors.
(1) Computers are very dumb; they have no elasticity and will strictly follow the specifications.
(2) To be able to coexist harmoniously with computers and master them, we must adapt to the temperament and logic of computers rather than expecting them to cater to us.
(3) To maximize programming ability, after each error occurs, do not rush to modify the code; instead, first understand why the computer gives this erroneous result.
(4) There must be a reason for the computer’s behavior.
(5) A bit of English understanding is essential.
If you have read this far, please give a thumbs up and keep reading, and share with friends who need it.
Author: Huan Dad, PhD in Computer Science from the Chinese Academy of Sciences, university professor, doctoral supervisor, winner of the first prize in the national mathematics Olympiad for middle and high school, first place in Jiangsu, and full marks in mathematics in college entrance examination. Author of bestselling books “Mathematical Thinking Lessons for Kids” and “Mathematical Problem-Solving Thinking Lessons for Kids”.
After years of refinement and practice, Huan Dad’s thinking class is finally online; scan the code to listen to or purchase. For more course introductions, see “Let’s Talk About How I Teach Math Thinking Classes (with Complete Lecture Video) “

After more than six years of establishment, the public account has organized 50 must-read articles for collection; you are welcome to browse and share! If you don’t want to view the webpage, you can directly scan the code at the end of the article to purchase the book.
Huan Dad’s comprehensive views, concepts, and methods on math learning (collection version)
Parents who want to join the learning group can add Huan Dad’s work WeChat:xuanbazhushou; when adding friends, please note the answers to the following two questions; otherwise, they will not be approved.(Note: Those from Changzhou and Hainan can indicate their region and apply to join the newly opened regional group)
(1) What is the decimal equivalent of binary 1.1?
(2) What grade is your child in this semester? (For kindergarten, please specify large, medium, or small class)