GNU c, ANSI c, standard c, standard C + + differences and links

Source: Internet
Author: User

The GNU program, also known as the slave program, was publicly launched by Richard Stallman on September 27, 1983. Its goal is to create a set of completely free operating Systems. It has made a standard for the GNU C when writing Linux. ANSI American national standards association, the standard ANSI C standard that it made to 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, after which standard C can be cross-platform, and gun C is typically used only under Linux C

18.1 ANSI C and standard C + + the Difference

The ANSI C here refers to the latest standard-c99

1. ANSI C does not support referencing

2, ANSI C does not support function overloading

3, ANSI c more than two integer (long long, unsigned long long), but the latest C + + compiler has supported the two integral types

4, ANSI C does not support a variable initialization method in C + +, for example: int a (8);

5, ANSI C Declaration structure must use the struct keyword, and standard C + + does not require

6, some header files in ANSI C standard library, have new names in standard C + +, such as ctime, cstring, climits, cfloat, cctype, Some files are not only the change of name

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 meaning of the null parameter is Different. An arbitrary number of arguments are accepted in ANSI c, whereas parameters are not accepted in standard C + +

10, ANSI C does not support inline functions

11, ASNI C does not support default parameters

12. ANSI C does not support scope resolution operators that can be used for global variables (::).

13. Global constants defined with const are externally linked in ANSI C and have internal linkage in standard C + +, so the global constants that declare external linkage in standard C + + must use extern, for example: extern const int a = 10;

18.02GNU C than ANSI C Where to expand

1. Allow 0-length arrays

GNU C allows a 0-length array, which 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 a struct instance through Var_data in the program can access the index address after Len and does not allocate memory for data[0].

Assuming that the data domain of the struct var_data exists in the memory area immediately following the struct var_data, the data can be traversed by:

struct Var_data s;

...

For (i=0;i<s.len;i++)

{

printf ("%02x", s.data[i]);

}

2. Case Scope

GNU C supports the syntax of case x...y, the number of intervals [x, y] will meet the conditions of that case, remember the data structure test, some students to do the menu using only 100 cases, Fortunately I do is the GUI

Switch (c)

{

Case ' 0′ ' 9′: c-= ' 0′;

Break

Case ' a ' ... ' F ': c-= ' a '-10;

Break

Case ' a ' ... ' F ': c-= ' a '-10;

Break

}

The case is a feature that everyone can see, less than the standard C knock how many case ah

3. Statement expression

GNU C considers the compound statement contained in parentheses as an expression, called a statement expression, that can appear anywhere an expression is Allowed. We can use Loop variables, local variables, and so on in statement expressions that are used only in compound statements, such as

#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 macros defined by the above method will have no side effects because the __x and __y are redefined as two local variables. In standard c, the corresponding macro usually has side effects:

#define min (x, y) (() < (y)? ( X):(y))

The code min (++ia,++ib) will be expanded to

(++ia) < (++ib)? (++ia):(++ib)) the parameters of the incoming macro are incremented two Times.

This is a basic question that embedded programmers should know about 0x10.

4. typeof Key Words

The typeof (x) statement can get the type of x, so we can redefine the min_t that is mentioned in 3rd by using Typeof.

#define MIN (x, y)/

({ /

Const typeof (x) _x= (x);

Const typeof (y) _y= (y);

(void) (&_x==&_y);

_x<_y? _x: _y; })

We don't need to pass in a type like the third, because the type can be obtained by typeof (x).

Code (void) (&_x==&_y), which checks whether the types of _x and _y are Consistent.

5. Variable-parameter Macros

Standard C supports only variable-parameter functions, which means that the parameters of the function can be non-fixed

The prototype of the printf () function, for example, is

int printf (const char *format [, argument] ...)

In GNU c, a macro can also accept a variable number of arguments, such as

#define PR_DEBUG (fmt,arg ...) printk (fmt,# #arg)

here, Arg means that the rest of the arguments can be 0 or more, and that the comma between the parameters and the arguments constitutes the value of arg,

Replace ARG when the macro is expanded, for example

Pr_debug ("%s:%d", filename,line);

be extended to

PRINTK ("%s:%d", filename,line);

Using # #的原因是为了处理arg不代表任何参数的情况, This time, the preceding comma becomes superfluous.

With # #之后, the GNU C preprocessor discards the preceding comma so that the code

Pr_debug ("success!/n") will be correctly extended to PRINTK ("success!/n")

Rather than PRINTK ("success!/n",);

6. Marking Elements

Standard C requires that the initialization values of an array or struct must appear in a fixed order, and in GNU c, allowing initialization to occur in any order by specifying the index or struct member Name.

The method of specifying an array index is to add [index]=] before the value is initialized and, of course, You can also use [first ... Specifies a range in the form of last]=. For example, The following code defines an array and assigns all of its elements to a value of 0:

unsigned char data[max] ={[0...max-1]=0};

The following code initializes the struct body with the struct member Name:

struct File_operations demo_fops = {

owner:this_module,

llseek:demo_llseek,

read:demo_read,

write:demo_write,

ioctl:demo_ioctl,

Open:dem O_o pen,

release:demo_release,

};

however, Linux 2.6 is recommended to use standard c, as follows

struct File_operations demo_fops = {

. Owner = this_module,

. Llseek = demo_llseek,

. Read = demo_read,

. write = demo_write,

. IOCTL = demo_ioctl,

. open = dem O_o pen,

. Release = demo_release,

};

7. Name of current function

GUN c predefined two identifiers to save the current function name, __function__ Save the function in the source name,

__pretty_function__ Save the name with the language feature. In the C function, these two names are the Same.

void Example ()

{

printf ("this is function:%s", __function__);

}

The __function__ in the code means the string "example"

8. Special Attribute Declaration

GNU C allows for the Declaration of special properties of functions, variables, and types for manual code optimization and the method of customizing Code Checking. Specify a declared property, just add __attribute__ ((attribute) after Declaration)

Where attribute is a property description, separated by commas if there are multiple Attributes. GNU C supports more than 10 properties such as Noreturn format section aligned packed

The Noreturn property acts on a function, indicating that the function never Returns. This causes the compiler to optimize the code and eliminate unnecessary warning messages. For example

#define Attrib_noret __attribute__ ((NORETURN)) ....

Asmlinkage noret_type void Do_exit (long Error_code) attrib_noret;

The Format property can also be used for functions that represent the function printf scanf or Strftime-style parameters, and specifying the Format property allows the compiler to check the parameter types based on the format string. For example:

asmlinkage int PRINTK (const char * fmt,...) /

__attribute__ (format (printf,1,2));

The details can be seen http://blog.163.com/sunm_lin/blog/static/9192142200741533038695/

The unused property acts on functions and variables, indicating that the function or variable may not be used, and that the compiler generates warning Messages.

The aligned property specifies the alignment of the struct, variable, Union. The packed property acts on variables and types, representing compressed structures, using minimal memory.

struct EXAMPRL_STRUCT

{

Char a;

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 function

GNU C provides a large number of built-in functions, many of which are built-in versions of standard C library functions, such as memcpy, which are functionally equivalent to the corresponding C library functions, which are not discussed in this article, and the names of other built-in functions usually begin 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 parameter level specifies the number of frames to search on the stack, 0 represents the return address of the current function, 1 represents 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 compile-time constant, and if the value of the parameter EXP is a const, the function returns 1, otherwise returns 0. For example:

++++ Include/asm-i386/bitops.h

249: #define Test_bit (nr,addr)/

: (__builtin_constant_p (nr)?

251:constant_test_bit (nr), (addr)):/

252:variable_test_bit ((nr), (addr)))

Many calculations or operations have a more optimal implementation when the parameters are constants, and in GNU C the above method can be used to compile only constant versions or very few versions, depending on whether the parameters are constants, and to compile the most optimized code when the parameters are Constants.

* __builtin_expect (EXP, C)

The built-in function __builtin_expect is used to provide the compiler with branch prediction information whose return value is the value of the integer expression EXP, and the value of C must be a compile-time constant. For example:

++++ Include/linux/compiler.h

: #define LIKELY (x) __builtin_expect ((x), 1)

: #define Unlikely (x) __builtin_expect ((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 EXP is expected to be C, and the compiler can properly rearrange the order of the block of statements according to this information, so that the program has higher execution efficiency in the expected case. The above example indicates that the interrupt context is rarely occurring, and that the target code for the 第565-566 line may be placed farther away to ensure that the target code that is executed frequently is more compact.

GNU c, ANSI c, standard c, standard C + + differences and links

Related Article

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.