Common Mistakes for C Language Beginners: A Summary of 22 Points

Click the blue text
Follow us

Content

1. Language Usage Errors

When coding, it is often necessary to switch between Chinese and English, which can lead to mistakenly using Chinese symbols. For example, the Chinese semicolon “;” occupies two bytes, while the English semicolon “;” occupies only one byte. The compiler can only recognize English characters, which can lead to errors. Checking these characters after an error occurs requires keen eyesight and time. Therefore, attention is needed when coding.

2. Forgetting to Add a Semicolon

The semicolon is a statement terminator in C. Without a semicolon, there is no statement, and the compiler cannot recognize it. If a semicolon is missing during compilation, the compiler will combine a=1 with b=2, resulting in a syntax error.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

3. Adding Extra Semicolons

In compound statements, a semicolon should not be added after the closing brace. While it may not have any impact, it is unnecessary and meaningless.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

Additionally, a semicolon should not be added after if(a == 0). Adding a semicolon after if(a == 0) prematurely ends the statement, while the purpose of using if is to control the subsequent statements. After adding the semicolon, the program will always execute i++, regardless of whether a is equal to 0.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

A semicolon should not be added after for().

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

The intention is to input four numbers, outputting each number after input. However, adding an extra semicolon after for() results in an empty statement, allowing only one number to be input and output.

4. Variable Naming Errors

C language specifies that identifiers consist of letters, numbers, and underscores “_”, and the first character must be a letter or underscore. The following three situations are not allowed for variable naming in C: 1. Starting with a number; 2. Including operators; 3. Having the same name as a reserved word (i.e., keyword). If any of these situations occur, the compiler will report an error. As shown in the figure below:

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

Here is a list of keywords in C language, which are used as special definitions, also known as reserved words.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

As a beginner in C language, variable naming is often simple and monotonous. However, experienced programmers often use words with specific meanings for naming and have developed their own naming conventions. Currently, there are four naming conventions in the industry: Camel Case, Hungarian Notation, Pascal Case, and Underscore Naming. The first three are more popular naming conventions. 1. Camel Case separates each word (logical breakpoint) with uppercase letters. 2. Hungarian Notation adds a lowercase letter prefix before the variable name to indicate the variable’s scope, type, etc. 3. Pascal Case is similar to Camel Case, except that the first letter is capitalized. 4. Underscore Naming separates each word (logical breakpoint) with underscores.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

5. Ignoring Case Sensitivity

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

In this case, the compiler will treat a and A as two different variables, resulting in an error. Therefore, when defining and outputting variables, it is important to maintain consistency in case sensitivity, as C treats uppercase and lowercase letters as different characters.

6. Incorrect Use of Data Types

For example, when we want to output a = 3.1415, if the data type is used incorrectly, we will not get the desired result. Using an integer type will only output 3. We must use float, double, or long double.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img
Common Mistakes for C Language Beginners: A Summary of 22 Points
img

7. Errors in Division and Modulus Operations

When using the “/” operator, if both operands are integers, the result will be an integer that truncates the decimal part. For example:

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

Even if a floating-point type is defined, the result will still be 1.0 instead of the desired 1.5. To obtain the result of 1.5, we must change 3/2 to 3.0/2 or 3/2.0.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

If either the dividend or divisor is negative, the result depends on the specific implementation. For example, -9/7 may yield -2 in some systems and -1 in others, due to different handling of the decimal part. The “%” operator requires both operands to be integers, and the result’s sign is the same as that of the left operand, such as -9/4 yielding -1. If a non-integer type is used, the compiler will report an error.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

This means that integer variables a and b can be used for the modulus operation, while floating-point variables cannot.

8. Errors in Representing Character Constants and String Constants

Character variables are defined using the char type, and character constants are single characters enclosed in single quotes; string constants are sequences of characters enclosed in double quotes, typically ending with a null character in C. For example, the string "a" actually contains two characters ‘a’ and ‘\0’, which cannot be assigned to a variable.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

9. Confusion Between “=” and “==”

In C language, “=” is the assignment operator, while “==” is the equality operator. The difference in naming implies different functions and priorities. The equality operator has a higher priority than the assignment operator.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

The statement inside the parentheses of if() is the content being evaluated, checking whether a equals 4, so the symbol inside should use the equality operator “==” instead of the assignment operator “=”. The correct representation is as follows:

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

10. Forgetting to Add the Address Operator “&” in scanf()

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

This type of writing is illegal. The scanf function is a formatted input function that reads input from the standard input device (keyboard). It stores the values of a and b in memory addresses, where “&a” refers to the address of a in memory.

11. Ignoring Delimiters

The scanf() function generally uses the space key, Tab key, or Enter key as delimiters. When there are non-format characters in the format string, they must also be included in the input. For example:

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

Input should be:

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

12. Unfamiliarity with End Conditions When Inputting Data

In C language, when inputting data, the following three conditions indicate that the data input is complete: 1. Encountering a space, Tab, or Enter key; 2. When there is a specified data length, the system automatically truncates according to that length; 3. Encountering invalid input.

13. Input Format and Requirements Not Matching

In C, when using %c for input, spaces and escape characters are considered valid characters.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

The character “a” is assigned to c1, the character “ ” is assigned to c2, and the character “b” is assigned to c3, because %c only requires reading one character, and there is no need for a space as a separator for two characters.

14. Specifying Precision When Inputting Data

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

Attempting to specify precision when inputting data is illegal in C.

15. Forgetting to Write the Break Statement in a Switch Statement

For example, printing student grades:

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

If the break statement is omitted, the case only serves as a label and does not function as a judgment. This means that if A is input, it will directly execute the last printf statement without stopping after executing the first printf statement. Therefore, a break must be added after each printf statement. For example:

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

16. Errors in Arrays

1. The number of elements after the array must match the declared number. If it does not match, the numbers exceeding the declaration will default to 0.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

2. The number of elements must be declared.

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

17. Ignoring the Differences Between While and Do-While Statements

Common Mistakes for C Language Beginners: A Summary of 22 Points
img
Common Mistakes for C Language Beginners: A Summary of 22 Points
img

As shown in the figure, when the input i is greater than 10, the results differ between the two. This is because the while loop checks the condition before executing, while the do-while loop executes once before checking. For numbers greater than 10, the compiler will not loop the body in the while statement, whereas the compiler will execute the body in the do-while statement once.

18. Misusing Variables When Defining Arrays

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

The expression in square brackets after the array must be a constant expression, which can include constants and symbolic constants. C does not allow dynamic definition of the size of an array.

19. Adding an Address Operator “&” Incorrectly

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

In C language, when the input is a character array name in the scanf function, it is not necessary to add the address operator “&”. It should be changed to:

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

20. Simultaneously Defining Parameters and Local Variables in Functions

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

Parameters should be defined outside the function body, while local variables should be defined inside the function body. It should be changed to:

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

21. Mistaking the Defined “Number of Elements” for the Maximum Index Value When Defining Arrays

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

C language specifies that when defining with a[10], it indicates that the array has 10 elements. The index values start from 0, so the array element a[10] does not exist.

22. Defining Two-Dimensional Arrays

1. In C language, a single pair of square brackets cannot be used to represent the elements of a two-dimensional array; the following is an illegal example:

Common Mistakes for C Language Beginners: A Summary of 22 Points
img

2. Additionally, since the system does not check whether the index of a two-dimensional array exceeds its bounds, programmers need to be careful to restrict the index from going out of bounds. For example:

Common Mistakes for C Language Beginners: A Summary of 22 Points

*Note: This article is compiled from the internet, and the copyright belongs to the original author. If there is any incorrect source information or infringement of rights, please contact us for deletion or authorization matters.

Common Mistakes for C Language Beginners: A Summary of 22 Points

Common Mistakes for C Language Beginners: A Summary of 22 Points
Click to read the original text for more information

Leave a Comment