A must-read for beginners of C language (Part 1) -- Common PTA errors and solutions

When novices are doing questions on PTA, they often have the following four states after submission:

"The answer is correct" means that your code passed all test points. Let’s expand on the other three errors:

(1) Partial error: It means that your code only passed some test points. Another part of the test points failed. There are usually two reasons for failure: (1) wrong format, (2) wrong answer, as shown in the figure below:

(1) Format error: It means that you did not output according to the format of the output sample given in the question. The most common situation is: where there are spaces in the output sample, there are no spaces in your springf statement, or where there are no spaces in the output sample, you typed extra spaces in the printf statement. Be sure to strictly follow the format of the output sample of the question to write what is inside the double quotes in your printf statement. To output the fixed text in the sample, you just copy it; to output the variables in the sample, you just use the corresponding format conversion specifier placeholder, and then write the corresponding variable in the subsequent parameters. For example:

Output sample: cost = 55.00. This 55.00 is the value of the floating point variable a you calculated, and it is required to retain 2 decimal places.

At this time, your printf statement should be written as printf("cost = %.2f", a). Pay special attention to the fact that there are spaces on both sides of the equal sign in the output sample, and there must be spaces on both sides of the equal sign in your printf statement.

As for how many decimal places are retained in the floating point output, use %.n to control the output. If you want to retain how many decimal places, just change n to a certain number.

(2) Wrong answer: It means that your calculation itself is wrong. At this time, you need to find the logical error in your own code. It is recommended that you find a correct code and compare the following logical differences between other people's code and your code. Of course, this kind of error is difficult to find and requires a little patience.

(2) Segmentation fault: This kind of error is usually related to memory. For example, when outputting, there is no address symbol in front of the variable, such as scanf ("%d", N); and the array is out of bounds, which may also occur. Segmentation fault.

(3) Compilation error: This is also the most common mistake made by novices. In fact, it means that your syntax is not written correctly and the compiler cannot compile it. Let me teach you a way to make this mistake. Look at the picture below:

When a compilation error occurs, there will be a prompt below. As long as you find the error in the error message, ignore the warning. The number in front of the error indicates which line and character of your code the error occurs in. ^~~~~ will directly mark the place where the error starts. Focus on this place. For example, line 6 of the code in the picture is intended to calculate the value of 0.54*N, but the * is omitted. This kind of omission is written this way in mathematics, but not in C language. The compiler doesn't understand it when it's written like this, so it compiles incorrectly.

There are also many compilation errors, such as the semicolon at the end of the statement is not typed, the English symbol is typed into Chinese, the parentheses, the curly braces are typed in another, etc., etc., etc., they can be quickly found through this method.

There will also be an article "A Must-read for C Language Beginners (Part 2)", where we will summarize more common mistakes that newbies can make when writing C language source code.

Guess you like

Origin blog.csdn.net/morn_l/article/details/133870189