2051 views|3 replies

119

Posts

0

Resources
The OP
 

Common mistakes made by C language beginners | 22 points of summary [Copy link]

1. Incorrect use of language

When typing code, you often need to convert between Chinese and English, so you often accidentally use some symbols incorrectly, using Chinese characters. For example, the Chinese semicolon ";" occupies two bytes, while the English semicolon ";" only occupies one byte. The compiler can only recognize English characters, so it will report an error. Checking these characters after the error is reported requires the most eyesight and time. Therefore, you need to be careful when typing code.

2. Forgetting to put a semicolon

Semicolon is the mark of statement in C language. In C, there is no statement without semicolon, and no statement can be recognized. If there is no semicolon in the compilation, the compiler will combine b=2 into the previous statement a=1, and then a syntax error will occur.

3. Use more semicolons

In compound statements, you should not add a semicolon after the curly braces. Although it will not have any effect, it is redundant and meaningless.

In addition, if(a == 0) should not be followed by a semicolon. If a semicolon is followed by if(a == 0), the statement will end prematurely. The purpose of using if is to control the statements that follow. After adding a semicolon, the program will always execute i++, regardless of whether a is equal to 0 or not.

There should be no semicolon after for().

The original intention is to input 4 numbers and output each number after inputting them. However, due to the extra semicolon after for(), the loop becomes an empty statement, and only one number can be input and output.

IV . Errors in variable naming

According to the C language, identifiers are composed of letters, numbers and underscores "_", and the first letter must be a letter or an underscore. In the C language, the following three situations are not allowed in variable naming: 1. Starting with a number; 2. Not containing operators; 3. Not having the same name as a system reserved word (i.e. a keyword). Once any of these three situations occurs, the compiler will report an error. As shown in the figure below:

Attached here are the keywords in C language, which are used as special definers and are also called reserved words.

As a beginner of C language, the naming of variables is often simple and monotonous. However, programmers often use English words with specific meanings to name them, and have formed their own naming rules: there are currently four naming rules in the industry; camel case naming, Hungarian naming, Pascal naming and underscore naming. The first three are more popular naming methods. 1. Camel case naming is to distinguish each word (logical breakpoint) with uppercase letters. 2. Hungarian naming is to add a lowercase symbol as a prefix to the variable name to identify the scope, type, etc. of the variable. 3. Pascal naming. Similar to camel case naming, except that the first letter at the beginning is replaced with an uppercase letter. 4. Underline naming is to separate each word (logical breakpoint) with an underscore.

5. Ignore the difference between uppercase and lowercase letters

In this case, the compiler will consider a and A to be two different variables, and report an error. Therefore, when defining and outputting variables, you must pay attention to the consistency of uppercase and lowercase letters, because C considers uppercase and lowercase letters to be different characters.

6. Incorrect use of data types

For example, we want to output a = 3.1415. If the wrong data type is used, we will not get the desired result. If we use an integer type, only 3 will be output. We must use floating point types such as float, double, and long double.

7. Errors in calculation results when using “/” and “%”

When the "/" operation is performed, if both numbers involved in the operation are integers, the result is an integer with decimals removed, for example;

Although the floating point type is defined, the result is 1.0, not the desired 1.5. To get a result of 1.5, we must change 3/2 to 3.0/2, or 3/2.0.

If one of the divisor and dividend is negative, the result depends on the specific implementation. For example, "-9/7" is calculated as -2 in some systems and -1 in others. This is caused by different rounding of the decimal part. "%" requires that all operands involved in the operation are integers, and the sign of the calculation result is the same as the sign of the operand on the left side of "%". For example, the result of "-9/4" is -1. If it is not an integer, the compiler will report an error.

That is to say, integer variables a and b can be used for remainder operations, while real variables are not allowed to perform "remainder" operations.

8. Representation errors of character constants and string constants

Character variables are defined with the type symbol char, and character constants are single characters enclosed in a pair of single quotes; string constants are character sequences enclosed in double quotes, and in C, "/" is usually used as the end mark. For example, the string "a" below actually contains two characters 'a' and '', so it is not possible to assign them to a variable.

9. Confusion between “=” and “==”

In C language, "=" is the assignment operator, while "==" is the relational operator. Different names mean different functions and priorities. The relational operator has a higher priority than the assignment operator.

The statement in the if() brackets is the content for judgment, judging whether a is equal to 4, so the symbol inside should use the relational operator "==" instead of the assignment operator "=". The correct expression is as follows:

10. Forgetting to add the address operator "&" in the brackets of scanf( )

This is illegal. The scanf function is a format input function that reads input information from the standard input device (keyboard). That is, it stores the values of a and b according to the addresses of a and b in memory. "&a" refers to the address of a in memory.

Search the official account of C Language Chinese Community, reply “resources” in the background, and get 200G programming materials for free.

11. Not paying attention to separators

The scanf() function generally uses the space key, Tab key, or Enter key as a delimiter. When there are non-format characters in the character pass in the format, they must also be entered when entering. For example:

You should enter:

12. Unfamiliarity with the end situation when entering data

In C language, when inputting data, the data is considered to end when encountering the following three situations: 1. Encountering the space key, Tab key, or Enter key; 2. When there is a specified data length, the system automatically truncates it according to the length; 3. Encountering illegal input.

13. The format of input characters is inconsistent with the requirements

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

The character "a" is assigned to c1, the character " " is assigned to c2, and the character "b" is given to c3. Because %c only requires one character to be read in, there is no need to use a space as the separator between two characters.

40G Baidu Cloud Disk C language, C++, Linux from entry, advanced to mastery
covers all knowledge points, as well as video tutorials, C language project source code
https://sourl.cn/KxhkMP

14. Specify precision when entering data

An attempt was made to specify precision when entering data, which is illegal in C.

15. Missing the break statement when using the switch statement

For example: Print student scores

If the break statement is omitted , case only acts as a label, not a judgment. That is, if A is input, it will directly execute to the last printf statement, and will not terminate after the first printf statement. Therefore, break must be added after each printf statement. For example:

16. Partial error of array

1. The number of elements after the array should be consistent with the declaration. If it is inconsistent with the declaration, the number after the declaration is defaulted to 0.

2. The number of elements must be declared.

17. Ignoring the differences between while and do-while statements

As shown in the figure, the results of the two statements are different when the input i is greater than 10. This is because the while loop is first judged and then executed, while the do-while loop is executed first and then judged. For numbers greater than 10, the compiler does not loop the loop body in the while statement, but the compiler executes the loop body once in the do-while statement.

18. Misusing variables when defining arrays

The constant expression enclosed in square brackets after the array can contain constants and symbolic constants. That is, C does not allow the size of the array to be defined dynamically.

19. Added the address operator "&"

In C language, when the input to the scanf function is a character array name, there is no need to add the address symbol &. It should be changed to:

20. Define both formal parameters and local variables in the function

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

21. When defining an array, the "number of elements" defined is mistakenly considered to be the maximum subscript value that can be used

C language stipulates: a[10] is used when defining, indicating that the array has 10 elements. Its subscript value starts from 0, so the array element a[10] does not exist.

22. Definition of two-dimensional array

1. In C language, square brackets cannot be used once to represent the elements of a two-dimensional array. The following are illegal examples:

2. In addition, since the system does not check whether the subscript of the two-dimensional array element is out of bounds, the programmer needs to pay attention to limiting the subscript so that it does not cross the bounds. For example:

丨The article is organized to spread relevant technologies, the copyright belongs to the original author丨

丨If there is any infringement, please contact us to delete丨

This post is from MCU

Latest reply

This problem will occur at the beginning, but now the compiler is very powerful and basically will report an error. In addition, write more code and develop a habit, and you will make fewer mistakes.   Details Published on 2021-8-21 21:52
Personal signature

嵌入式、汇编语言等免费视频<

 

7422

Posts

2

Resources
2
 

These are basically old questions, and some tutorials don't involve such details. They are well organized!

This post is from MCU
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 

104

Posts

0

Resources
3
 
There are quite a lot of summaries, which can be used as a reference for programming.
This post is from MCU
 
 
 

1412

Posts

3

Resources
4
 

This problem will occur at the beginning, but now the compiler is very powerful and basically will report an error. In addition, write more code and develop a habit, and you will make fewer mistakes.

This post is from MCU
 
Personal signature

没有什么不可以,我就是我,不一样的烟火! 

 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list