C language Review

Source: Internet
Author: User
ArticleDirectory
    • 1. digit representation
    • 2. Static, extern, auto, register, volatiate
    • 3 federated union
    • 4 struct and bitfield
1. digit representation

Integer:

Floating point:

2. Static, extern, auto, register, volatiate2.1 Variables

Storage Method:

Static storage

Dynamic Storage

Stack  
Heap  
Static Data Zone Data Segment. Data
  Data Segment not initialized. BSS
ProgramSegment . Text

 

The Data Segment and the uninitialized data segment form a static storage area.

Heap and stack constitute a dynamic storage Zone

Generally, global variables and static local variables are in the static storage area.

Non-static auto variables, on-site protection during function calls, and return addresses are on the stack, while the dynamically allocated space is on the heap.

I. Local variable

1. Auto variable

It is implicitly an Automatic Storage Class and belongs to dynamic storage. The auto keyword can be omitted.

2. Static declared local variables or static local variables

Static storage, which still exists after function call

Note: Although static local variables still exist after the function call, other functions cannot reference them.

3. Local variables declared by register

Only Local Auto variables and formal parameters can be used as register variables. Local static variables cannot be used as register variables.

II. External variable (or global variable)

1. External variables declared in a file

Example:

 
Main ()
 
{
 
ExternA, B;
 
Printf ("% D", );
 
}
 
 
 
IntA = 1, B =-8;

2. Declare external variables in multiple files

Function: extends the scope of external variables.

 
// File1.c
 
IntA;
 
 
Main ()
 
{
 
//...
 
}

 

 
// File2.c
 
ExternA;
 
Power (IntN)
 
{
 
//..
 
}

3. Static declared external variables

Purpose: restrict the scope of external variables to the file reference, but not to other files.

 
// File1.c
 
Static IntA;
 
Main ()
 
{
//...
 
}
 
// File2.c
 
ExternA;
 
Fun (IntN)
 
{
 
A = A * N;
 
//...
 
}
 
// Error: file2.c cannot use the, although declare itAsExtern

 

3. Differences between variables and declarations:

For functions: the difference is obvious.

Function declaration is the prototype of a function.

The function definition includes the function body, that is, the function implementation.

For variables:

A broad declaration includes definitions, including defining declaration and reference declaration)

A narrow statement refers to a non-defined statement.

Definition declaration refers to the need to create a bucket, while the reference declaration does not need to create a bucket

The definition of a bucket is generally used for describing convenience, but the declaration of a bucket that does not need to be created is called a declaration. Obviously, the declaration here is narrow, that is, non-definition declaration.

2.2 Functions

1. Internal functions

For example:

 
Static IntFun (IntA,IntB)
 
{
 
//...
 
}

Using Internal functions, You can restrict functions to only files

2. External Functions

1) For example:

 
Extern IntFun (IntA,IntB );

In this way, function fun can be called in other files. The C language specifies that if extern is omitted when defining a function, it is implicitly an external function.

2) in the file that needs to call this function, use extern to declare that the function used is an external function. This extern can also be omitted, but only the function prototype can be written.

 

Summary:

From the scope perspective, there are local variables and global variables.

    • Local variable
      • Automatic variables, that is, dynamic local variables (values disappear when the function is left)
      • Static local variable (leave the function and keep the value)
      • Register variable (when the function is left, the value disappears)
    • Global Variables
      • Static external variables (only available for reference in this document)
      • External variables (non-static external variables that can be referenced by other files)

From the variable life cycle, there are dynamic and static storage

    • Static storage
      • Static local variables (valid in the function)
      • Static external variables (valid in this file)
      • External variables (other files can be referenced)
    • Dynamic Storage
      • Automatic variable (valid in function)
      • Register variable (valid in function)
3 federated union

Define consortium and consortium Variables

For example:

 
Union sa_union
 
{
 
IntI;
 
CharCh;
 
FloatF;
 
} A, B, C;

Note: you cannot reference a federated variable. You can only reference members in a federated variable, such as A. I, B. ch, and C. F.

 
Printf ("% D", );
 
// Error !!!

1) each instantaneous can only store one of them and cannot work at the same time.

2) the members that play a role in the federated variables are the members that were last stored. After a new member is saved, the original members become useless.

3) the address of the consortium variable and the address of each member are the same address. For example, & A, & A. I, & A. C, & A. F are all the same address values.

4) The following are all incorrect:

    • Reference a consortium variable name to get a value, but you can use a consortium variable pointer.
    • When defining a federated variable, it is initialized.
    • Using federated variables as function parameters
    • Returns a federated variable to a function.
4 struct and bitfield

The C language allows a struct to specify the Memory Length occupied by its members in units of bits. This bit-based member is called a "bit domain"

 
StructPacked_data
 
{
 
Unsigned A: 2;
 
Unsigned B: 6;
 
Unsigned C: 4;
 
Unsigned D: 4;
IntI;
 
} Data;

A, B, C, and D occupy 2 bytes, And I occupies 4 bytes.

Sizeof (data) = 8

 
StructPacked_data
 
{
 
Unsigned A: 2;
 
Unsigned B: 3;
 
Unsigned C: 4;
 
IntI;
 
} Data;

1. A single-byte field must be stored in the same byte, and cannot span two bytes. If the remaining space of one byte is insufficient to store another domain, it should be stored from the next unit. You can also intentionally start a domain from the next unit. Example:
struct BS
{< br> unsigned A: 4
unsigned: 0/* airspace */
unsigned B: 4/* store the data starting from the next unit */
unsigned C: 4
}< br> In this bit field definition, a occupies 4 of the first byte, if the last four digits are set to 0, it indicates no use. B starts from the second byte and occupies 4 bits. c occupies 4 bits.
2. Because the bit field cannot span two bytes, the length of the bit field cannot exceed the length of one byte, that is, it cannot exceed 8-bit binary.
3. A bit domain can be a non-bit domain name. In this case, it is only used for filling or adjusting the position. An anonymous domain cannot be used. For example,
struct k
{< br> int A: 1
INT: 2/* The two digits cannot be used */
int B: 3
int C: 2
};
from the above analysis, we can see that bit fields are essentially a structure type, however, its members are allocated by binary.

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.