C language programming often make 18 mistakes summary _c language

Source: Internet
Author: User
Tags constant lowercase

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. When writing identifiers, the difference between uppercase and lowercase letters is ignored.

Copy Code code as follows:

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, ignoring the type of the variable, the operation of the illegal.

Copy Code code as follows:

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, the character constants volume and string constants confused.

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, it is automatically added by the system, so the string "a" actually contains two characters: ' A ' and ', ' and it is not possible to assign it to a character variable.

4, ignoring the "=" and "= =" difference.

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.

Copy Code code as follows:

{
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:

Copy Code code as follows:

{
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, input variables forget to add the address operator "&".

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 way to enter data and requirements do not match.

①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 input character format and requirements are inconsistent.

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", &C1,&C2,&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");

13, ignoring the distinction between while and do-while statements in detail.

(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);
Todo
{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 o'clock, the results are the same. And when i>10, the results are different. Because the while loop is first judged and then executed, and the Do-while loop is first executed and then judged. The number of while loops that are greater than 10 do not execute the loop body at once, and 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 array name, enclosed in square brackets, is a constant expression that can include constants and symbolic constants. That is, C does not allow for dynamic definition of the size of the array.

15, in defining the array, the definition of the "number of elements" mistaken for the maximum subscript value can be made.

Main ()
{static int a[10]={1,2,3,4,5,6,7,8,9,10};
printf ("%d", a[10]);
}
The C language stipulates: Defined with A[10], which means that a array has 10 elements. The subscript value starts at 0, so the array element a[10] does not exist.
 
17. Add the address operator at the location where address operators & should not be added.
scanf ("%s", &STR);
C language compilation system array name processing is: The array name represents the starting address of the array, and the scanf function of the entry is a character array name, no need to add the address character &. should read:
scanf ("%s", str);

18, which defines local variables in both formal parameters and functions.

int max (x,y)
int x,y,z;
{
z=x>y?x:y;
return (z); The
}
parameter should be defined outside the function, and local variables should be defined in the function body. should read:
int max (x,y)
int x,y;
{
int z;
z=x>y?x:y;
return (z);
}

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.