Current location - Training Enrollment Network - Mathematics courses - What's the difference between c89 and c99? What compiler supports c89?
What's the difference between c89 and c99? What compiler supports c89?
The difference between C99 and C89:

1, increase the limit pointer

C99 adds a restricted type modifier for pointers. This is the only way to initially access the object pointed to by the pointer, so it only

This object can only be accessed with the help of restricted pointer expressions. The restriction pointer is mainly used as a function parameter or points to the malloc () function.

The number of memory variables allocated. The restrict data type does not change the semantics of the program.

If a function defines two restriction pointer parameters, the compiler assumes that they point to two different objects memcpy ().

Function is a typical application example of limiting pointer. The prototype of memcpy () function in C89 is as follows:

Code:

void *memcpy (void *s 1,const void *s2,size _ t size); If the objects pointed by s 1 and s2 overlap,

Its operation is undefined. Memcpy () function can only be used for non-overlapping objects. The prototype of memcpy () function in C99 is as follows:

Code:

void * memcpy(void * restrict s 1,const void *restrict s2,size _ t size); By using restrict

Modifying parameters s 1 and s2 can ensure that they point to different objects in the prototype.

2. Embedded keywords

In addition to maintaining structured and functional definitions, inline functions also enable programmers to write efficient code. Each call to this function is associated with the

Returning will consume a lot of system resources, especially when the function call appears in repeated loop statements. Generally speaking, when sending,

When making a function call, you need to put the independent variables on the stack and save various register memories. When the function returns, the contents of the register need to be restored. If this

This function is extended online in the code. When the code is executed, these save and restore operations will happen again and the function call will be executed.

The line speed will also be greatly accelerated. Online extension of functions will produce long codes, so only those codes that have a significant impact on application performance should be inlined.

Functions and functions with shorter lengths.

3. Add a new data type

_Bool

The value is 0 or 1. In C99 language, it is used to define the title folder of bool, true and false macros, so this project

Members can write applications that are compatible with C and C++. When writing a new application, you should use the

Bool macro in & ltstdbool.h & gt header file.

_ complex number and _ imaginary number

The complex number types defined in C99 standard are as follows: float _ Complexfloat _ Imaginary double _ complex;

Double _ deficiency; Long double _ complex; Long double _ deficiency.

< complex.h > Complex and fictional macros are defined in the header file and extended to _Complex and _Imaginary.

Therefore, when writing a new application, you should use the.

Long integer

Long long int(-(2e63- 1) to 2e63- 1) and unsigned long long int(0-2e64) are introduced into C99 standard.

- 1)。 Long long int can support an integer length of 64 bits.

4. Enhancement of the array

Variable length array

In C99, when a programmer declares an array, the dimension of the array can be determined by any valid integer expression, including only at runtime.

This kind of array is called variable-length array, but only local arrays can be variable-length.

The dimension of variable-length array is constant during its lifetime, that is, variable-length array is not dynamic. What can be changed is the size of the array.

You can use * to define a variable-length array with an indefinite length.

Type modifiers in array declarations

In C99, if you need to use an array as a function parameter, you can use the static keyword in square brackets in the array declaration.

When told to the compiler, the array pointed by the parameter will contain at least the specified number of elements. You can also enclose it in square brackets in the array declaration.

Restrict, volatile, const keywords, but only for function parameters. If you use restrict, pointers are the only way to access objects initially.

Diameter. If const is used, the pointer always points to the same array. There is no point in using volatile.

5. Single-line comments

A single-line comment tag "//"is introduced, which can be used like C++.

6, scattered code and statements

7. Modification of pretreatment program

I. Parameter list

Macros can be accepted by ellipsis (...) in the macro definition. The internal preprocessing identifier __VA_ARGS__ determines the location of the parameter.

This place was replaced. Example: # Define my sum (...) sum (_ _ va _ args _ _) statement MySum(k, m, n);

Will be converted into: sum(k, m, n); Parameters can also contain parameters. Example: #define compare(compf, ...)

Compf(__VA_ARGS__) where compare(strcmp, "small", "big"); Will be replaced by:

Strcmp ("small", "big");

B._Pragma operator

C99 introduces another method of defining compilation instructions in programs: _Pragma operator. The format is as follows:

_Pragma ("instruction")

Instruction is a compilation instruction that needs to be fully prepared. The _Pragma operator allows compilation instructions to participate in macro substitution.

C, internal compilation instructions

If STDCFP_CONTRACT ON/OFF/DEFAULT is ON, floating-point expressions are considered to be processed independently of hardware.

Unit. The default value is the defined tool.

STDCFEVN_ACCESS ON/OFF/DEFAULT tells the compiler that it can access the floating-point environment. The default value is the defined tool.

STD CCX _ Limited _ Range ON/Off/Default If this value is ON, it is equivalent to telling the compiler that some programs contain.

Formulas with complex numbers are more reliable. The default is off.

D. new internal macro

__STDC_HOSTED__ 1 (if the operating system exists).

_ _ stdc _ version _ _199991l or higher. Represents the version of C.

__STDC_IEC_599__ 1 If IEC 60559 floating-point operation is supported.

__STDC_IEC_599_COMPLEX__ If IEC 60599 complex operation is supported, 1.

__STDC_ISO_ 10646__ Compiler support, which is used to explain the year-month format of ISO/IEC 10646 standard:

yyymmmL

8. Variable declaration in the 8.for statement

In C99, programmers can define one or more variables in the initialization part of a for statement, and the scope of these variables is limited to this for language.

In the cycle of sentence control. For example:

Code:

for(int I = 0; I< 10; i++){

//Do something ...

}

9. Compound assignment

In C99, in compound assignment, you can specify an array, structure or union expression of object type. When using compound assignment, it should be in parentheses.

Type specified in, followed by an initialization list enclosed in braces. If the type is array, the size of the array cannot be specified. The goal of construction is

Unnamed.

For example: double * FP = (double []) {1.1,2.2, 3.3};

This statement is used to create a pointer fp to a double, which points to the first element of this three-element array. In the file

The compound assignment established in the domain is only valid during the whole life cycle of the program. The compound assignment established in the module is a local object. After exiting the module,

No longer exists.

10, a flexible array structure member.

In C99, the last element in the structure is allowed to be an array of unknown size, which is called a flexible array member, but the flexible number in the structure.

There must be at least one other member before a group member. Flexible array members allow structures to contain arrays of variable sizes. Sizeof returned this

This structure size does not include the memory of the flexible array. Structures containing flexible array members use malloc () function to dynamically allocate memory, and

The allocated memory should be larger than the size of the structure to adapt to the expected size of the flexible array.

1 1, the specified initializer.

In C99, this feature is very useful for programmers who often use sparse arrays. The specified initializer is usually used in two ways: for arrays,

And structure and association. Format used for arrays: [index] = vol; Where index represents the subscript of the array and vol represents the array.

The initialization value of the element.

For example: int x [10] = {[0] = 10, [5] = 30}; Only x[0] and x[5] are initialized. Used of structures or associations.

The format is as follows:

Member name (member name)

When initializing a structure, a simple method is allowed to initialize the specified members in the structure.

For example: struct example{ int k, m, n; } object = {m = 10,n = 200 };

Where k is uninitialized. There is no limit to the initialization order of structure members.

Enhancement of 12, printf () and scanf () function series

The series of printf () and scanf () functions in C99 introduced data types dealing with long long int and unsigned long long int.

The characteristics of. The format modifier of long long int type is ll. In the printf () and scanf () functions, ll applies to d, i, o, u and x.

Format specifier. In addition, C99 also introduced hh modifier. Hh is used to specify char when using D, I, O, U and X format descriptors.

Type parameter. Both ll and hh modifiers can be used for the n specifier.

When format modifiers a and a are used in the printf () function, the result will output hexadecimal floating-point numbers. The format is as follows: [-]0xh, hhhhp.

+d When using format modifiers, x and p must be capitalized. One and one format modifier can also be used in the scanf () function for reading.

Floating point number. When calling the printf () function, it is allowed to add the l modifier before the %f specifier, that is, %lf, but it has no effect.

13, C99 new library

Standard header file in C89

& ltassert.h & gt define macro assert ().

& ltctype.h & gt character processing

& lt error number & gt error report

& ltfloat.h & gt defines floating-point tasks related to implementation.

& lt restrictions.> define various limits related to implementation.

& ltlocale.h & gt supports the function setlocale ().

Various definitions used in & ltmath.h & gt mathematical function library

& ltsetjmp.h & gt supports non-local jumps.

< signal.h > define the signal value.

& ltstdarg.h & gt supports variable-length parameter lists.

& ltstddef.h & gt define common constants.

& ltstdio.h & gt supports file input and output.

& ltstdlib.h & gt Other statements.

& ltstring.h & gt supports the string function.

& lttime.h & gt supports the system time function.

C99 new header files and libraries

& ltcomplex.h & gt supports complex number algorithm.

& ltfenv.h & gt allows access to floating-point status flags and other aspects of the floating-point environment.

H> defines a set of standard portable integer types. Functions that handle integers with maximum width are also supported.

& ltiso646.h & gt was first introduced in the first revision of 1995 to define macros corresponding to various operators.

& ltstdbool.h & gt supports Boolean data types. Define the macro bool to be compatible with C++

& ltstdint.h & gt defines a set of standard portable integer types. This file is contained in the.

& lttgmath.h & gt defines the general types of floating-point macros.

& ltwchar.h & gt It was originally introduced in the first revision of 1995 to support multi-byte and wide-byte functions.

& ltwctype.h & gt was first introduced in the first revision of 1995 to support multi-byte and wide-byte classification.

14, __func__ predefined identifier

Used to represent the function name stored in __func__, similar to string assignment.

15. Changes in other characteristics

Relax conversion restrictions

Restrict C89 standard C99 standard

The nesting level of the data block is 15 127.

Nesting level of conditional statements 8 63

The number of valid characters in the internal identifier is 3 1 63.

Number of valid characters in external identifier 6 3 1

Number of members in the structure or union 127 1023

The number of parameters in the function call is 3 1 127.

Implicit int rules are no longer supported.

Deleted implicit function declaration.

Constraint of return value

In C99, non-null type functions must use a return statement with a return value.

Extended integer type

Extended type meaning

The integer length of int 16_t is exactly 16 bits.

The integer length of int _ lease 16 _ t is at least16 bits.

Int_fast32_t is the most stable integer type with a length of at least 32 bits.

Intmax_t maximum integer type

Uintmax_t largest unsigned integer type

Improvement of integer lifting rule

In C89, the value of char, short int or int type in the expression can be promoted to int type or unsigned int type.

In C99, every integer type has a level. For example, the level of long long int is higher than that of int, and the level of int is higher than that of char. In an expression, any integer type with a level lower than int or unsigned int can be replaced with int or unsigned int.