Chapter 2 Syntax "trap" of "reread"

Source: Internet
Author: User
Tags arithmetic operators bitwise operators

 

After reading the first chapter, I found that the first section is still so profound ~ Haha, I read the second chapter today. Although it is troublesome

 

If you have mastered the key words mentioned by the author, it is not very difficult, but some declarative methods are still quite weird.

 

Maybe I have never heard of it because I have never used it. I found some errors in my book ~ Haha, This Is

 

It's a big success.

 

1.Understanding of function declaration.

 

In C, the statement is divided into two parts:TypePart and groupDeclaration Operator similar to expression. Declaration operator from table

 

This is similar to the expression. If you evaluate it, a result of the given type in the Declaration is returned. Once you know

 

How to declare a variable of the given type, the type conversion operator of this type is easy to get: as long as the variable in the Declaration is not

 

Remove the semicolon at the end of the sentence, and then encapsulate it with parentheses. For complex statements, we only need one layer.

 

A layer of analysis will gradually understand the meaning.

 

2.Operator priority.

 

In the C language, operators are quite complex, not only the priority, but also the associativity.

 

In this section, the author solved the priority problem for us. At the same time, I also found errors in this chapter. First, let's talk about

 

The priority of the operators.

 

HighestYes:()(Function call operators ),[](Subscript operator ),(->And.) (Structure member selection operator) these four operators.

 

SecondYes:Single Object Operator.AgainYesArithmetic Operators, Where% */Ratio+
-
High priority.

 

AgainYes:Shift Operator.AgainYesRelational operators, Where
<> <=> =Ratio
=! =High priority.

 

AgainYes:Bitwise operatorsThe priority of bitwise AND is higher than that of bitwise OR. The priority of bitwise OR is between the two.

 

AgainYes:Logical operators.

 

AgainYes:Conditional Operators(Unique three-object operator ).

 

AgainYes:Value assignment operator(Including compound assignment operators ).

 

LastYes:Comma Operator

 

We need to remember two points.:1.The priority of any logical operator is lower than that of any relational operator.2.Shift

 

The priority of an operator is lower than that of an operator, but higher than that of a relational operator.

 

Detected errors:

In the original book (Chinese version), priority is divided into four operators of the highest level, single object operator and binary operator,

 

Binary operators are classified into operator operators, displacement operators, Relational operators, logical operators, value assignment operators

 

Operator. The error occurs here. The true priority of the value assignment operator should be

 

The bitwise operator is low, which is also mentioned later. Here, the priority of the value assignment operator is higher than that of the conditional operator.

 

The priority is high. This is the first (temporary) error in my book. The second error is that the conditional operator is used as the binary element.

 

Operator, of course, the translator also pointed out that this mistake should be the mistake of the author of the original book, and the first one is unknown.

 

The book also mentions that "the priority of all bitwise operators is higher than that of the operators in order, and the priority of each 'and' operator is higher.

 

Level is higher than the priority of the corresponding 'or' operator, while the priority of bitwise OR operator is between bitwise AND operator and bitwise OR

 

Between operators ". I do not know whether this sequence operator is a comma operator or a comma operator ,? : And, the four operators,

 

I haven't found it yet, but someone on the Internet calls the comma operator the order evaluate operator, but according to my understanding

 

Test, I personally think it refers to four types of operators, which will be found to be being changed in the future. If there is a publisher, you can leave a message or send an email.

 

Thank you for your cooperation.

 

3.Note the semicolon used as the statement end flag.

 

Pay special attention to statements such as if, while, return, and struct declaration.

 

4. SwitchStatement.

 

Suggestion:Remember to addBreak;In special cases, you need to share a few words.

 

Write comments. The default statement is optional and can be placed in any position. We recommend that you put it at the end and add a break at the end.

 

Statement, just in case.

 

5.Function call.

 

Different from some languages, whether or not a function has parameters, the call must contain parentheses. For example, F (); and F; F (); yes

 

Function call, But F; does nothing. The more precise point is to calculate the address of function f and does not call this function.

 

6."".

 

According to the C language processing method, else will be paired with the first if, so when the if statement is nested

 

It will lead to the issue of "suspension", and even experienced programmers will make such a mistake. For example, the following code:

 

If (x = 0)

 

If
(Y = 0)

 

Err ();

 

Else {

 

Z
= X + y;

 

F (& Z );

 

}

 

The real purpose of this Code is to first judge whether X is equal to 0. If yes, then judge whether y is equal to 0. If yes, call err

 

Function, no, then do nothing; if X is not equal to 0, execute the else statement. However, due to the "suspension" problem

 

The program and the programmer's intent are quite different. Sort out the code:

 

If (x = 0)

 

If
(Y = 0)

 

Err ();

 

Else
{

 

Z
= X + y;

 

F (& Z );

 

}

 

That is to say, what this code really implements is to first judge whether X is equal to 0, yes, then judge whether y is equal to 0, yes, then execute err

 

Function. If not, the else statement is executed. If X is not equal to 0, nothing is done.

 

We recommend that you enclose the if statement in parentheses, as shown in the following code:

 

If (x = 0 ){

If
(Y = 0)

Err ();

} Else {

Z
= X + y;

F (& Z );

}

 

You can also perform some macro definitions and perform the following operations:

 

# Define if {

 

# Defien then }{

 

# Define else} else {

 

# Defien fi }}

 

If X = 0 then

 

If
Y = 0 then

 

Err ();

 

Fi

 

Else

 

Z
= X + y;

 

F (& Z );

 

Fi

 

This is the content of Chapter 2.

 

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.