Chapter 3 C language statement analysis

Source: Internet
Author: User

  • 3.1 Only the compiler will like the syntax

The Declaration syntax (Declaration model) of C language is obscure and easily becomes a barrier for programmers. It is precisely because of the clumsy behavior in the combination type that C language looks very complicated.

This is because" Type model)This concept is still unfamiliar with the programming language theory at that time. BCPL (the predecessor of C language) has almost no type. It uses binary as the only data type. One of the C language design philosophy: Object declaration formWith its UsageTry Similar. Int * p [3] is an int type pointer array. The usage is as follows:
         * p[     a0[] = {, ,  a1[] = {,  a2[] = {-, , , ,     p[] =] =] =( i = ; i < ; i++, p[]++ , *p[( i = ; i < ; i++, p[]++ , *p[( i = ; i < ; i++, p[]++ , *p[
The execution result is as follows: The biggest problem with the C language statement is that you cannot habitually Read a statement from left to right. About const The Pointer Points to an object that is read-only.Two types of declarations:
      i [] = {,       * grape ;           * orange ;        grape = &= & , *grape );      grape++;                        printf(  , *grape );  

 

The pointer itself is read-only.Statement:
     i =      *  apple = &*apple += ;   , *apple );  

The pointer itself is read-only.
And pointer The object to be pointed to is also read-only.Two types of declarations:
      i =       *  grape_jam_1 = &  *  grape_jam_2 = &+= ;       *grape_jam_1 += ;  
The above code produces the following error: Summary rule: In the pointer declaration, As long as the const is "next to" pointer variable, it indicates that the pointer itself is read-only. Otherwise, the object pointed to by the pointer is read-only..
  • 3.2 How statements are made
Declarator)Is the core of all declarations. Declarative: identifier and any pointer (*), function brackets (), and array subscript ([]) combined with it.   About structure (struct)  Structure is a data structure that combines some data items. Its syntax is:
{Content ...};
After the structure is defined, you can use some variable names to indicate that the type of these variables is the structure:
{Content...} plum, pomegranate, pear;
After the struct keyword, you can add an optional "structure tag", which can be used as a short form of the structure (structfruit_tag) to define variables in the future:
Fruit_tag {content...} plum, pomegranate, pear;
Therefore, the common structure form is as follows:
The author suggests that you do not mix the structure declaration with the definition of variables, but separate them to make the code easier to read. The declaration of variables should be separated from the declaration of types. Two parameter passing problems related to the structure: the second point is the code below: 
 a[];    s_tag twofold( ( j = ; j < ; j++*= ( i = ; i < ; i++= == lemon;      }
The structure contains a pointer to the structure. This method is often used in lists, trees, and so on. Union)  Each member in the structure is stored in sequence, and in the Union, all Members start to store at 0 offset. At a certain time point, only one member is actually stored in this address. Association is also called a variant record ". Common form of association:
Consortium generally exists as a part of a large structure. To save storage space, because some data items 
It is impossible to appear at the same time. Union can also interpret the same data as two different things, rather than interpreting two different data as the same thing. The author finally lists the data (15 million lines of OS source code irrelevant to the machine), indicating that the number of occurrences of the structure is about one hundred times that of the Union. 
Enum) 
 Enumeration uses a simple way to combine a string of names with a string 
Integer valueTogether. For C, there are few things that can only be done by enumeration, but cannot be solved by # define. The first version of enumeration: 
By default, the integer value starts from 0. If a value is assigned to an identifier in the list, the subsequent identifier is 1 in turn. Enumeration is different from # define: # The name defined by define is generally discarded during compilation, while the enumeration name is usually visible in the debugger. 
  • 3.3 Priority rules
To understand a declaration, you must understand its priority rules. Understanding the priority rules declared by C language: A declares that the priority is read from its name and then read in sequence according to its priority. B priority order: B. 1. Part B enclosed in parentheses in the statement. 2 suffix OPERATOR: () represents a function [] represents an array B. 3 prefix OPERATOR: * indicates "pointing... pointer ". C If the const and volatile keywords follow the type specifier (int long float), it acts on the type specifier. In other cases, it acts on the pointer asterisk next to it.
  • 3.5 about typedef
In some ways, typedef is similar to replacing macro text-it does not introduce a new type, but takes a new name for the existing type. In general, typedef is used to represent pointers to other things in a concise manner. Do not include several declarers in a typedef; Do not embed typedef in the middle of the Declaration. In the same code block, the name introduced by typedef cannot have the same name as other identifiers.
  • 3.6 differences between typedef and define
There are two differences between typedef and macro text replacement: 1. You can use another type specifier to extend the macro type name, but you cannot do this for the type name defined by typedef;
   peach   int
2. In the declaration statement for several consecutive variables, the type defined by typedef can ensure that all the variables in the Declaration are of the same type, while the type defined by # define cannot be guaranteed.
 d_string char* *

 

  • 3.7 namespace in C Language
Namespace in C language: In the same namespace, any name must be unique, but the same name may exist in different namespace.
typedef  fruit mandarin;  
In this case, the structure tag has the same name as the structure alias of typedef. The author suggests that when you have two different things, A better principle is to use different names to name them. This reduces the risk of obfuscation (which is always an important criterion for software ). For example, you can add a "_ tag" suffix after the structure tag.

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.