18 Common Mistakes New C Programmers Make and How to Fix Them

Click the blue text18 Common Mistakes New C Programmers Make and How to Fix ThemFollow us

Due to changes in our public account’s push rules, please click “View” and add “Star” to receive exciting technical shares as soon as possible.

Source from the internet, please delete if infringing

The biggest feature of C language is: powerful functionality, convenient and flexible to use.The C compiler does not check syntax as strictly as other high-level languages, leaving programming personnel with “flexibility,” but this flexibility also brings many inconveniences to debugging programs, especially for beginners in C language, who often make mistakes that they themselves do not know where they went wrong. Looking at a program with errors and not knowing how to fix it, I have accumulated some common mistakes made in C programming through my learning of C, and I write this for all students for reference.1. Ignoring the distinction between uppercase and lowercase letters when writing identifiers.main(){  int a=5;  printf(“%d”,A);}The compiler treats a and A as two different variable names and shows an error message. C considers uppercase and lowercase letters as two different characters. As a habit, symbolic constant names are written in uppercase, while variable names are written in lowercase to increase readability.2. Ignoring the variable type and performing illegal operations.main(){  float a,b;  printf(“%d”,a%b);}% is the modulus operator, obtaining the integer remainder of a/b. Integer variables a and b can perform modulus operations, while floating-point variables are not allowed to perform “modulus” operations.3. Confusing character constants with string constants.char c;c=”a”;Here, character constants and string constants are confused; a character constant is a single character enclosed in a pair of single quotes, while a string constant is a sequence of characters enclosed in a pair of double quotes. C specifies that “\” is used as the string termination mark, which is automatically added by the system, so the string “a” actually contains two characters: ‘a’ and ‘\0’, and assigning it to a character variable is not allowed.4. Ignoring the difference between “=” and “==”.In many high-level languages, the “=” symbol is used as the relational operator “equal to”. For example, in BASIC programs, it can be written asif (a=3) then …But in C language, “=” is the assignment operator, while “==” is the relational operator. For example:if (a==3) a=b;The former is for comparison, checking if a is equal to 3, while the latter indicates that if a is equal to 3, assign the value of b to a. Due to habitual issues, beginners often make such errors.5. Forgetting to add a semicolon.A semicolon is an indispensable part of C statements; there must be a semicolon at the end of a statement.a=1b=2During compilation, if the compiler does not find a semicolon after “a=1”, it treats the next line “b=2” as part of the previous statement, which will lead to a syntax error. When correcting errors, sometimes the line pointed out does not show an error, so it is necessary to check if the previous line is missing a semicolon.{  z=x+y;  t=z/100;  printf(“%f”,t);}  For composite statements, the last semicolon in the last statement cannot be ignored (this is different from PASCAL).6. Adding too many semicolons.  For a composite statement, such as:{  z=x+y;  t=z/100;  printf(“%f”,t);};A semicolon should not be added after the closing brace of a composite statement; otherwise, it will be redundant. For example:if (a%3==0);I++;This means if a is divisible by 3, then I should increment by 1. However, due to the extra semicolon after if (a%3==0), the if statement ends here, and the program will execute the I++ statement regardless of whether a is divisible by 3, causing I to increment automatically. Another example:for (I=0;I<5;I++);{scanf(“%d”,&x);printf(“%d”,x);}This was intended to input 5 numbers in sequence, outputting each number after inputting it. However, due to the extra semicolon after for(), the loop body becomes an empty statement, allowing only one number to be input and output.7. Forgetting to add the address operator “&” when inputting variables.int a,b;scanf(“%d%d”,a,b);This is not valid. The scanf function works by storing the values of a and b in memory addresses. “&a” refers to the address of a in memory.8. The input data format does not match the requirements.①scanf(“%d%d”,&a,&b);When inputting, commas cannot be used as separators between two data, such as the following input is invalid: 3,4When entering data, a single space or multiple spaces can be used as a separator, and the enter key or tab key can also be used.②scanf(“%d,%d”,&a,&b);C specifies that if there are other characters besides format specifiers in the “format control” string, then the same characters must be input when entering data. The following input is valid: 3,4Using spaces or other characters instead of commas is incorrect. 3 4 3:4Another example: scanf(“a=%d,b=%d”,&a,&b);Input should be in the following form: a=3,b=49. The format of inputting characters does not match the requirements.When using the “%c” format to input characters, both “space characters” and “escape characters” are considered valid characters for input. scanf(“%c%c%c”,&c1,&c2,&c3);For example, if you input a b cCharacter “a” is assigned to c1, space character ” ” is assigned to c2, and character “b” is assigned to c3, because %c only requires reading one character, and there is no need to use spaces as separators between two characters.10. Input and output data types do not match the format specifier used.For example, a is defined as an integer, and b is defined as a floating point a=3;b=4.5; printf(“%f%d\n”,a,b);During compilation, no error message is given, but the runtime result will not match the original intention. This type of error is particularly important to pay attention to.11. Attempting to specify precision when inputting data. scanf(“%7.2f”,&a);This is not valid; precision cannot be specified when inputting data.  12. Missing the break statement in a switch statement.For example: printing the percentage range based on exam grades.switch(grade){  case ‘A’:printf(“85~100\n”);  case ‘B’:printf(“70~84\n”);  case ‘C’:printf(“60~69\n”);  case ‘D’:printf(“<60\n”);  default:printf(“error\n”);} Due to the missing break statement, the case only serves as a label and does not perform a judgment. Therefore, when the grade value is A, the printf function executes the first statement and continues to execute the second, third, fourth, and fifth printf function statements. The correct way should add “break;” after each branch. For example:case ‘A’:printf(“85~100\n”);break;13. Ignoring the subtle differences between while and do-while statements.(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);do{a=a+I;I++;}while(I<=10);printf(“%d”,a);}It can be seen that when the input value of I is less than or equal to 10, both yield the same result. However, when I>10, the results differ. This is because the while loop checks first before executing, while the do-while loop executes first and checks afterward. For values greater than 10, the while loop does not execute the loop body at all, while the do-while statement will execute the loop body once.14. Misusing variables when defining arrays.int n;scanf(“%d”,&n);int a[n];The expression in brackets after the array name must be a constant expression, which can include constants and symbolic constants. C does not allow dynamic definitions of array sizes.15. Confusing the defined “number of elements” with the maximum index value in array definitions.main(){static int a[10]={1,2,3,4,5,6,7,8,9,10};printf(“%d”,a[10]);}C language specifies that defining a[10] means the array a has 10 elements. The index values start from 0, so the array element a[10] does not exist. 17. Adding the address operator & at positions where it should not be added. scanf(“%s”,&str);The C language compiler treats array names as representing the starting address of the array, and the scanf function’s input item is the character array name, so there is no need to add the address operator &. It should be changed to: scanf(“%s”,str);18. Defining both formal parameters and local variables within a function.int max(x,y)int x,y,z;{  z=x>y?x:y;  return(z);}  Formal parameters should be defined outside the function body, while local variables should be defined inside the function body. It should be changed to:int max(x,y)int x,y;{  int z;  z=x>y?x:y;  return(z);}


If you are over 18 years old and find learning 【C language】 too difficult? Want to try other programming languages, then I recommend you learn Python. Currently, a Python zero-based course worth 499 yuan is available for free, limited to 10 spots!



▲ Scan the QR code - Get it for free

Recommended Reading

Master these C language skills to greatly enhance your programming ability

Make C language source code know its own function’s actual address and size

Why C language will not become obsolete?

If only I had known these before learning C language!

Click Read the original text to learn more

Leave a Comment