Comparison between c89 and c99 standards

Source: Internet
Author: User

GCC supports c99 and is enabled using the -- STD = c99 command line parameter. Example: GCC -- STD = c99 test. c

 
1. Add a restrict pointer.

C99 adds a restrict type modifier for pointers, which is the only way to initially access the objects indicated by pointers. Therefore, only the restrict pointer expression can be used to access objects. The restrict pointer is mainly used as a function variable, or points to the memory variable allocated by the malloc () function. The restrict data type does not change the semantics of the program.
If a function defines two restrict pointers, the compiler assumes that they point to two different objects. The memcpy () function is a typical example of a restrict pointer. The memcpy () function prototype in c89 is as follows:

Code:
  void *memcpy (void *s1, const void *s2, size_t size);

If S1 and S2 point to an object that overlaps, the operation is undefined. Memcpy () functions can only be used for non-overlapping objects.. The memcpy () function prototype in c99 is as follows: code:

    void *memcpy(void *restrict s1, const void *restrict s2,size_t size);

 By modifying S1 and S2 variables using restrict, you can ensure that they point to different objects in the prototype.

2. inline (Inline) KEYWORDS
In addition to maintaining a structured and functional definition method, inline functions allow programmers to write highly efficient code. each call and return of a function consumes a considerable amount of system resources, especially when a function call occurs in a loop statement with many repetitions. in general, when the swap function is called, the variable needs to be written into the stack, and the memory of various registers needs to be saved. when the function returns, the register content needs to be restored. If the function is online extended in the Code, when the code is executed, these save and restore operations will happen again, and the execution speed of function calls will be greatly accelerated. Online expansion of functions produces long code.So that only inline functions with a significant impact on the application's procedural nature and short-lived functions can be used.

3. New Data Types
_ Bool
The value is 0 or 1. The header folder <stdbool. h> is added to c99 to define bool, true, and false macros, so that programmers can write applications compatible with C and C ++ at the same time. When writing new applications, you should use
<Stdbool. h> the bool macro in the header file.

_ Complex and _ imaginary
The plural types defined in the c99 standard are as follows: float_complex; float_imaginary; double_complex; double_imaginary; long double_complex; long double_imaginary.
<Complex. h> the header file defines complex and imaginary macros and extends them to _ complex and _ imaginary. Therefore, you should use <stdbool when writing new applications. h> the complex and imaginary Macros in the header file.

Long long int
The c99 standard introduces long int (-(2e63-1) to 2e63-1) and unsigned long int (0-2e64-1 ). Long long int supports 64-bit integer length.

4. array Enhancement
  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 an expression that determines its value only at runtime. Such an array is called a variable length array,However, only partial Arrays can become longer.
The dimensions of the Variable Length array remain unchanged during the lifetime of the array. That is to say, the variable length array is not dynamic. only the size of the array can be changed. you can use * to define a variable length array with uncertain length.
  
Type modifier in array declaration
In c99, if you need to use an array as the function variable, you can use the static keyword in the square brackets declared by the array, which is equivalent to telling the Compilation Program, the array to which the variable points will contain at least the specified number of elements. You can also use the restrict, volatile, and const keywords in square brackets declared by the array, but only for function variable. If you use restrict, the pointer is the only way to initially access this object. If you use const, the pointer always points to the same array. Using volatile makes no sense.
5. Single line comment
The single line comment mark "//" is introduced, which can be used like C ++.

6. Scattered code and Declaration

7. Modify the Preprocessing Program
A. Variable list
Macros can contain variable elements, which are expressed by ellipsis (...) in macro definition. The internal preprocessing identifier _ va_args _ determines where the variable will be replaced. Example: # define mysum (...) sum (_ va_args _) Statement mysum (K, M, N );
Will be converted to: sum (K, m, n); the variable can also contain the variable. Example: # define compare (compf ,...) compf (_ va_args _) Where compare (strcmp, "small", "large"); will be replaced with: strcmp ("small", "large ");
B. _ Pragma Operator
C99 introduces another method for defining compilation commands in a program: _ Pragma operator. The format is as follows:
_ Pragma ("directive ")
In this example, direve VE is the compilation command to be full. The _ Pragma operator allows compilation commands to be used in macro replacement.
C. Internal compilation commands
If stdcfp_contract On/Off/default is on, the floating point expression is treated as an independent unit based on hardware. The default value is a defined tool.
Stdcfevn_access On/Off/default indicates that the compiler can access the floating point environment. The default value is a defined tool.
Stdc cx_limited_range On/Off/default if the value is on, it is equivalent to telling the compile program that some formulas containing plural numbers are reliable. The default value is off.
D. New Internal macros
_ Stdc_hosted _ If the operating system exists, it is 1
_ Stdc_version _ 5o91l or higher. Represents the C version
_ Stdc_iec_599 _ If the IEC 60559 floating point operation is supported, the value is 1.
_ Stdc_iec_599_complex _ If the IEC 60599 complex operation is supported, the value is 1.
_ Stdc_iso_000046 _ supported by the compiler, used to describe the ISO/IEC 10646 Standard year and month format: yyymmml

9. compound assignment
In c99, you can specify an array, structure, or union expression of the object type in composite assignment. When a compound value is used, the type should be specified in the arc, followed by the initialization list enclosed by curly brackets. If the type is array, the array size cannot be specified. The built object is not named.
Example: Double * fp = (double []) {1.1, 2.2, 3.3 };
This statement is used to create a pointer FP pointing to double, and the Pointer Points to the first element of the three element array. Composite assignments created in the file domain are valid only for the entire lifetime of the program. The composite assignment created in the module is a local object and does not exist after exiting the module.

  10. Flexible array structure member
In c99, the last element in the structure can be an array of unknown size, which is called a flexible array member, but the flexible array member in the structure must be at least one other member. Flexible array Members allow the structure to contain an array of variable sizes. The size of the structure returned by sizeof does not include the memory of the flexible array. The structure containing flexible array members uses the 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.

  11. Specified initialization character
In c99, this feature is very useful for programmers who frequently use sparse arrays. The specified initialization character can be used as an array, as well as a structure and a union. Array format: [Index] = vol. index indicates the subscript of the array, and vol indicates the initialization value of the array element.
For example, int X [10] = {[0] = 10, [5] = 30}. Only X [0] and X [5] are initialized. the format used for structure or union is as follows:
Member-Name (member name)
You can use a simple method to initialize a specified member of a structure when initializing the structure.
Example: struct example {int K, M, N;} object = {M = 10, n = 200 };
K is not initialized. There is no limit on the order in which the structure members are initialized.

12. Enhancements to the printf () and scanf () function series
The printf () and scanf () functions in c99 introduce the features for processing long int and unsigned long int data types. The long int Type format modifier is ll. In the printf () and scanf () functions, ll applies to the format specifiers D, I, O, U, and X. In addition, the HH modifier is introduced in c99. When the format of D, I, O, U and X is used, HH is used to specify the char variable. Both the ll and HH modifiers can be used as the N specifiers.
When the format modifiers A and A are used in the printf () function, the result will output a hexadecimal floating point number. The format is as follows: [-] 0xh, hhhhp + D when using the format modifier, X and P must be in uppercase. The format modifiers A and A can also be used in the scanf () function to read floating point numbers. When calling the printf () function, you can add the L modifier (% lf) before the % F specifier, but it does not work.

13. database Added by c99
  
Standard header file 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 list
<Stddef. h> define common Constants
<Stdio. h> supports file input and output.
<Stdlib. h> other declarations
<String. h> support for string functions
<Time. h> support for system time functions

Header files and libraries added by c99
<Complex. h> supports the plural algorithm.
<Fenv. h> provides access to the floating-point status mark and other aspects of the floating-point environment.
<Inttypes. h> defines a set of standard and portable 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 the boolean data type. Define macro bool to be compatible with C ++
<Stdint. h> defines a set of standard and portable integer types. This file is 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.

14. _ FUNC _ predefined identifier
Specifies the name of the function stored in _ FUNC _, which is similar to the value assignment of a string.

15. Changes to other features

Relaxed conversion restrictions
 
C89 standard c99 Standard
Data Block nesting layer 15 127
Number of nested layers of conditional statements 8 63
Number of valid characters in the internal identifier 31 63
The number of valid characters in the external identifier is 6 31.
127 Members in a structure or union
Number of parameters in function call 31 127

Implicit int rules are no longer supported

Removed the implicit function declaration.

Constraints on return values
In c99, non-null functions must use return statements with return values.

Extended Integer type
Extended type description
Int16_t integer length is precise 16 bits
Int_least16_t integer length is at least 16 bits
Int_fast32_t is the most stable Integer type, with a length of at least 32 characters
Intmax_t maximum integer type
Uintmax_t maximum unsigned integer type

Improvement on Integer type escalation rules
In c89, the expression type is Char, and the value of short Int or Int can be upgraded to int or unsigned Int.
In c99, each integer type has a level. for example, the level of 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 lower than Int or unsigned int can be replaced with Int or unsigned Int.

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.