The GNU program, also known as the German plan, was launched by Richard Stallman in September 27, 1983. It aims to create a completely free operating system. When writing Linux, it creates a standard to become the gnu c standard. ANSI American National Standards Association, which made the Standard ansi c standard for c, was later accepted by the International Standards Association as standard C, so ansi c and Standard C are a concept
In general, Linux also supports Standard C. In the future, standard C can be used across platforms, while gun C is generally only used in Linux C.
18.1 ANSI CAnd Standard C ++Difference
Here ansi c refers to the latest standard-c99
1. ansi c does not support reference
2. ansi c does not support function overloading.
3. ansi c has two integers (long and unsigned long), but the latest C ++ compiler supports these two integers.
4. ansi c does not support variable initialization in C ++, for example, int A (8 );
5. The struct keyword must be used when ansi c declares the structure, but the standard C ++ does not
6. Some header files in the ansi c standard library have new names in the Standard C ++, such as ctime, cstring, climits, cfloat, and cctype. Some files are not only named
7. ansi c does not support namespaces.
8. ansi c does not contain the bool type, and the true and false keywords.
9. When declaring a function, the parameter is null and has different meanings. In ansi c, parameters of any number are accepted, while in Standard C ++, parameters are not accepted.
10. ansi c does not support inline functions.
11. asni C does not support default parameters
12. ansi c does not support the scope parsing operator (: :) that can be used for global variables (::)
13. Global constants defined by const have external links in ansi c and internal links in Standard C ++, therefore, extern must be used to declare global constants with external links in Standard C ++, for example, extern const int A = 10;
18.02gnu CRatioANSI CExtended location
1. Zero-length array allowed
Gnu c allows zero-length arrays. This feature is useful when defining the header structure of a variable-length object.
Struct var_data
{
Int Len;
Char data [0];
};
Char data [0] only means that the data [Index] member of the var_data struct instance in the program can access the index address after Len, and no memory is allocated for data [0.
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 case X... in the syntax such as Y, the number of intervals [x, y] will meet the condition of this case. Remember that during the data structure test, some students used only 100 cases for menus, fortunately, I am working on a GUI.
Switch (c)
{
Case '0'... '9': C-= '0 ';
Break;
Case 'A'... 'F': C-= 'a'-10;
Break;
Case 'A'... 'F': C-= 'a'-10;
Break;
}
We can see the characteristics of this case. How many cases are missing than standard C?
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, such
# Define min_t (type, x, y )/
({Type _ x = (x); Type _ y = (y) ;__ x <__ y? _ X :__ y })
Int IA, IB, mini;
Mini = min_t (INT, IA, IB );
In this way, the macro defined in the above method will not have any side effects because the two local variables _ x and _ y are redefined. In standard C, the corresponding macro usually has the following side effects:
# Define min (x, y) (x) <(y )? (X) :( y ))
The Code min (++ IA, ++ Ib) is expanded
(++ IA) <(++ Ib )? (++ IA) :( ++ Ib) the input macro parameter is added twice.
This is mentioned in the basic 0x10 questions that embedded programmers should know.
4. typeof keywords
The typeof (x) statement can obtain the X type. Therefore, we can use typeof to redefine the min_t macro mentioned in Article 3rd.
# 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 pass in a type as in the third article, because type can be obtained through typeof (X.
Code (void) (& _ x = & _ y); checks whether the types of _ x and _ y are consistent.
5. Macro of variable parameters
Standard C only supports functions with variable parameters, meaning that the parameters of the function can be unfixed.
For example, the prototype of the printf () function is
Int printf (const char * Format [, argument]...)
In gnu c, macros can also accept variable parameters, such
# 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,
Replace ARG in macro extension, for example
Pr_debug ("% s: % d", filename, line );
Expanded
Printk ("% s: % d", filename, line );
The reason for using ## is that the processing of Arg does not represent any parameters. In this case, the preceding comma becomes redundant.
After using ##, the gnu c Preprocessor discards the preceding comma so that the code
Pr_debug ("success! /N ") will be correctly extended to printk (" success! /N ")
Instead of printk ("success! /N ",);
6. Label Elements
Standard C requires that the initialization values of arrays or struct appear in a fixed order. In gnu c, the initialization values can appear in any order by specifying the index or struct member name.
The method for specifying an array index is to add [Index] = before the initialization value. Of course, you can also specify a range in the form of [first... last] =. For example, the following code defines an array and assigns all elements to 0:
Unsigned char data [Max] = {[0... MAX-1] = 0 };
The following code uses the struct member name to initialize the struct:
Struct file_operations demo_fops = {
Owner: this_module,
Llseek: demo_llseek,
Read: demo_read,
Write: demo_write,
IOCTL: demo_ioctl,
Open: demo_open,
Release: demo_release,
};
However, standard C is recommended for Linux 2.6 as follows:
Struct file_operations demo_fops = {
. Owner = this_module,
. Llseek = demo_llseek,
. Read = demo_read,
. Write = demo_write,
. IOCTL = demo_ioctl,
. Open = demo_open,
. Release = demo_release,
};
7. Current function name
Gun C predefines two identifiers to save the current function name, __function _ The Name Of The saved function in the source code,
_ Pretty_function _ saves names with language characteristics. In the C function, the two names are the same.
Void example ()
{
Printf ("this is function: % s" ,__ function __);
}
In the code, _ function _ indicates the string "example"
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 )));
Detailed can see http://blog.163.com/sunm_lin/blog/static/9192142200741533038695/
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 aligned attribute specifies the aligned mode of the struct, variable, and consortium. 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
For structure alignment under VC, refer to http://hi.baidu.com/deep_pro/blog/item/421db081aeb604debd3e1e01.html
9. built-in functions
Gnu c provides a large number of built-in functions, many of which are the built-in versions of Standard C library functions, such as memcpy, which share the same functions as the corresponding C library functions. This article does not discuss such functions, the names of other built-in functions usually start with _ builtin.
* _ Builtin_return_address (level)
The built-in function _ builtin_return_address returns the return address of the current function or its caller. The level parameter specifies the number of frames to be searched on the stack. 0 indicates the return address of the current function, 1 indicates the return address of the caller of the current function, and so on. For example:
++ Kernel/sched. c
437: printk (kern_err "schedule_timeout: Wrong timeout"
438: "Value % lx from % P/N", timeout,
439: _ builtin_return_address (0 ));
* _ Builtin_constant_p (exp)
The built-in function _ builtin_constant_p is used to determine whether a value is a compilation constant. If the exp value of the parameter is a constant, the function returns 1; otherwise, 0. For example:
++ Include/asm-i386/bitops. h
249: # define test_bit (NR, ADDR )/
250: (_ builtin_constant_p (NR )? /
251: constant_test_bit (NR), (ADDR )):/
252: variable_test_bit (NR), (ADDR )))
Many calculations or operations are more optimized when the parameter is a constant. In gnu c, the above method can be used to compile only the constant version or a very few version based on whether the parameter is a constant, in this way, it is both universal and can compile the optimal code when the parameter is a constant.
* _ Builtin_ct (exp, c)
The built-in function _ builtin_predict CT is used to provide branch prediction information for the compiler. Its return value is the value of the integer expression exp, and the value of C must be the compile time. For example:
+++ Include/Linux/compiler. h
13: # define likely (x) _ builtin_exact CT (x), 1)
14: # define unlikely (x) _ builtin_exact CT (x), 0)
++ Kernel/sched. c
564: If (unlikely (in_interrupt ())){
565: printk ("Scheduling in interrupt/N ");
566: Bug ();
567 :}
The semantics of this built-in function is that the expected value of exp is C. The compiler can sort the order of statement blocks according to this information, so that the program can be executed more efficiently as expected. The above example indicates that the target code in the interrupt context rarely occurs. The target code in lines 565th-566 may be placed in a far location to ensure that the target code that is executed frequently is more compact.