C language programming error award, dedicated to children's shoes who are beginners to C language programming~

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 letters 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. Ignore the type of the variable and perform 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 quotation marks, and a string constant is a sequence of characters enclosed in a pair of double quotation marks.

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". For example, if (a=3) then… can be written in the BASIC program

But in C language, "=" is an assignment operator, and "==" is a 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. Forgot to add a 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); }; No semicolon should be added after the curly braces of the 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 the other, 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 way 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. 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 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 be as follows: a=3,b=4

9. The format of the 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); such as input 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 will be given when compiling, but the running result will be Inconsistent with the original intention. This kind of error requires special attention.

 


In addition, if you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster! I may be able to help you here~

UP has uploaded some video tutorials on learning C/C++ programming on the homepage. Those who are interested or are learning must go and take a look! It will be helpful to you~

Sharing (source code, actual project video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning:

Programming learning:

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/114745258
Recommended