Key Points of C Language

Source: Internet
Author: User

One keyword:

1. static:

The first role is to modify the variable. Variables are classified into local and global variables, but they all have static memory areas. Because static modified variables always exist in the static memory zone, the value of the static variable will not be destroyed even if the function stops running, and the function can still use this value the next time it is used.

Static int j;

Void fun1 (void)

{

Static int I = 0;

I ++;

}

Void fun2 (void)

{

J = 0;

J ++;

}

Int main ()

{

For (k = 0; k <10; k ++)

{

Fun1 ();

Fun2 ();

}

Return 0;

The second role is to modify the function. Add static before the function to make the function a static function. However, the meaning of "static" here is not the storage method, but the scope of the function is limited to this file (so it is also called an internal function ). The advantage of using internal functions is that when different people write different functions, you don't have to worry about your own defined functions and whether they will have the same name as the functions in other files.

2. sizeof

Sizeof is a keyword, not a function

3. signed and unsigned keywords

4. Compare the float variable with the "zero value"

5. if else statement

Handle normal conditions before handling exceptions.

6. Loop statement Style

In multiple cycles, if possible, put the longest loop in the innermost layer and the shortest loop.

In the outermost layer, the number of times the CPU switches across the cycle layer is reduced.

7. void

Void cannot represent a real variable.

Void a; // Error

Function (void a); // Error

8. const

The read-only variables modified by const must be initialized at the same time as they are defined. The Compiler usually saves them in the symbol table instead of allocating storage space for common const Read-Only variables, this makes it a value during compilation, without the storage and read memory operations, making it highly efficient.

A) modify the pointer

Const int * p; // p variable. The object pointed to by p cannot be changed.

Int const * p; // p variable. The object pointed to by p cannot be changed.

Int * const p; // p is unchangeable, and the object pointed to by p is variable.

Const int * const p; // the pointer p and p point to the object are not changeable

B) modify function parameters

The const modifier can also modify the function parameters. This parameter is used when the function does not want this parameter value to be accidentally changed.

. For example:

Void Fun (const int I );

9. extern

Extern is equivalent to these characteristics that distinguish them from the Chinese. Extern can be placed before a variable or function

The variables or functions are defined in other files.

10. struct

The size of the empty struct is located at 1 byte. Struct members are public by default, while class Members are private.

11. union

Union maintains enough space to place one of multiple data members, instead of configuring space for each data member. All data members in union share one space, only one data member can be stored at a time. All data members have the same starting address. Example:

Union StateMachine

{

Char character;

Int number;

Char * str;

Double exp;

};

A union can only be configured with a large enough space to accommodate the maximum length of data members.

 

12 typedef

 

 

Binary preprocessing

1. # if

# The general meaning of if is that if the constant expression after # if is true, the code between it and # endif will be compiled; otherwise, the code will be skipped. Command # endif identifies the end of a # if block. # The functions of the else command are somewhat like the else in C, # The else creates another option (in the case of # if failure ). # The elif command has the same meaning as the else if command. It forms an if else-if step-by-step statement and supports multiple compilation options.

 

2. # define

# Define will be replaced as it is, but it is actually very easy to fix it, just don't mean it.

Example:

Define a macro function and calculate the square of x:

# Define SQR (x) x * x

Try: assume that the value of x is 10, and the value of SQR (x) is changed to 10*10 after it is replaced. No problem.

Try again: assume that the value of x is a 10 + 1 expression, and SQR (x) is replaced with 10 + 1*10 + 1. Problem

This is not what I want. What should I do? Isn't it covered in parentheses?

# Define SQR (x) * (x ))

Three pointers and Arrays

Note !!! : The pointer offset value is the starting address plus the size of the pointer pointing to the object.

1. Differences between array name a as the left value and the right value

The array name cannot be used as the left value. Add 1 to the pointer to get the address of the next element, instead of adding 1 directly to the original address value. Therefore, the movement of a pointer of type T is measured in sizeof (T. Main ()

{

Int a [5] = {1, 2, 3, 4, 5 };

Int * ptr = (int *) (& a + 1 );

Printf ("% d, % d", * (a + 1), * (ptr-1 ));

}

& A + 1: Get the first address of array a. The value of this address is added with the value of sizeof (a), that is, & a + 5 * sizeof (int ).

Is the first address of the next array. Obviously, the current pointer has crossed the limit of the array. (Int *) (& a + 1): The address calculated in the previous step is forcibly converted to the int * type and assigned to ptr. * (A + 1): the value of a and a is the same, but the meaning is different. a is the first address of the array's first element, that is, the first address of a [0, & a is the first address of the array, and a + 1 is the first address of the next element of the array, that is, the first address of a [1]. & a + 1 is the first address of the next array. So output 2 * (ptr-1): Because ptr is pointing to a [5], and ptr is int * type, so * (ptr-1) is pointing to a [4], output 5.

 

2. Relationship between arrays and pointers

File 1 is defined as follows:

Char a [100];

The declaration in file 2 is as follows:

Extern char *;

Here, array a is defined in file 1, and it is declared as a pointer in file 2. Is there any problem? Isn't it always said that arrays are similar to pointers or even can be used in general? However, unfortunately, this is wrong. Through the above analysis, we can also understand that arrays are arrays, pointers are pointers, and they are completely different! There is no relationship between them, but they often get confused by wearing similar clothes. The following is an analysis of this problem:

 

The difference between definition and Declaration is that the allocated memory is defined, but the Declaration does not. The definition can only appear once, but the Declaration can appear multiple times. Here, extern tells the compiler that the name a has been defined in other files. The following code uses the name a as defined by other files. Looking back at the previous discussions on the left and right values, we know that if the compiler needs an address (which may also require an offset) to execute some operation, it can directly read or write the memory on the address through the unlock action (using the "*" key), and does not need to find the location where the address is stored first. On the contrary, for pointers, you must first find the location where the address is stored, retrieve the address value, and then unlock the address (use the key ). For example:

This is why extern char a [] is equivalent to extern char a [100. Because this is only a declaration and does not allocate space, the compiler does not need to know how many elements the array has. Both Declarations tell the compiler that a is an array defined in other files. a also represents the first address of the first element of array a, that is, the starting address of the memory. The address of any element in the array Mainland can be calculated by knowing this address.

However, when you declare it as extern char * a, the compiler naturally considers a as a pointer variable, which occupies 4 bytes in a 32-bit system. These four bytes store an address, which stores character-type data. Although in file 1, the compiler knows that a is an array, but in file 2, the compiler does not know this. Most compilers compile by file separately, and the compiler only processes according to the types declared in this file. Therefore, although the actual size of a is 100 bytes, in file 2, the compiler considers that a only occupies 4 bytes.

As we said, the compiler will treat any data in the pointer variable as an address. Therefore, if you need

To access the data of these character types, we must first extract the saved address from the pointer variable. For example:

 

Obviously, according to the above analysis, an error occurs when we declare the array defined in file 1 as a pointer in file 2. Similarly, if file 1 is defined as a pointer and declared as an array in the file, an error occurs:

File 1

Char * p = "abcdefg ";

File 2

Extern char p [];

In File 1, the compiler allocates four bytes and names them p. At the same time, p Stores the first character address of the String constant "abcdefg. This string constant is stored in the static area of the memory and its content cannot be changed. In file 2, the compiler considers p as an array with a size of 4 bytes. The array stores char-type data. The process of using p in file 2 is as follows:

 

3. array parameters and pointer Parameters

An array cannot be passed to a function. In C language, when a one-dimensional array is used as a function parameter, the compiler always resolves it as a pointer pointing to the first address of its first element.

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.