Strange C Language Features

Source: Internet
Author: User

The features listed below may not be strange, but some are interesting.

1) a [2] is equivalent to 2 [a]

"Aabbccdd" [5] is equivalent to 5 ["aabbccdd"]

This feature can be used for arrays, pointers, and strings, but cannot be used for variable definition. On the K & r c Programming language 217 page, we will introduce this.

2) binary and ternary composite characters

Http://en.wikipedia.org/wiki/Digraphs_and_trigraphs

String Literal Value ??! Will be considered as |, so be careful when two question marks appear in the string at the same time. Binary composite characters are introduced in C99, for example, <: equivalent [

3) Duff's Device

Http://en.wikipedia.org/wiki/Duff%27s_device

Switch and while are staggered. Similar code

4) Same Name and surname

The C traps and defects are described in detail.

5) a [I ++] = I;

This seems to depend on the implementation of a specific compiler. In Xcode, I first assigned an I value to a [I] And then operated on I ++. Be careful with this type of code. If you find that ++ appears in other expressions or as a parameter in code review, you must immediately move it out as a separate statement, and be careful when driving the ship.

6) sizeof

Sizeof (x), x can be an expression or type name. If it is an expression, int x = 1; size_t sz = sizeof (x ++); X will not increase. T * p = NULL; p = malloc (sizeof * p); p has not been mentioned, and the K & R holy book also speaks.

Sizeof unary-expr; sizeof (typename); The unary expression can have no parentheses, which is mentioned in the syntax section of the holy book. For example, size_t f = sizeof 99;

7) Be careful with macro definition

For example: # define FOO (a, B) (a + B)/(1-a) If FOO (bar ++, 4) is called in this way, it is automatically added twice. Of course, it is very clear to expand the macro.

References:

Http://www.steike.com/code/useless/evil-c/

C traps and 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.