C Expert Programming note (i) History of--c languages, K&r C and ANSI C

Source: Internet
Author: User

1. Many of the features of the C language are built for the convenience of the compiler designer. So the language features of C language are: array subscript starting from 0 instead of 1; The basic data type of C language corresponds directly to the underlying hardware; the Auto keyword is only meaningful for compiler designers who create symbol table portals; Array names in expressions can be treated as pointers; float is automatically extended to double (ANSI No more in c); Nested functions are not allowed (simplified compiler); The Register keyword, which provides clues to the compiler designer, throws the burden to the programmer.

2. Some features not implemented by the C compiler must be implemented in other ways (compiler-implemented functions such as subtraction, pointers, fetch addresses). In the C language, they are processed at run time, either in the application code or in the runtime library.

3. C language standard: ANSI C 1990 standard, ANSI C 1999 standard, up to now ANSI C 2011 Committee Draft (Committee Draft).

4. Several terms of the C language standard:

Non-portable code. Defined by the compiler, such as when the integer number is shifted to the right, do not extend the symbol bit. Indeterminate, such as the order in which the parameters are evaluated.

Bad code. Undefined, such as what action should be taken when a signed integer overflows. Constraints, such as the integer number of the% operator, must belong to an integral type.

Portable code. Strict adherence to the standard: Use only identified attributes, and do not break any limitations that are implemented by the compiler. does not produce any output that relies on undefined or undefined attributes defined by the compiler.

5. The most important new feature of ANSI C, compared to K&r C, is the "prototype", which increases the parameter type, rather than just the function name and return type, when making a forward declaration to a function. This allows the compiler to check for consistency between the arguments in the function call and the parameters in the function declaration at compile time. In K&r C, this check is deferred to the link, or simply not checked.

1 /*K&r C*/2 /*Statement*/3 Char*strcpy ();4 /*definition*/5 Char*strcpy (DST, SRC)6         Char*DST, *src;7 { ... }8 9 /*ANSI C*/Ten /*Statement*/ One Char* STRCPY (Char* DST,Const Char*src); A /*another form of declaration*/ - Char* STRCPY (Char*,Const Char*); - /*definition*/ the Char* STRCPY (Char*DST,Const Char*src) -{ ... }

6. The keyword const does not change the variable to a constant! Adding a const qualifier to a symbol simply means that the symbol cannot be assigned a value. That is, its value is read-only for this symbol, but it does not prevent the program from modifying the value by its internal method. The most useful thing about const is to use it to define the formal parameters of a function. (This is why it is not possible to initialize an array of integers using the const keyword decoration.) )

7.

1 Main ()2 {3     if(-1< (unsignedChar)1) {4printf"-1 is less than (unsigned char) 1");5}Else {6printf"-1 not less than (unsigned char) 1");7     }8}

The result is-1 less than (unsigned char) 1.

1 Main ()2 {3     if(-1< (unsignedint)1) {4printf"-1 is less than (unsigned int) 1");5}Else {6printf"-1 not less than (unsigned int) 1");7     }8}

The result is-1 is not less than (unsigned int) 1.

This is because ANSI C employs the value retention principle. When an integral type is upgraded, if int can fully represent all the values of the source type, the value of the source type is converted to int, otherwise it is converted to unsigned int.

8. Consider this procedure:

1 #defineTotal_elements (Array) (sizeof (array)/sizeof ((array) [0]))2 3 Main ()4 {5     intArr[] = {1,2,3,4,5,6,7,8,9};6     7     intD =-1, X;8 9     if(d <= total_elements (arr)-2) {Tenx = arr[d+1]; One     } A  -}

Does he have any bugs? Yes, if the value of an if expression is not true, X is not assigned a value. Why is it? Because of the total_elements (array) macro, the return type of sizeof is an unsigned number. So in the comparison, D is promoted to unsigned int, which becomes a huge number.

9. Consider this procedure:

1 foo (constChar * *p) {}23 main (intChar* * argv) 4 {5    foo (argv); 6 }

Using GCC compilation, you are prompted with a warning: [Warning] passing arg 1 of ' foo ' from incompatible pointer type.

1 foo (constChar *p) {}23 main (intChar* * argv) 4 {5     foo (*argv); 6 }

And this program does not prompt for warnings.

We know that during a function call, the argument is passed to the formal parameter, which is essentially the value of the argument given to the parameter.

In the ANSI C standard, the following constraints are proposed:

To make the above assignment legal, one of the following conditions must be met:

Two operands are pointers to compatible types that have qualifiers or no qualifiers, and the type that the left pointer points to must have all qualifiers for the type pointed to by the right hand pointer.

Later in the program, the left pointer points to the const char, and the right pointer points to char. According to qualifiers, the left side is compatible to the right, so the constraints are met.

In the previous procedure, the left pointer points to the const char*, and the right pointer points to *char. The left and right are two different pointer types.

In the ANSI 2011 standard Draft 6.2.5 section 29 (43 pages), we see that the Const float* and float* are incompatible. FLOAT * Const and float* are compatible.

So we can know that char * const and char * are compatible. Then write the following procedure:

1 foo (charconst * p) {}23 main (intChar* * argv) 4 {5    foo (argv); 6 }

By compiling, no warning.

C Expert Programming note (i) History of--c languages, K&r C and ANSI C

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.