Ugly mask

zhaozj2021-02-12  148

Ugly mask

Preface: This article is I am officially written in Herb Sutter online http://www.gotw.ca/gotw/086.htm first two weeks, in the formal answer, the difficulty is lowered. As mentioned in the following, I thought I thought I was just the appetizer before the dinner, but I dinner came out, I feel that my aperweed has made readers. :) The title name is changed, and more attractive is more in line with the facts. :)

C is in addition to the limitation of the general people's thinking of the general people because of the academic research and engineering practice, its complexity and the freedom to get rid of various controls.

In the field of academic research, through the 20 years from birth to now, C is in various competitions with Fortran, C, Pascal, and the birth of C #, whether it is a variety of programming paradigms (PARADIGM) Flexible use, or as a program language design, various inspirational source live water, C can be said to be home! However, in the field of engineering practice, due to the freedom of C uncontrolled, there is a huge complexity, and there is a word: freedom never has no price. (FreeDom IS Not Free! In addition to the field of real-time, running efficiency, compatibility with strict requirements, in today's various commercial use, network application, Java can be said to be the martial arts supreme, Baodao Dragon, the scenery has been a few years. It's just now with the heavenly sword c # already scabble. In the enterprise-class application, choose that, is your freedom, but it is gratifying, just like a Dragon Sherry, hey the sword is from the Xuan Tie Baojian, Java and C # also come from the name C , so, They are like twins, even if there is a difference, that is very small, one language, and the other is not too much.

I am too far from the topic. Let me pick a kite back. Today I want to solve the two problems of the gotw86 in "Tianshi catch":) (GURU of the week my jog). In fact, it is solved, it is better to say that the answer on the newsgroup is summarized, and a little understanding is counted. Below I will express the GOTW86 in Chinese as follows: http://www.gotw.ca/gotw/086.htm

Slight typing error? Graphical language and other rare weird puzzles: 8/10 (slight type and other curiosities Difficulty: 8/10)

Sometimes or even small, difficult typing errors may accidentally have a huge impact on code meaning. To illustrate how difficult the typing error, and how easy you can have an illusion, even if they don't exist, please see the example below:

problem

Prawn problem

Don't use a compiler, please ask the following questions.

1. What is the output of the following programs on a standard compatible C compiler?

#include

int main ()

{

INT x = 1;

For (int i = 0; i <100; i);

// What will the next line do? Increment ??????????? / x;

Std :: cout << x;

}

2. How many different errors will be reported when compiling under a standard compatible C compiler?

Struct x {

Static Bool F (INT * P)

{

Return P && 0 [P] and not p [1: >> P [2];

}

}

answer

It's coming ...

It seems that many people have impatiently impatiently impatiently waited for the answer. I will try it before the final standard answer, and I will try to give you a big meal J. The essence of these two issues is covered under the mask.

1.answer: First of all, I will seize a demon in front of the sinful heart.

(1) for (int i = 0; i <100; i);

I think a lot of people have seen it, yes, you are right, semicolon ";". This is a separate statement because there is a semicolon behind, there is no impact on the statement behind the code. In a modern compiler, this sentence will even be optimized. For this sentence, our programmer should make it more sincere! I usually write this:

For (int i = 0; i <100; i)

; // understand the empty statement

Let's Go ON!

(2) The following sentence is the real sinful heart! This is a comment, yes, the sky! It is a pity that it is a nest of sinful soul hiding. The reason why it can escape your fire, our culture is too single, here we have a few people to understand a few countries? Such elites are not interested in C , don't see me Article.

If you are a non-English European European, then your keyboard is likely nothing to do now, the character set is different! The character set of the ASCII code is designed by ISO, but in most European countries ISO-646 characters, these locations are from non-English letters. . Therefore, many Europeans use the keyboard without these letters, but not, these letters are frequently used in C . So a compromised program is used:

In C , there are several icons to indicate these characters, as follows:

Keywords: icon comparison table

Keywords

Digraphs

Trigraphs

And && <% {?? = # and_eq & =%>} ?? ([Bitand & <: [?? <{bitor |:>] ?? / / compl ~%: # ??)] not!%: &: ## ??>} or ||

?? '^ OR_EQ | =

??! | xor ^

?? - ~ xor_eq ^

Not_eq! =

For example, you can replace "?? /" in C .

OK, the kite must be returned again. Now let's take a look at the original code.

// What will the next line do? Increment ??????????? /

This sentence, take a look at it, you will be like the top. Replace with Trigraph (3master) [Note]. The result is: // What Will the next line do? Increment ????????? /

Note: (Interestingly, if you look at the GNU G Documentation for the --wtrigraphs command-line switch, you will encounter the folloading statement:

"WARNINGS Are Not Given for Trigraphs with Xin Comments, as They Do Not Affect The Meaning Of The Program")

Interestingly, if you use the command line -Wtrigraphs to check the GNU G document, you will find the following description:

"Trigraphs in the comment does not have a warning because they do not affect the meaning of the program."

Thanks to TAODM reminders, it is true that DEVC 4.9.7.0 is true, it will be directly ignored.

OK, see the last character, this time you should think about the macro (Macro), look at one example:

#define mymacro (a, b) /

IF (XYZZY) ASDF (); /

Else

What is the role of "/", the wrap connection symbol, the next line is the continuation of this line. and so

// What will the next line do? Increment ??????????? /

x;

Equal

// What will the next line do? Increment ????????? x;

x; annotated. The corrected procedure is as follows:

#include

int main ()

{

INT x = 1;

For (int i = 0; i <100; i)

;

// What will the next line do? Increment ????????? x;

Std :: cout << x;

}

Is there anything in the code in the code?

The nine-story platform begins with the soil. With this foundation, the following road is better.

2. The aanswer: That is like a cattle in the eyes of the eyes, this question is like a cow in the eyes, and it is also easy to make a long! We convert the icon to a formal form:

And is &&

Not is!

:> Yes]

Do you still remember what A [i] said? (I have mentioned in the "Memory"), there is an old saying in China: Qing Father is not dead, Lu is not there. (Qing Father is a nobleman in the Spring and Autumn Period, if he is not dead, Lu Guo's disaster will not end.) In us, the mask is not awkward!

A [i] is the shorthand of * (a i), OK! That 0 [P]? We will raise three, 0 [P] is a shorthand of * (0 p), it is equal to * (p 0), is short-written as P [0]. Is it already very clear now! J

Now Return P && 0 [P] and not p [1: >> P [2]; we stripped all ugly masks and give it beautiful real body! Return P && P [0] &&! p [1]> p [2];

The last tail: "!" The priority is greater than ">", so the above sentence is:

Return P && P [0] && ((! p [1])> p [2]);

Perhaps someone will say there is another mistake, then there is a semicolon, the function is used to use a large bracket, and there should be a selection of snakes, but it doesn't matter, it is not added, it is not more simple. . The current episode of the current code is:

Struct x {

Static Bool F (INT * P)

{

Return P && P [0] && ((! p [1])> p [2]);

}

}

If you have a solution, you can add it.

Is it just awake from the nightmare! Congratulations, in the thick forest in C , there are countless devils, there are countless treasures! It welcomes fearless warriors!

Just in our own code, we should also remember the sincerity, not special, the crowd! (The film "big wrist" line). Because you are the code to give the customer, maybe it can't take a long time, you are this customer!

No one doesn't like to be sincere, at least he will hope that others are sincere!

I like others sincerity, I should be sincere!

Wu Tong wrote in 2003.4.28

Recently modified 2003.6.20

转载请注明原文地址:https://www.9cbs.com/read-6140.html

New Post(0)