Home  >  Article  >  Backend Development  >  Collection of common mistakes in C language programming

Collection of common mistakes in C language programming

黄舟
黄舟Original
2016-12-16 09:38:321447browse

Common errors in C language (repost)
The biggest features of C language are: powerful functions, easy and flexible use. C-compiled programs are not as strict about syntax checking as other high-level languages, which leaves "room for flexibility" for programmers. However, this flexibility brings a lot of inconvenience to program debugging, especially for beginners learning C language. People often make mistakes that they don't even know where they went wrong. Looking at the erroneous program, I don't know how to correct it. Through studying C, I have accumulated some common mistakes in C programming and wrote them for your reference.
1. When writing identifiers, ignore the difference between uppercase and lowercase letters.
main()
{
int a=5;
PRintf("%d",A);
}
The compiler considers a and A to be two different variable names and displays an error message. C considers uppercase letters and lowercase letters to be two different characters. By convention, symbolic constant names are written in uppercase and variable names are written in lowercase to increase readability.
2. Ignoring the type of the variable and performing an illegal operation.
main()
{
float a,b;
printf("%d",a%b);
}
% is the remainder operation to get the integer remainder of a/b. Integer variables a and b can perform remainder operations, but real variables are not allowed to perform "remainder" operations.
3. Confuse character constants with string constants.
char c;
c="a";
Character constants and string constants are confused here. Character constants are single characters enclosed by a pair of single quotes, and string constants are characters enclosed by a pair of double quotes. sequence. C stipulates that "" is used as the end mark of a string. It is automatically added by the system, so the string "a" actually contains two characters: 'a' and '', and assigning it to a character variable does not work. of.
4. Ignore the difference between "=" and "==".
In many high-level languages, the "=" symbol is used as the relational operator "equals". For example, in a BASIC program you can write
if (a=3) then...
But in C language, "=" is an assignment operator and "==" is a relational operator. For example:
if (a==3) a=b;
The former is to compare whether a is equal to 3, and the latter means that if a and 3 are equal, assign the value b to a. Beginners often make such mistakes due to habits.
5. Forgot to add the semicolon.
The semicolon is an indispensable part of the C statement, and there must be a semicolon at the end of the statement.
a=1
b=2
When compiling, the compiler does not find a semicolon after "a=1", so it treats the next line "b=2" as part of the previous line of statements, which will cause a syntax error. When correcting errors, sometimes if no error is found in the line where the error was pointed out, you need to check whether there is a missing semicolon in the previous line.
{ z=x+y;
t=z/100;
printf("%f",t);
}
For compound statements, the last semicolon in the last statement cannot be ignored (this is different from PASCAL).
6. Add more semicolons.
For a compound statement, such as:
{ z=x+y;
t=z/100;
printf("%f",t);
};
There should be no semicolon after the curly braces of the compound statement , otherwise it will be superfluous.
Another example:
if (a%3==0);
I++;
This is if 3 divides a, then I adds 1. However, since there is an extra semicolon after if (a%3==0), the if statement ends here, and the program will execute the I++ statement. Regardless of whether 3 divides a, I will automatically increase by 1.
Another example:
for (I=0;I<5;I++);

The original intention is to input 5 numbers one after another, and then output it after each number is input. Since an extra semicolon is added after for(), the loop body becomes an empty statement. At this time, only one number can be input and output.
7. Forgot to add the address operator "&" when entering variables.
int a,b;
scanf("%d%d",a,b);
This is illegal. The function of the Scanf function is to store the values ​​​​of a and b according to the addresses of a and b in the memory. "&a" refers to the address of a in memory.
8. The method of inputting data does not meet the requirements. ①scanf("%d%d",&a,&b);
When inputting, commas cannot be used as the separator between two data. For example, the following input is illegal:
3, 4
When inputting data, between the two data Separate them with one or more spaces, or you can use the Enter key or the tab key.
②scanf("%d,%d",&a,&b);
C stipulates: If there are other characters in the "format control" string besides the format description, the same characters as these characters should be entered when entering data character. The following input is legal:
3, 4
It is incorrect to use spaces or other characters instead of commas at this time.
3 4 3:4
Another example:
scanf("a=%d,b=%d",&a,&b);
The input should be in the following form:
a=3,b=4
9. Enter characters The format is inconsistent with the requirements.
When entering characters in the "%c" format, both "space characters" and "escape characters" are entered as valid characters.
scanf("%c%c%c",&c1,&c2,&c3);
If you enter a b c
The character "a" is sent to c1, the character " " is sent to c2, and the character "b" is sent to c3, because %c Only one character is required to be read, and there is no need to use a space as the interval between two characters.
10. The input and output data types are inconsistent with the format specifier used.
For example, a has been defined as an integer type, and b has been defined as a real type
a=3;b=4.5;
printf("%f%d ",a,b);
No error message will be given during compilation, but the running results will not be consistent with the original intention. This kind of error requires special attention.
11. When entering data, try to specify accuracy.
scanf("%7.2f",&a);
This is illegal and the accuracy cannot be specified when entering data.
12. The break statement is missing from the switch statement.
For example: Print out percentage segments based on test score levels.
switch(grade)
{ case 'A':printf("85~100 ");
case 'B':printf("70~84 ");
case 'C':printf("60~69 ") ;
case 'D':printf("<60 ");
default:printf("error ");
Due to the omission of the break statement, the case only serves as a label and not as a judgment. Therefore, when the grade value is A, the printf function executes the second, third, fourth, and fifth printf function statements after executing the first statement. The correct way to write it is to add "break;" after each branch. For example
case 'A':printf("85~100 ");break;
13. Ignore the difference in details between while and do-while statements.
(1)main()
{int a=0,I;

scanf("%d",&I);
while(I<=10)

printf("%d",a);
}
(2)main()
{int a=0,I;
scanf("%d",&I);
do
while(I<=10);
printf("%d",a);
}
You can see that when the value of input I is less than or equal to 10, the two results are the same. When I>10, the results are different. Because the while loop is determined first and then executed, while the do-while loop is executed first and then determined. For numbers greater than 10, the while loop will not execute the loop body once, but the do-while statement will execute the loop body once.
14. Misuse of variables when defining arrays.
int n;
scanf("%d",&n);
int a[n];
The array name enclosed in square brackets is a constant expression, which can include constants and symbolic constants. That is, C does not allow dynamic definition of the size of the array.
15. When defining an array, the defined "number of elements" is mistaken for the maximum subscript value that can be used.
main()
{static int a[10]=;
printf("%d",a[10]);
}
C language stipulates: use a[10] when defining, indicating that the a array has 10 elements . Its subscript value starts from 0, so the array element a[10] does not exist.
16. When initializing the array, static storage is not used.
int a[3]=;
It is wrong to initialize the array like this. C language stipulates that only static storage (static) arrays and external storage (exterm) arrays can be initialized. It should be changed to:
static int a[3]=;
17. The address operator & is added where the address operator & should not be added.
scanf("%s",&str);
The C language compilation system handles array names as follows: the array name represents the starting address of the array, and the input item in the scanf function is the character array name, and there is no need to add the address. symbol&. Should be changed to:
scanf("%s",str);
18. Both formal parameters and local variables in the function are defined.
int max(x,y)
int x,y,z;

Formal parameters should be defined outside the function body, while local variables should be defined inside the function body. It should be changed to:
int max(x,y)
int x,y;
{int z;
z=x>y?x:y;
return(z);
}

The above is the easy way to program in C language The content of the collection of mistakes, for more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn