"Experience" C language programming most common 15 errors "to the careless classmate"

Source: Internet
Author: User
Tags lowercase
1. The distinction between uppercase and lowercase letters is ignored when writing identifiers.
Main ()
{int a=5;
printf ("%d", A); }
The compiler says that a and a are two different variable names, and the error message is displayed. C considers uppercase and lowercase letters to be two different characters. Traditionally, the symbol constant name is capitalized and the variable name is in lowercase to increase readability.


2. The type of the variable is ignored and an illegal operation is performed.


Main ()
{float a,b;
printf ("%d", a%b); }
% is the remainder operation, and A/b is the whole residue. Integer variables A and B can be evaluated, while real variables do not allow for "redundancy" operations.


3. Confuse the amount of character constants with string constants.


char c;
C= "a";
Here it confuses the character constants with the string constant, which is a single character enclosed by a pair of single quotes, and a string constant is a sequence of characters enclosed by a pair of double quotes. C stipulates "\" As the end of the string, which is automatically added by the system, so the string "a" actually contains two characters: ' A ' and ' \ ', and assigning it to a character variable is not possible.


4. Ignores the difference between "=" and "= =".
In many high-level languages, the "=" symbol is used as the relational operator "equals". As in basic programs, you can write
if (a=3) then ...
But in the C language, "=" is the assignment operator, and "= =" is the relational operator. Such as:
if (a==3) a=b;
The former is compared, and a is equal to 3, which means that if a and 3 are equal, the B value is assigned to a. Because of habit problems, beginners often make such mistakes.

5. Forget to add a semicolon.

Semicolons are an integral part of the C statement and must have a semicolon at the end of the statement.
A=1
b=2
At compile time, the compiler does not find a semicolon after "a=1", and the next line, "b=2", is also part of the previous line of statements, which can cause syntax errors. When you make a mistake, sometimes you don't find an error in a row that is pointed out to be wrong, you need to see if the previous line misses a semicolon.
{z=x+y;
t=z/100;
printf ("%f", t);}
For a compound statement, the last semicolon in the last statement cannot be ignored or not written.

6. Add a semicolon.

For a compound statement, such as:
{z=x+y;
t=z/100;
printf ("%f", t);
};
The curly braces of a compound statement should not be followed by a semicolon, otherwise it would be superfluous.
Another example:
if (a%3==0);
i++;
This is if 3 is divisible by a, then I plus 1. However, since if (a%3==0) adds a semicolon, the IF statement ends, the program executes the i++ statement, regardless of whether 3 is divisible evenly a,i will automatically add 1.

Another example:
for (i=0;i<5;i++);
{scanf ("%d", &x);
printf ("%d", x);}

The original intention is to enter 5 number, each input a number and then output it. Because a semicolon is added after the for (), the loop body becomes an empty statement, and you can only enter a number and output it.

7. Forget to add the address operator "&" when entering variables.

int a,b;
scanf ("%d%d", a,b);
This is not legal. The role of the scanf function is to store the values of A and B in the address of A and B in memory. "&a" means the address in memory of a.


8. The data was entered in a manner inconsistent with the requirements.


①SCANF ("%d%d", &a,&b);
When you enter, you cannot use commas as delimiters between two data, such as if the following entry is illegal:
3,4
When you enter data, you can use the ENTER key between two data at one or more space intervals, and the Key tab.
②SCANF ("%d,%d", &a,&b);
C stipulates that if there are other characters in the format control string in addition to the format description, you should enter the same characters as the characters when you enter data. The following input is legal:
3,4
It is not correct to use a space or other character without 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. The format of the input character is inconsistent with the requirement.


When you enter a character in the "%c" format, both the space character and the escape character are entered as valid characters.
scanf ("%c%c%c", &AMP;C1,&AMP;C2,&AMP;C3);
If you enter a b C
The character "A" is given to the C1, the character "" is given to the C2, and the character "B" is given to C3, because%c only requires a character to be read, followed by no space as a two-character interval.


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


For example, a is defined as an integral type, and B is defined as a solid type
a=3;b=4.5;
printf ("%f%d\n", a,b);
No error message is given at compile time, but the result will be inconsistent with the original intention. This error is especially noteworthy.


11. When entering data, attempt to specify precision.


scanf ("%7.2f", &a);
It is illegal to do this, and you cannot specify precision when entering data.


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


For example: Print out hundred several paragraphs based on the grade of the test.
Switch (grade)
{case ' A ':p rintf ("85~100\n");
Case ' B ':p rintf ("70~84\n");
Case ' C ':p rintf ("60~69\n");
Case ' D ':p rintf ("<60\n");
default:printf ("error\n");
As the break statement is omitted, the case only acts as a label, but it does not judge the function. Therefore, when the grade value is a, the printf function executes the 第二、三、四、五个 printf function statement after the first statement is executed. The correct wording should be followed by "break;" after each branch.

For example
Case ' A ':p rintf ("85~100\n");
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.