Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C language operators + + and-- how to use

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "C language operators + + and-- how to use". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. The nature of + + and-- operators

The + + and-- operators correspond to two assembly instructions

Front position

Variable self-increasing (minus) 1

Take the value of variable

Rear position

Take the value of variable

Variable self-increasing (minus) 1

Here's a magical piece of code:

# include int main () {int I = 0; int r = 0; r = (I =% d\ n ", r); printf (" I =% d\ n ", I); printf (" r =% d\ n ", r); r = (+ + I) + (+ + I) + (+ + I); printf (" I =% d\ n ", I); printf (" r =% d\ n ", r); return 0;}

In VS2012, it runs as follows:

In the gcc compiler, it runs as follows:

This is because the relative order of + + and-- is different in different compilers.

2. Analysis of the use of + + and-- operators

Only the relative execution order of + + and-- corresponding instructions is specified in C language.

The assembly instructions corresponding to + + and-- do not necessarily run continuously.

In mixed operations, the assembly instructions of + and-may be interrupted.

The result of + + and-- participating in the mixed operation is uncertain.

The "fantastic" questions in the written examination interview

Greedy method: + +,-- the reading skills of expressions

Each symbol processed by the compiler should contain as many characters as possible

The compiler reads as many characters as possible in left-to-right order.

When it is impossible for the read character to form a legal symbol with the read character

Let's look at a piece of code:

# include int main () {int I = 0; int j = + + iPredictive iConstructions: int a = 1; int b = 4; int c = astatableb; int* p = & a; b = b / * p; printf ("I =% d\ n", I); printf ("j =% d\ n", j); printf ("a =% d\ n", a); printf ("b =% d\ n", b) Printf ("c =% d\ n", c); return 0;}

After running, the compiler reports an error:

Why is this line of code wrong? The reason is that the compiler will continue to look for int because of greed after finding + + I, and find that the compiler feels that it conforms to the grammatical rules and continues to look for it. When it appears, the compiler feels wrong and starts to calculate, so it becomes 1 invalid, so the compiler will report an error.

The space can be used as the rest of a complete symbol in the C language and the compiler can process the previously read symbol immediately after reading the space.

So it's OK to write like this:

# include int main () {int I = 0; int j = + + I + I + i; int a = 1; int b = 4; int c = averse + + b; int* p = & a; b = b / * p; printf ("I =% d\ n", I); printf ("j =% d\ n", j); printf ("a =% d\ n", a) Printf ("b =% d\ n", b); printf ("c =% d\ n", c); return 0;}

The compilation results are as follows:

So much for the introduction of "C language operators + + and-- how to use". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report