Development of C language standards
C Language Development History is roughly divided into four stages: Old Style C, c89, c99 and c11.
C89 is the earliest C language specification. It was proposed in 1989 that the ANSI version was first released by ANSI (American National Standards Institute) in 1990, later, it was accepted as the ISO international standard (ISO/iec9899: 1990), which is also known as C90. The most typical C language textbook [K & R] is based on this version, c89 is currently the most widely used C language standard. Most compilers fully support c89. c99 (ISO/iec9899: 1999) was launched in 1999 and has added many new features, however, it is still not widely supported. For a long time after c99 was launched, GCC did not fully implement all the features of c99. In December 8, 2011, ISO released the new C language standard C11, formerly known as c1x, officially named ISO/IEC 9899: 2011.
URL: http://www.cnblogs.com/archimedes/p/c99-new-feature.html.
Now we will introduce the new features of c99 compared with c89 or ansi c:
1. Plural (complex)
complex.hIt is the header file in the C standard function library and provides the macro definition and function declaration required by the plural arithmetic.
#define complex _Complex#define _Complex_I ((const float _Complex)__I__)#define I _Complex_I
C99 specifies the keyword_Complex. Therefore, there are three plural types:
double _Complex
float _Complex
long double _Complex
The order is not mandatory. For example, float _ complex can also be written as _ complex float._Complex_IExtended to typeconst float _ComplexThe constant value of, whose value is the unit of virtual number. C99 RegulationscomplexExtended as a macro_Complex. But C ++ is not defined.complexMacro. GCC only supports complex type and does not support imaginary type. Therefore, macrosIExtended_Complex_I.
<Complex. h> it also contains a number of mathematical functions that support the complex numbers (C is ):
1. ccos, Csin, ctan, Cacos, CASIN, Catan: trigonometric functions in the complex field, corresponding to the F and l versions.
2. ccosh, csinh, ctanh, cacosh, casinh, catanh: hyperbolic functions in the complex field, with corresponding F and l versions.
3. cexp, clog, cabs, cpow, and csqrt: The exponent, logarithm, absolute value, and power function in the complex field. The F and l versions correspond to each other.
4. CARG, cimag, Creal, conj, and cproj: Obtain the projection of quadrant angle, virtual number, real number, A = X, B =-y, and Riann ball, there are corresponding F and l versions.
Code:
#include<stdio.h>#include<complex.h>int main() { double complex cmp = 1.3 + 2.3*I; printf("%f + %fi\n", creal(cmp), cimag(cmp)); return 0; } 2. Specify the initialization (designated initializers)
When initializing struct and array, you can specify a specific member name or array subscript to assign the initial value.
To specify the index value of an array, you can use '[Index] =' before the corresponding element value. The index must be a constant expression. For example:
int a[6] = { [4] = 29, [2] = 15 };
It is equivalent:
int a[6] = { 0, 0, 15, 0, 29, 0 };
You can also initialize it as follows:
int a[10] = { [1] = 1, [8 ... 9] = 10 };
In this way, only three elements a [1], a [8], and a [9] can be initialized. The values of other elements are 0, which is equivalent:
int a[10] = {0, 1, 0, 0, 0, 0, 0, 0, 10, 10};
For struct, you can use '. fieldname =' to specify the member name for initialization. For example:
struct point { int x, y; };
Next, initialize:
Struct point P = {. Y = yvalue,. x = xvalue}; // equivalent to struct point P = {xvalue, yvalue };
You can also use the colon:
struct point p = { y: yvalue, x: xvalue };
Of course, it can also be used in union:
union foo { int i; double d; };union foo f = { .d = 4 };3. Variable Length array (variable length arrays)
C99 allows you to define an array with a variable length (the length of this array can be determined at runtime)
FILE *concat_fopen (char *s1, char *s2, char *mode){ char str[strlen (s1) + strlen (s2) + 1]; strcpy (str, s1); strcat (str, s2); return fopen (str, mode);}GNU sample code
You can also use VLA in struct or union:
void foo (int n){ struct S { int x[n]; }; }
You can use alloca functions to implement similar functions, but not all alloca functions are implemented. from another perspective, VLA is better.
You can also use VLA as function parameters:
struct entrytester (int len, char data[len][len]){ /* ... */}
Of course, you can also post Len
Struct entrytester (INT Len; char data [Len] [Len], int Len) // note the semicolon {/*...*/}
Sample Code:
#include<stdio.h>void func(int n){ int vla[n]; printf("%d\n", sizeof(vla));}int main() { func(4); return 0; } 4. Single Row comment
GCC supports C ++-style comments starting with '//' until the end of a row. Many other C compilers that support c99 support this feature, but versions earlier than c99 may not.
5. Flexible array members)
See 《C language flexible arrayArticle
6. Long TYPE
C99 supports 64-bit integer types. Use long Int or unsigned long int to declare an integer constant as long int, and add'll 'to the end of the integer ', if the value is unsigned long int, 'ull 'is added'
7. inline functions
Inline in C/C ++ is used in the function declaration to indicate that the programmer requests the compiler to insert the function implementation in the called part of this function, instead of generating call code like a normal function (whether the application is valid depends on the compiler ). Generally, this method saves the overhead of calling a function. The disadvantage is that it may increase the size of the target code generated by the generation.
In fact, even if the inline function is not manually specified, the compiler usually selects some functions with a small amount of code but frequently used as the inline function, as one of the ways to optimize the performance.
Compared with parameterized macro, it has the following advantages:
- Parameter type check: the parameters used in the macro definition are only replaced by the macro definition without any type check.
- Return Value: return cannot be used in macro definition.
- Easy to debug
Sample Code:
static inline intinc (int *a){ return (*a)++;}8. bool type
I remember that I used to write # define true 1, # define false 0, or enum Boolean macros by myself. Now I can use the bool type of <stdbool. h>.
9. Compound literals)
In short, composite constants allow you to define an anonymous struct or array variable. For example:
struct foo {int a; char b[2];} structure;structure = ((struct foo) {x + y, ‘a‘, 0});
It is equivalent:
{ struct foo temp = {x + y, ‘a‘, 0}; structure = temp;}
You can also create an array:
char **foo = (char *[]) { "x", "y", "z" };
More instances:
Static struct Foo x = (struct Foo) {1, 'A', 'B'}; static int y [] = (INT []) {1, 2, 3 }; static int Z [] = (INT [3]) {1}; // equivalent to the following code: static struct Foo x = {1, 'A ', 'B'}; static int y [] = {1, 2, 3}; static int Z [] = {1, 0, 0 };10. For Loop Variable initialization (for loop intializers)
C99 introduces the For Loop Variable Initialization Method in C ++:
for(int i = 0; i < 10; ++i) { ...;}
In addition to writing convenience, the life cycle of cyclic variables is also minimized. This also eliminates the bad habit of bringing loop variables out of the loop to continue using them.
11. Added header files in c99.
Standard Header files in c89:
<Assert. h> define macro assert ()
<Ctype. h> Character Processing
<Errno. h> Error Report
<Float. h> define implementation-related floating-point duty
<Limits. h> defines implementation-related limits.
<Locale. h> the setlocale () function is supported ()
<Math. h> definitions used by the mathematical function library
<Setjmp. h> supports non-local jump
<Signal. h> define the signal value
<Stdarg. h> variable length parameter list
<Stddef. h> define common Constants
<Stdio. h> supports file input and output.
<Stdlib. h> other declarations
<String. h> string functions
<Time. h> support for system time functions
Header file added by c99
<Complex. h> support for complex algorithms
<Fenv. h> provides access to the floating-point status mark and other aspects of the floating-point environment.
<Inttypes. h> defines a standard, portable set of integer types. It also supports functions that process Integers of the maximum width.
<Iso646.h> first introduced in the first revision in 1995 to define macros corresponding to various operators
<Stdbool. h> supports boolean data types and defines macro bool to be compatible with C ++
<Stdint. h> defines a set of standard and portable integer types, which are included in <inttypes. h>.
<Tgmath. h> define general floating point macros
<Wchar. h> first introduced in the first revision in 1995 to support multibyte and wide byte Functions
<Wctype. h> it was introduced in the first revision in 1995 to support multibyte and wide byte classification functions.
Note: some new features have not been summarized, and will be added after fully understanding the practice.
References
Https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html#C-Extensions