Common error collection in C language

Source: Internet
Author: User
Tags constant lowercase printf
The main features of C language are: Strong function, easy to use and flexible. C compiled programs are not as strict as other high-level languages. This gives programmers the "flexibility", but still because of this flexibility to debug the program has brought a lot of inconvenience, especially for beginners of the C language, often a few even they do not know where the mistake. Look at the wrong procedure, I do not know how to change, I learned through C, accumulated some C programming often make mistakes, write to you for reference.
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 (this is different from Pascal).
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

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.