Common C language programming mistakes == suitable for programmers? It turned out to be so!

Learning a computer is actually different from being a programmer. Those who learn computers may not always choose to be programmers. Those who are programmers are not necessarily all those who study computers in academic classes. Determining whether you can learn is actually just the first step.

The most important thing is how to learn, how to learn well, and find a job to become a real programmer.

As for how to learn, you can start with C language. C-compiled programs are not as strict in syntax checking as other high-level languages, which leaves "flexibility" for programmers. However, this flexibility brings a lot of inconvenience to program debugging.

Especially for people who are new to the C language, they often make mistakes that they don't even know what's wrong. Looking at the wrong program, I don't know how to change it. Through the study of C, I have accumulated some common mistakes in C programming for reference.

 

1. When writing identifiers, the difference between uppercase and lowercase letters is ignored

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 thinks that uppercase and lowercase letters are two different characters. Traditionally, 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, while real variables are not allowed to perform remainder operations.

3. Confusing character constants with string constants

char c;

c=”a”;

Here is the confusion between character constants and string constants. A character constant is a single character enclosed in a pair of single quotes, and a string constant is a sequence of characters enclosed in a pair of double quotes. C stipulates that "\" is used as the end of the string, which is automatically added by the system, so the string "a" actually contains two characters:'a' and'\0', and it is assigned to one character Variables will not work.

4. Ignore the difference between "=" and "=="

In many high-level languages, the "=" symbol is used as the relational operator "equal to". If you can write in BASIC program

if (a=3) then …

But in C language, "=" is the assignment operator, and "==" is the relational operator. Such as:

if (a==3) a=b;

The former is to compare whether a is equal to 3, and the latter means that if a is equal to 3, assign the value of b to a. Because of habits, beginners often make such mistakes.

5. Forget 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 the next line "b=2" is also used as a part of the previous statement, which will cause a syntax error. When correcting an error, sometimes no error is found in the line where the error is pointed out, you need to check whether the semicolon is missing in the previous line.

{

  z=x+y;

  t=z/100;

  printf(“%f”,t);

}

For compound statements, the final 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);

};

A semicolon should not be added after the curly braces of a compound statement, otherwise it will be superfluous. Another example:

if (a%3==0);

I++;

Originally, if 3 divides a, then I adds 1. However, because an extra semicolon is added after the if (a%3==0), the if statement ends here, and the program will execute the I++ statement. I will automatically add 1 regardless of whether 3 is divisible by a. Another example:

for (I=0;I<5;I++);

{

       scanf(“%d”,&x);

       printf(“%d”,x);

}

The original intention is to input 5 numbers one after another, and then output it after each input. Because 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 inputting variables

int a,b;

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

This is illegal. The function of scanf 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, you cannot use a comma as a separator between two data. For example, the following input is illegal:

3,4

When entering data, use one or more spaces between the two data, and you can also use the Enter key and 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 input when inputting data. The following input is legal:

3,4

At this time, it is wrong to use spaces or other characters instead of commas.

3 4 3:4

Another example:

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

The input should look like the following:

a=3,b=4

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

When inputting characters in "%c" format, both "space character" and "escape character" are input as valid characters.

scanf(“%c%c%c”,&c1,&c2,&c3);

For example, enter abc

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

10. The input and output data type is inconsistent with the format specifier used

For example, a has been defined as an integer type, and b is defined as a real type

a=3;b=4.5;

printf(“%f%d\n”,a,b);

No error message is given when compiling, but the running result will be inconsistent with the original intention. This kind of error requires special attention.

11. Attempt to specify accuracy when inputting data

scanf(“%7.2f”,&a);

It is illegal to do so, and accuracy cannot be specified when entering data.

12. Omission of break statement in switch statement

For example: according to the grade of the test score, print out the hundred-point system.

switch(grade)

{

  case ‘A’:printf(“85~100\n”);

  case ‘B’:printf(“70~84\n”);

  case ‘C’:printf(“60~69\n”);

  case ‘D’:printf(“<60\n”);

  default:printf(“error\n”);

}  

Because of the omission of the break statement, the case only serves as a label, not a judgment. Therefore, when the grade value is A, the printf function executes the second, third, fourth, and fifth printf function statements after the first statement is executed. The correct way to write is to add "break;" after each branch. E.g

case ‘A’:printf(“85~100\n”);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)

{a=a+I;

I++;

}

printf(“%d”,a);

}

(2)

main()

{

int a=0,I;

scanf(“%d”,&I);

do

{a=a+I;

I++;

}while(I<=10);

printf(“%d”,a);

}

It can be seen that when the value of input I is less than or equal to 10, the results obtained by the two are the same. When I>10, the two results are different. Because the while loop is judged first and then executed, while the do-while loop is executed first and then judged. For numbers greater than 10, the while loop does not execute the loop body once, while the do-while statement executes the loop body once.

14. Misuse of variables when defining arrays

int n;

scanf(“%d”,&n);

int a[n];

The square brackets after the array name are constant expressions, 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, mistake the defined "number of elements" as the maximum subscript value that can be made

main()

{

    static int a[10]={1,2,3,4,5,6,7,8,9,10};

    printf(“%d”,a[10]);

}

C language stipulates: Use a[10] when defining, which means that the a array has 10 elements. The subscript value starts from 0, so the array element a[10] does not exist.

If you want to become a programmer, want to quickly master programming, hurry up and pay attention to the editor!

Free study books:

Free learning materials:

16. The address operator is added where the address operator & should not be added

charstr[20];

scanf(“%s”,&str);

The processing of the array name by the C language compilation system is: the array name represents the starting address of the array, and the input item in the scanf function is the character array name, and the address character & is not necessary. Should be changed to:

scanf(“%s”,str);

17. The formal parameters and local variables in the function are defined at the same time

int max(x,y)

int x,y,z;

{

  z=x>y?x:y;

  return(z);

}

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

int max(x,y)

int x,y;

{

  int z;

  z=x>y?x:y;

  return(z);

}

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/113045138