Storage category and type limiters

Source: Internet
Author: User

 

Storage Type

 

Five categories: automatic, register, static with code block scope, static with external links, static with internal links.

 

Link: External link: variable in the file scope. Internal link: static modified variables with file scopes have internal links ),

 

Empty link: variables with code block scopes or function prototype scopes have empty links.

 

<1> local variables: defined in the function and visible in the function;

 

Global variables (external variables): external function definitions, which are visible in this source file. Generally, the first letter of the global variable is capitalized. <2> dynamic storage: auto Declaration (dynamic variable) Register (Register) --------------- the static storage of the bucket is released after the function call ends: static declared local variables (extern declared) external variables -------------- do not release during the entire program running <3> declare local variables with static: do not lose the original value after the function call ends. <4> register variable: only local dynamic variables and form parameters can be used as register variables for quick storage. Register long I; the address of the register variable cannot be obtained; if there are redundant registers, place I in the register first to speed up execution. <5> Use extern to declare external variables: declare external variables in multiple files: In. the C file defines the static variable num with external links, and in the other file, use extern to declare the external variable num (extern num) for num ). This statement is often placed in the header file of the C file that defines the variable. The external functions in the. c file can also be declared to improve the readability of the program. <6> use static to declare external variables: these variables are only used by this file and cannot be used by other files.

 

 

Type qualifier

1, const

Http://blog.csdn.net/welcome_ck/article/details/227964

<1> Use const for global data to protect data, including const variables, arrays, and structures.

For example, const int A = 2; int const A = 2;

When files share const data:

It can be declared with extern in other files, extern const double Pi (usually placed in a. h file corresponding to the. c file defining the constant );

To define constants in the header file, you must use the static Declaration: static const double Pi = 3.14159.

<2> modify a Common Object
A constant object is an object constant. The definition format is as follows:
Class;
Const;
A const;
When defining a common object, Initialization is also required, and the object cannot be updated. The modifier const can be placed after the class name or before the class name.
<3> modify the regular pointer
Const int * P = date; or Int const * P = date;

The pointer pointing to a constant cannot use the pointer to modify the value (commonly used as a function parameter) * p = 1; (not allowed) P [1] = 2; (not allowed) date [0] = 1 (allowed ):

Int * const P = date; P must always point to the same address, but the value can be changed.

Const int * const P = date; P must always point to the same location, and the stored value cannot be changed.

You can assign a constant or a constant address to a pointer to a constant. You cannot assign the address (or const int * P1) of the constant data to a normal pointer, because the const data may be changed through a new pointer.

<4> modify common references
The const modifier can also be used to describe the reference. The referenced object cannot be updated. The definition format is as follows:
Const double & V;
<5> modify the common parameters of a function
The const modifier can also modify the passing parameters of a function. The format is as follows:
Void fun (const int var); void fun (const stud * student );
Tells the compiler that VaR cannot be changed in the function body, thus preventing unintentional or incorrect modifications.
<6> modify the member functions of the class:
The const modifier can also modify the member functions of the class in the following format:
Class classname
{
Public:
Int fun () const;
.....
};
In this way, the data in the class cannot be modified when the function fun is called.
Note that constants must be initialized!

 

2, volatile

Http://www.cnblogs.com/chio/archive/2007/11/24/970632.html

<1> volatile variables may change at any time. Do not compile and optimize the operations related to volatile variables to avoid errors.

<2> variables defined by volatile are changed outside the program. Each time they are read from the memory, they cannot be reused in the cache or register.

Example of volatile variables:

1) Hardware registers of parallel devices (for example, Status Registers)
2) Non-automatic variables that will be accessed in an interrupt service subroutine)
3) variables shared by several tasks in multi-threaded applications

Note:

<1> one parameter can be const or volatile.

For example, read-only status registers. It is volatile because it may be unexpectedly changed. It is const because the program should not try to modify it

<2> A pointer can be volatile.

Uncommon: for example, when a medium service subroutine repairs a pointer to a buffer.

<3>

Volatile char;
A = 0;
While (! A ){
// Do some things;
}
Doother ();
If there is no volatile doother (), it will not be executed.

 

3, restrict

It can only be used to modify a pointer. It is a unique and initial method for accessing a data object.

Purpose:

<1> tell the compiler to make optimization assumptions freely (this is not a concern ).

<2> tell the programmer to use parameters that meet restrict requirements. Generally, the compiler cannot check whether you have followed this restriction.

C99 function: Copy n Bytes at the S2 position to the S1 position:

Void * memcpy (void * restrict S1, const void * restrict S2, size_t N );

// Indicates that when you use this function, S1 and S2 are the only access method for the corresponding data, that is, to ensure that the space of the n Bytes that S1 and S2 point to is not repeated.

Void * memmove (void * S1, const void * S2, size_t N );

// Overlapping is allowed. This function avoids Overwriting data before data is used when copying data.

 

Random function:

<Stdlib. h>

Rand () generates 0 ~ An integer in the range of rand_max; Roll = rand () % 6 + 1; roll is 1 ~ An integer between six. With this function, the random number is the same every time you re-execute it.

# Include <time. h>

Srand (unsigned INT) time (0); // initialize the seed

The time () function returns the system time. The unit is determined by the system. The return value belongs to the numerical type.

Storage category and type limiters

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.