C programming language often make eighteen mistakes summary

  • 2020-04-02 01:12:12
  • OfStack

Looking at the wrong procedures, I do not know how to change, I through the learning of C, accumulated some C programming mistakes often made, for your reference.

1. Ignore the difference between upper and lower case letters when writing identifiers.


main()
{
  int a=5;
  printf("%d",A);
}

The compiler treats a and a as 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 capitalized and variable names are lowercase for increased readability.

2. The type of variable is ignored and illegal operation is carried out.


main()
{
  float a,b;
  printf("%d",a%b);
}

% is the remainder, the remainder of a over b. Integer variables a and b can be used for remainder, while real variables are not allowed to be used for remainder.

Confuse character constants with string constants.

Char c;
C = "a";

Here you confuse a character constant, which is a single character enclosed in a pair of single quotes, with a string constant, which is a sequence of characters enclosed in a pair of double quotes. C specifies that the string ends with "\", which is automatically added by the system, so the string "a" actually contains two characters: 'a' and' \0', and it is not possible to assign it to a character variable.

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

In many high-level languages, the = symbol is used as the relational operator equals. Such as can be written in BASIC programs
If (a = 3) then...
But in C, "=" 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 is to assign b to a if a is equal to 3. Beginners often make this mistake because of habit.

Forget the semicolon.

A semicolon is an integral part of a C statement and must be at the end of the statement.
A = 1
B = 2
At compile time, the compiler does not find a semicolon after "a=1" and then includes the next line "b=2" as part of the previous line. This can lead to syntax errors. When correcting an error, it is sometimes necessary to see if a semicolon has been left out on the previous line when the error is not found on the line indicated.


{
  z=x+y;
  t=z/100;
  printf("%f",t);
}

For compound statements, the last semicolon in the last statement cannot be ignored without writing (unlike PASCAL).

Add a semicolon.

For a compound statement, such as:


{
  z=x+y;
  t=z/100;
  printf("%f",t);
};

The compound statement should not be followed by a semicolon, otherwise it will gild the lily. Such as:
If (a % 3 = = 0);
I++;

So if 3 goes into a, then I plus 1. But since if (a%3==0) is followed by a semicolon, the if statement ends here and the program executes I++, adding 1 automatically to I regardless of whether 3 is divisible by a. Such as:
For (I = 0; i. < 5; I++);
{the scanf (" % d ", & x);
Printf (" % d ", x); }

The original meaning is to input 5 Numbers successively, each input a number and then output it. Because for() is followed by a semicolon, the body of the loop becomes empty, and only one number can be entered and output.

7. Forgot to add the address operator "&" when entering a variable.

Int a, b;
The scanf (" % d % d ", a, b);
It's not legal. The Scanf function stores the values of a and b as they are located in memory. "&a" refers to the address of a in memory.

8. The way of data input is inconsistent with the requirements.

(1) the scanf (" % d % d ", & a, & b);
When typing, do not use a comma as the separator between two data, such as the following illegal input:
      3, 4
Input data, in the two data with one or more Spaces between the space, also can use the enter key, TAB TAB TAB.

(2) the scanf (" % d, % d ", & a & b);
C states that if there are other characters in the format control string besides the format specification, the same characters should be entered when entering data. The following input is legal:
      3, 4
It is not correct to use Spaces or other characters instead of commas.
      3, 4, 3, 4
Such as:
      The scanf (" a = % d, b = % d ", & a & b);
The input should be in the following form:
      A = 3, b = 4

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

When entering characters in the '%c' format, both the space character and the escape character are entered as valid characters.
      The scanf (" % % % c c c ", & c1, and c2, & c3);
For example, input a, b and c

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

10. The input and output data types are inconsistent with the format specifiers used.

For example, a has been defined as an integer and b as a real
      A = 3; B = 4.5;
      Printf (" % f, % d \ n ", a, b);
No error message is given at compile time, but the result will not be as intended. This error is especially noticeable.

11. When entering data, attempt to specify the accuracy.

      The scanf (" % 7.2 f ", & a);
This is illegal and cannot be specified when entering data.

12. The break statement was omitted from the switch statement.

For example: to print out a percentage paragraph according to the grade of the examination result.
The switch (grade)
{
Case 'A' : printf (" \ n "85 ~ 100);
Case 'B' : printf (" \ n "70 ~ 84);
Case 'C' : printf (" 60 ~ 69 \ n ");
Case 'D' : printf (" < 60 \ n ");
Default: printf (" error "\ n");
}
Because the break statement was omitted, case only serves as a label, not a judgment. Therefore, when grade is A, the printf function executes the second, third, fourth, and fifth printf function statements after the first statement. The correct way to write this is to add "break;" to each branch. . For example,
Case 'A' : printf (" \ n "85 ~ 100); Break;

13. Ignoring the difference in detail between while and do-while statements.

(1) the main ()
{int a = 0, I;
The scanf (" % d ", & I);
While (I < = 10)
{a = a + I;
I++;
}
Printf (" % d ", a);
}

(2)
The main ()
{int a = 0, I;
The scanf (" % d ", & I);
The do
{a = a + I;
I++;
} while (I < = 10);
Printf (" % d ", a);
}
As you can see, when the value of input I is less than or equal to 10, they both get the same result. And when I >At 10, it's going to be different. Because while loops are judged and executed, and do-while loops are executed and judged. The while loop never executes the body of the loop once for Numbers greater than 10, whereas the do-while statement executes the body of the loop once.

Misuse of variables when defining an array.

Int n;
The scanf (" % d ", & n);
Int a [n].
The array name is enclosed in square brackets by a constant expression, which can include both constants and symbolic constants. That is, C does not allow dynamically defining the size of an array.

15. When defining an array, mistake the "number of elements" defined for the maximum possible value.

The main ()
{the static int a [10] =,2,3,4,5,6,7,8,9,10 {1};
Printf (" % d ", a [10]);
}
C language provisions: when defined with a[10], means that an array has 10 elements. The value below starts at 0, so the array element a[10] does not exist.

17. Add the address operator where the address operator & should not be added.
      The scanf (" % s ", & STR);
C compiler system to the array name processing is: the array name represents the array starting address, and the scanf function input item is the character array name, do not need to add the address &. Should be changed to:
      The scanf (" % s ", STR);

18. Meanwhile, the parameters and local variables in the function are defined.

Int Max (x, y)
Int x, y, z;
{
Z = x > Y? X: y;
Return (z);
}
Formal parameters should be defined outside the function, while local variables should be defined inside the function. Should be changed to:
Int Max (x, y)
Int x, y;
{
Int z;
Z = x > Y? X: y;
Return (z);
}


Related articles: