Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the pitfalls of using C language?

Shulou Source: shulou.com Published: 2022-06-02 12:15:57 10月04日 Update

This article mainly explains "what are the pitfalls in the use of C language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn what are the pitfalls in the use of C language.

1. Correct understanding of function declaration

Have you ever seen the code "(* (void (*) ()) 0) ();"? can you know what it means?

Don't worry if you don't understand. Let's analyze it: any C variable declaration consists of two parts: a type and a set of expression-like declarants (declarator). On the surface, the declarant looks like a fish table, so it should only return a result given by the declaration. (void (*) () 0) is the type that converts the constant 0 to a pointer to a function that returns void. And (* (void (*) ()) 0) (); can be seen as a function call, and the semicolon at the end makes the expression a statement. Of course, we can also use the typedef afterlife expression to be clearer:

Typedef void (* funcptr) (); (* (funcptr) 0) ()

two。 Precedence of operators

In C language, there are as many as 15 operator priorities, if we do not know the priority of operators, we are very passive in dealing with problems and analyzing code, then we can simply group operators and understand the relative priorities between groups of operators, so it is relatively easy to remember:! > arithmetic operators > relational operators > & & > | > assignment operators.

3. Semicolon problem of statement Terminator

The C language uses the semicolon ";" as a sign at the end of the statement, so what happens if you write more semicolons after the expression? This semicolon may be seen as an empty statement that will not have any actual effect, or the compiler will generate a warning message for this extra semicolon, which can be removed based on the warning message. But one exception is to write an extra semicolon after the if or while statement. At this time, even if the statement after if or while is a separate statement, the fish condition judgment has nothing to do with it. Take a look at the code:

If (x > y) return; If (x > y); return

These two sentences are completely different.

4. Switch statement problem

Switch in C language needs to add a break; statement after the case branch, otherwise it will be known to execute along the statement and will not achieve the effect we need.

5. Function call

Different from other programming languages, C language requires that when a function is called, the function should also contain a list of parameters, so if f is a function, f (); a function call, and f; is a statement that does nothing.

6. Else suspension problem

We often hear that else should be paired with if, but we often make mistakes when we write code, such as the following code:

The original intention of this code is divided into two cases, x equals 0, not 0, but the meaning of the above code is quite different. That is, if you think that it is not equal to 0, the program does not do any processing, and when x equals 0, you will judge the value of y and make a different treatment.

Thank you for your reading. The above is the content of "what are the pitfalls in the use of C language?" after the study of this article, I believe you have a deeper understanding of the pitfalls in the use of C language. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Statement language function semicolon operator operation code problem trap priority expression difference processing learning information content parameter that is case meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft vpn Redmi NVidia