C Pitfalls and grammatical pitfalls of defects

Source: Internet
Author: User
Tags signal handler

    2.1 Understanding function Declarations

    no matter what C the declaration of a variable is made up of two parts: a type and a set of declaration symbols that resemble expressions. Like

    float F;

    The implication of this Declaration is that when evaluated, the type of the expression F and g is the floating-point number class. Due to the sound

    Mingfu is similar to an expression. So we can also use parentheses in the declarator:

    Float ((f))

    The implication of this Declaration is that when it is evaluated, the type of W is a floating-point type, which can infer that f is also a floating-point type. Similar to.

    float *g (), (*h) ();

    represents *g () with the (*h) () is a floating-point expression.

    Because () the combination of precedence is higher than *. *g () is the * (g ()): G is a function. The return value type of the function is a pointer to a floating-point number. Similarly, it is possible to conclude that H is a function pointer, andthe return value of the function that H points to is a floating-point type.

    after understanding the above knowledge. We are able to analyze the signal function, andthe prototype of thesignal function is:

    #include <signal.h>

    void (*signal (int signo, void (*func) (int)))) (int);

    Signal The function prototype shows that the function requires two parameters, returns a function pointer, and the function that the pointer points to requires an integer parameter with no return value.

    The first parameter, Signo , is an integer, and the second is a function pointer. The function it points to requires an integer parameter with no return value. Descriptive narration in natural language is to pass an integer parameter to the signal processing program, but it has no return value. When you call signal to set a signal handler, the second parameter is a pointer to the function (that is, the signal handler).

    Signal The return value is a pointer to the previous signal handler.

    2.2 precedence issues for operators

    Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzgfuy2h1/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/center ">

    The highest priority is in fact not the real operator, including: array subscript, function call operator each structure member selection operator. They are all white Zou combined. So the meaning of A.B.C is (A.B). C, not a. (B.C).

    The precedence of the monocular operator is second only to the preceding operator. In the full true sense of the operator. They have the highest priority.

    Since the priority of a function call is higher than the precedence of a single-mesh operator, it is assumed that P is a function pointer. To invoke the function that P points to, you must write this: (*p) (). Suppose written as *p (). The compiler interprets it as * (P ()). Type conversions are also single-mesh operators. Its priority is the same as the precedence of other single-item operators. The monocular operator is white right to left

    , so *p++ is interpreted by the compiler as * (p++), which takes the object pointed to by the pointer p. Then increment p by 1: instead of (*p) + +, take the object that the pointer p points to, and then increment the object by 1.

    The priority is lower than the monocular operator, followed by the binocular operator.

    In binocular operators, arithmetic operators have the highest precedence. The shift operator is second, the relational operator again, and then the logical operator. The assignment operator, and finally the conditional operator (the three-mesh operator).

    The two most important points we need to remember are:

    1. No matter what the precedence of a logical operator is lower than whatever a relational operator is.

    2. The shift operator has a lower precedence than the arithmetic operator, but is higher than the relational operator.

    In all operators, the comma operator has the lowest precedence. This is very easy to remember, because the comma operator is often used to replace a semicolon as a statement end flag in situations where an expression is required instead of a statement.

    2.3 note the semicolon as the statement end flag

    In the C program, it is assumed that careless writing of a semicolon may not cause any undesirable consequences: this semicolon may be considered an empty statement that does not produce any actual effect, or the compiler will generate a warning message from this redundant semicolon, which can be easily removed by warning messages. An important exception is when you need to follow a statement immediately after the IF or while statement. Assuming that there is a semicolon at this point, then the statement immediately following the if or while sentence is a separate statement. and the conditional inference part has no matter what the relationship. Consider the following

    Here's the example:

    if (x [I]>big);

    Big=x[i];

    2.4 Switch Statement

    Switch Note in the statement Case back Add Break . Such talents play a role of control. Of course, there are times when it's good to do without break to achieve some kind of effect.

    2.5 Suspension Else issues that Arise

    C in Language if Else Pairing rules: Else is always the same as a pair of parentheses in the recent match if combined.

    This, for example, could and would violate the programmer's intentions:

    if (x==0)

    if (y==0) error ();

    else{

    z = x + y;

    }

    After indentation. Into this

    if (x = = 0)

    if (y = = 0)

    Error ();

    else{

    z= x + y;

    }

    in order to avoid such a situation, the if and the Else curly braces are added to the back .

C Pitfalls and grammatical pitfalls of defects

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.