Cool C-language skills

Source: Internet
Author: User

Turn from: http://www.kuqin.com/language/20120423/320158.html

C language often makes people feel that what it can express is very limited. It does not have advanced functionality such as first-level functions and pattern matching. But C is very simple, and there are still some very useful grammatical skills and functions, but not many people know it.

the specified initialization

Many people know to initialize arrays statically like this:

1 int fibs[] = {1, 1, 2, 3, 5};

The C99 standard actually supports a more intuitive and simple way of initializing various collection class data (e.g., struct, federation, and Array).

Array

We can specify the elements of the array to initialize. This is useful, especially when we need to maintain a synchronized update of a mapping relationship based on a set of #define. Look at the definition of a set of error codes, such as:

1 2 3 4 5 6 7 8 9 10 /* Entries may isn't correspond to actual numbers. Some entries omitted. * * #define EINVAL 1 #define ENOMEM 2 #define EFAULT 3/* .../#define E2BIG 7 #define EBUSY 8/* ... * * #define ECHILD 1 2/* ... * *

Now, suppose we want to provide a string of error descriptions for each error code. To ensure that the array is kept up to date, we can use the syntax specified by this array, regardless of any modifications or additions made to the header file.

1 2 3 4 5 6 7 8 9 10 11-12 Char *err_strings[] = {[0] = "Success", [einval] = "Invalid argument", [enomem] = "Not enough memory", [efault] = "Bad Address",/* ... [e2big] = "Argument list too Long", [ebusy] = "Device or resource Busy",/* ... * [Echil D] = "No Child Processes"/* ... */};

This allows for a static allocation of sufficient space and guarantees that the maximum index is legitimate, initializes the special index to the specified value, and initializes the remaining index to 0. (Bole online map)

Structural Body and consortium

1 It is useful to initialize data with the field names of structs and federations. Let's say we define:
1 2 3 4 5 struct point {int x; int y; int z;}
1 And so we initialize struct point:
1 struct point P = {. x = 3,. y = 4,. z = 5};

When we don't want to initialize all the fields to 0 o'clock, it's easy to build the structure at compile time without having to call an initialization function specifically.

For a consortium, we can use the same approach, except that we initialize only one field.

List of macros

One of the idioms in C is that there is a named list of entities, you need to create a function for each of them, initialize each of them, and extend their names in different code modules. This is often used in Mozilla's source code, and I learned this technique at that time. For example, in the project that I worked on last summer, we had a list of macros that were tagged for each command. It works in the following ways:

1 2 3 4 5 6 7 8 #define Flag_list (_) _ (Inworklist) \ _ (emittedatuses) \ _ (Loopinvariant) _ (commutative) _ (movable) \ _ (lowered) \ _ ( Guard)

It defines a flag_list macro, which has a parameter called _, which itself is a macro that can invoke each parameter in the list. A practical example might be a more intuitive illustration of the problem. Suppose we define a macro define_flag, such as:

1 2 3 4 5 6 7 #define DEFINE_FLAG (FLAG) FLAG, enum FLAG {None = 0, flag_list (define_flag) Total}; #undef Define_flag

Extensions to Flag_list (Define_flag) can get the following code:

1 2 3 4 5 6 7 8 9 10 11 Enum Flag {None = 0, Define_flag (inworklist) Define_flag (emittedatuses) Define_flag (loopinvariant) Define_flag ( Commutative) Define_flag (movable) define_flag (lowered) Define_flag (Guard) Total};

Next, expand the Define_flag macro for each parameter so that we get the enum as follows:

1

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.