Gnu c and ANSI C

Source: Internet
Author: User

Click here to share it with you!

The available C compiler on Linux is the gnu c compiler, which is based on the programming license of the Free Software Foundation and can be freely released. Gnu c has made a series of extensions for Standard C to enhance the functions of Standard C.
1. Zero-length Array
Gnu c allows zero-length arrays. This feature is particularly useful when defining the header structure of a variable-length object. For example:
Struct var_data
{
Int Len;
Char data [0];
};
Char data [0] only means that the data [Index] member of the var_data structure instance in the program can access the index address after Len. It does not allocate memory for the data [] array, therefore, sizeof (struct var_data) = sizeof (INT ).
Assume that the data field of struct var_data is stored in the memory area following struct var_data, and the data can be traversed through the following code:
Struct var_data S;
...
For (I = 0; I <S. Len; I ++)
{
Printf ("% 02x", S. Data [I]);
}
2. Case range
Gnu c supports the syntax such as case X... Y. The number of [x, y] In the interval meets the condition of this case. See the following code:
Switch (CH)
{
Case '0'... '9': C-= '0 ';
Break;
Case 'A'... 'F': C-= 'a ';
Break;
}
The case '0'... '9' in the Code is equivalent to the following code in Standard C:
Case '0 ':
Case '1 ':
...
Case '9 ':
3. Statement expression
Gnu c regards compound statements contained in parentheses as an expression called a statement expression, which can appear in any allowed expression. We can use loop variables and local variables that can only be used in composite statements in statement expressions, for example:
# Define min_t (type, x, y )\
({Type _ x = (x); Type _ y = (y); _ x <_ y? _ X: _ y ;})
Int IA, IB, mini;
Float FA, FB, minf;

Mini = min_t (INT, IA, IB );
Minf = mini_t (float, IA, IB );
Because the _ x ,__ y local variables are redefined, macros defined in the preceding method will not produce side effects. In standard C, the corresponding macro will produce side effects:
# Define min (x, y) (x) <(y )? (X) :( y ))
Code min (++ IA, ++ Ib) is expanded to (++ IA) <(++ Ib )? (++ IA) :( ++ Ib), the input macro parameter is added twice.
4. typeof keywords
The typeof (x) statement can obtain the X type. Therefore, we can use typedef to redefine the min macro:
# Define min (x, y )\
(Const typeof (x) _ x = (x );\
(Const typeof (y) _ y = (y );\
(Void) (<__ x ==<__ y );
_ X <_ y? _ X :__ y ;})
We do not need to input type like the macro min_t (type, x, y), because the type can be obtained through the keyword. The code line (void) (<__ x ==&__ Y) is used to check whether the types of _ x ,__ y are consistent!
5. Macro of variable parameters
Standard C only supports variable parameter functions, which means that the parameters of the functions are not fixed. For example, the printf () function prototype:
Int printf (const char * Format [, argument]...);
In gnu c, macros can also accept variable parameters, for example:
# Define pr_debug (FMT, Arg ...)\
Printk (FMT, # Arg)
Here Arg indicates that the remaining parameters can be zero or multiple. These parameters and the comma between them constitute the ARG value, replacing Arg during macro extension, for example, the following code:
Pr_debug ("% s: % d", filename, line)
Will be extended:
Printk ("% s: % d", filename, line)
The reason for using ## is that processing Arg does not represent any parameter. In this case, the preceding comma becomes redundant. After using ##, the gnu c Preprocessor discards the brackets. In this way, the Code:
Pr_debug ("success ")
Will be extended:
Printk ("success ")
Instead:
Printk ("success ",)
6. Label Elements
In standard C, the initialization values of arrays or struct must appear in a fixed order. In gnu c, the initialization values can appear in any order by specifying the index or struct member name.
To specify an array index, add "[Index]" before the initialization value. You can also specify a range in the form of "[first... last] =. For example, define an array and initialize all its elements to 0:
Unsigned char data [Max] = {[0... MAX-1] = 0 };
Structure initialization:
{
. Llseek = generic_file_llseek;
};
7. Current function name
Gnu c predefines two identifiers to save the name of the current function, __function _ to save the name of the function in the source code, __pretty_function _ to save the name with language characteristics. In C, the two names are the same. That is, it is only a string;
8. Special attribute Declaration
Gnu c allows you to declare special attributes of functions, variables, and types for manual code optimization and custom code check methods. To specify a declared attribute, you only need to add _ attribute _ (attribute) after the declaration ))
Attribute is an attribute description. If multiple attributes exist, they are separated by commas. Gnu c supports more than 10 attributes, such as noreturn format section aligned packed.
* The noreturn attribute acts on the function, indicating that the function never returns. This will allow the compiler to optimize the code and eliminate unnecessary warning information. For example
# Define attrib_noret _ attribute _ (noreturn ))....
Asmlinkage noret_type void do_exit (long error_code) attrib_noret;
* The format attribute can also be used as a function to represent printf scanf or strftime-style parameters. specifying the format attribute allows the compiler to check the parameter type based on the format string. For example:
Asmlinkage int printk (const char * FMT ,...)\
_ Attribute _ (format (printf, 1, 2 )));
* The unused attribute acts on functions and variables, indicating that the function or variable may not be used to avoid warning information generated by the compiler.
* The packed attribute applies to variables and types, indicating to compress the struct and use the minimum memory.
Struct examprl_struct
{
Char;
Int B;
Long C;
}__ Attribute _ (packed ));
Note that this _ attribute _ (packed) can only be used in gnu c;

* Section ("section-name ")
Attribute section is used for functions and variables. Generally, the compiler places the function in. text section, put the variables in. data or. BSS section. With the section attribute, the compiler can place functions or variables in the specified section. For example:
+++ Include/Linux/init. h
78: # DEFINE _ init _ attribute _ (_ Section _ (". Text. init ")))
79: # DEFINE _ exit _ attribute _ (unused, _ Section _ (". Text. Exit ")))
80: # DEFINE _ initdata _ attribute _ (_ Section _ (". Data. init ")))
81: # DEFINE _ exitdata _ attribute _ (unused, _ Section _ (". Data. Exit ")))
82: # DEFINE _ initsetup _ attribute _ (unused ,__ section _ (". setup. init ")))
83: # DEFINE _ init_call _ attribute _ (unused ,__ section _ (". initcall. init ")))
84: # DEFINE _ exit_call _ attribute _ (unused ,__ section _ (". exitcall. Exit ")))
The connector can arrange the code or data of the same section together. The Linux kernel prefers this technology. For example, the initialization code of the system is arranged in a separate section, after initialization, you can release this part of memory.
* Aligned (alignment)
The aligned attribute is used for variables, structures, or union types. It specifies the aligned quantity of variables, structure fields, structures, or associations, in bytes. For example:
++ Include/asm-i386/processor. h
294: struct i387_fxsave_struct {
295: Unsigned short CWD;
296: Unsigned short SWD;
297: Unsigned short TWD;
298: Unsigned short fop;
299: Long FIP;
300: Long FCS;
301: Long Foo;
......
308: }__ attribute _ (aligned (16 )));
It indicates that the variable of this structure type is 16 bytes aligned. Generally, the compiler selects an appropriate alignment, indicating that the specified alignment is usually caused by System Restrictions, optimizations, and other reasons.
* Packed
The property packed is used for variables and types. When used for variables or structure fields, it indicates that the minimum possible alignment is used. When used for enumeration, structure, or union type, it indicates that the type uses the minimum memory. For example:
++ Include/asm-i386/DESC. h
51: struct xgt_desc_struct {
52: Unsigned short size;
53: Unsigned Long Address _ attribute _ (packed ));
54 :};
The domain address will be allocated immediately after the size. The purpose of attribute packed is mostly to define hardware-related knots.
Structure, so that there is no holes between elements due to alignment.

9. built-in functions
Gnu c provides a large number of built-in functions, most of which are the built-in versions of the gnu c compiler labeled C library functions.
The names of other built-in functions that do not belong to library functions usually start with _ builtin, as shown below:
_ Builtin_return_address (level );

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.