Comma operator for generation of alien code

Source: Internet
Author: User

Comma operator for generation of alien code

 

Code reduction does not mean increasing readability. On the contrary, excessive code reduction makes the code more difficult and difficult to maintain. This article does not advocate misuse of the distorted alien C syntax, but serves as a interesting record. It will give readers a brief introduction to how some inexplicable code is made.

The C-school language provides many strange operators. One of the most strange operators is the comma operator, which does not actually make much sense ", only calculate the values of each operation item, and then return the value of the rightmost operation item. However, this makes it possible to combine multiple expressions into one:

A = 1;

B ++;

C * = 4;

 

Merged:

A = I, B ++, C * = I;

It seems that it doesn't make much sense, but if it is used in a loop, it will be different:

While (I <n)

{

A = I;

B ++;

I ++;

}

Can be abbreviated:

While (A = I ++, B ++, I <n);/* Note that there must be no less semicolons */

In C ++, because the output statement using stream objects such as cout is actually an expression, the code for printing the value of the entire array is as follows:

For (I = 0; I <size; ++ I)

Cout <A [I] <Endl;

Use the comma operator and take advantage of the features of the auto-increment operation, which can be abbreviated:

For (I = 0; I <size; cout <A [I ++] <Endl);/* Note that there must be no less semicolons */

For the operation of printing a two-dimensional array, the outer loop (traversing each row) has to use a composite statement because the line feed is output after the loop is completed:

For (I = 0; I <size; ++ I)

{

For (j = 0; j <size; ++ J)

Cout <A [I] [J];

Cout <Endl;

}

However, if you move cout to the loop, you can significantly reduce the number of rows:

For (I = 0; I <size; cout <Endl; ++ I)

For (j = 0; j <size; cout <A [I] [J ++]);/* Note that there must be no less semicolons */

 

Of course, it can also be

For (I = 0; cout <Endl, I <size; ++ I)

For (j = 0; cout <A [I] [J], j <size; ++ J);/* Note that there must be no less semicolons */

 

But when using the comma operator, you must pay attention to its implicit uncertainty, such as the expression:

++ I, cout <A [I], X + Y;

If the order of evaluation for each expression in the language is not determined, then the [I] output by the cout subexpression cannot be determined whether it is the I before the auto-addition or the I after the auto-addition. pay special attention to this when using the comma operator and the related continuous expressions of the overload operator.

Note: Due to the rush of time, the program segment in this article has not been debugged. If any error occurs, please criticize and correct it.

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.