Strange C language Features _c language

Source: Internet
Author: User

The features listed below may not be surprising, and some are interesting.

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

"AABBCCDD" [5] is equivalent to 5["AABBCCDD"]

This attribute can be used for arrays, pointers, strings, but not for variable definitions. K&r C Programming Language 217 pages are described here.

2) Two yuan, ternary compound character

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

string literal value??! will be considered |, so be careful when two question marks appear in the string at the same time. Binary compound characters are introduced in C99, as follows: equivalent to [

3) Duff ' s Device

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

The feature is that the switch and the while interleaving appear. Code Similar

4) The same name

There are detailed explanations in "C Traps and defects".

5) a[i++]= I;

This seems to be dependent on a particular compiler implementation, and I xcode the results of the experiment to first assign value to A[i] and then manipulate i++. This kind of code must be careful. If you're doing code review find ++– appearing in other expressions or as parameters, be sure to immediately move it out as a separate statement, carefully sailing the ship.

6) sizeof

sizeof (x), x can be an expression or type name, if it is an expression, not an operation, int x = 1; size_t sz = sizeof (x + +); x does not increase. T *p = NULL; p = malloc (sizeof *p); P did not mention the collar, and k&r the holy book.

sizeof unary-expr; sizeof (typename); a unary expression can be without parentheses, which is mentioned in the grammar section of the holy book. such as size_t f = sizeof 99;

7 macro definition to be careful

For example: #define FOO (a,b) (a+b)/(1-A) If you call Foo (bar++,4) This way, two more times, of course, it's very clear to expand the macro.

Resources:

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.