17 Common Mistakes New C Programmers Make

The C compiler is not as strict with syntax checks as other high-level languages, which gives programming experts some “flexibility”. However, this flexibility brings many inconveniences to debugging, especially for those who are just starting to learn C. They often make errors that they themselves cannot identify.

1. Ignoring Case Sensitivity When Writing Identifiers.

17 Common Mistakes New C Programmers Make

The compiler treats ‘a’ and ‘A’ as two different variable names, resulting in an error message. C considers uppercase and lowercase letters as different characters. Conventionally, symbolic constant names are written in uppercase, while variable names are written in lowercase for better readability.

2. Ignoring Variable Types and Performing Invalid Operations.

17 Common Mistakes New C Programmers Make

% is the modulus operator, yielding the integer remainder of a/b. Integer variables a and b can perform modulus operations, while floating-point variables cannot.

3. Confusing Character Constants with String Constants.

Here, character constants are confused with string constants. A character constant is a single character enclosed in single quotes, while a string constant is a sequence of characters enclosed in double quotes. C specifies that a string ends with “”, which is automatically added by the system, so the string “a” actually contains two characters: ‘a’ and ‘”‘, and assigning it to a character variable is not allowed.

17 Common Mistakes New C Programmers Make

4. Ignoring the Difference Between “=” and “==”.

In many high-level languages, the “=” symbol is used as a relational operator for “equals”. For example, in BASIC, you can write:

But in C, “=” is the assignment operator, while “==” is the relational operator. For example:

The former compares whether a equals 3, while the latter means if a equals 3, assign the value of b to a. Due to habitual issues, beginners often make such mistakes.

5. Forgetting to Add a Semicolon.

The semicolon is an essential part of C statements, and there must be a semicolon at the end of the statement.

During compilation, if the compiler does not find a semicolon after “a=1”, it considers the next line “b=2” as part of the previous statement, leading to a syntax error. When correcting errors, sometimes the error is not found in the indicated line, so it is necessary to check if the semicolon is missing in the previous line.

17 Common Mistakes New C Programmers Make

For compound statements, the last statement’s final semicolon cannot be omitted (unlike PASCAL).

6. Adding Extra Semicolons.

For a compound statement, such as:

17 Common Mistakes New C Programmers Make

The intention is to input 5 numbers sequentially, outputting each number after it is entered. However, by adding an extra semicolon after for, the loop body becomes an empty statement, allowing only one number to be entered and output.

7. Forgetting to Add the Address Operator “&” When Inputting Variables.

This is illegal. The scanf function’s purpose is to store the values of a and b at their memory addresses. “&a” refers to the address of a in memory.

8. Input Data Format Does Not Match Requirements.

① scanf(“%d%d”,&a,&b);

When inputting, commas cannot be used as separators between two data, such as the following input is illegal:

3,4

When inputting data, there should be one or more spaces between two data, or the enter key or tab key can be used.

② scanf(“%d,%d”,&a,&b);

C specifies that if there are other characters in the “format control” string besides the format description, the same characters must be entered when inputting data. The following input is legal:

3,4

In this case, using spaces or other characters instead of a comma is incorrect.

3 4 3:4

For example:

scanf(“a=%d,b=%d”,&a,&b);

The input should be in the following format:

a=3,b=4

9. The format of input characters does not match the requirements.

When using the “%c” format to input characters, both “space characters” and “escape characters” are considered valid characters.

For example, inputting a b c will assign character ‘a’ to c1, space ‘ ‘ to c2, and character ‘b’ to c3, because %c only requires reading one character, and no spaces are needed as separators for two characters.

17 Common Mistakes New C Programmers Make

10. Input and Output Data Types Do Not Match Format Specifiers.

For example, if a is defined as an integer and b as a floating-point number,

the compiler does not give an error message during compilation, but the runtime result will not match the original intention. This type of error needs special attention.

11. Attempting to Specify Precision When Inputting Data.

This is illegal; precision cannot be specified when inputting data.

12. Missing Break Statement in Switch Statement.

For example: printing the percentage score range based on exam grades.

Due to the missing break statement, the case serves only as a label and does not perform a judgment function. Therefore, when the grade value is A, the printf function will execute the next four printf function statements after executing the first one. The correct approach is to add “break;” after each branch. For example:

17 Common Mistakes New C Programmers Make

13. Ignoring the Differences in Detail Between While and Do-While Statements.

(1)

17 Common Mistakes New C Programmers Make

(2)17 Common Mistakes New C Programmers Make

It can be seen that when the value of I is less than or equal to 10, both yield the same result. However, when I > 10, the results differ. This is because the while loop checks the condition before executing, while the do-while loop executes first and then checks. For numbers greater than 10, the while loop does not execute the loop body at all, while the do-while statement executes the loop body once.

14. Misusing Variables When Defining Arrays.

17 Common Mistakes New C Programmers Make

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

15. Misunderstanding the Defined “Number of Elements” as the Maximum Subscript Value.

17 Common Mistakes New C Programmers Make

C language specifies that when defining with a[10], it means the array a has 10 elements. The subscript value starts from 0, so the array element a[10] does not exist.

17 Common Mistakes New C Programmers Make

16. Adding Address Operator & Where It Should Not Be Added.

Source: C Language Plus

Leave a Comment