Const constant usage

Recently written code, see a lot of places used const, decided to write the article once recorded

First of all, when we see const, we think of macro. First look at the difference between macro and Const

First, the difference between const constants and macros

1.define is replaced during the preprocessing phase, and the const constant is used during the compile phase

The 2. macro doesn’t do type checking, just changes, const constants have data types, and type checks are performed

3.define cannot debug, and const constants can be debugged

The constants defined by 4.define continue to take up memory when they are replaced, and the constants defined by the const are stored in the data section with only one copy, which is more efficient

5.define can define some simple functions, and const cannot

Macro definition

#define, XMGUserDefaults, [NSUserDefaults, standardUserDefaults],

#define XMGNameKey @ @ name”

Definition of const

CGFloat const a = 3;

Second: the role of const

Const:

1. modified right basic variables or pointer variable int a int *p

2. modified by const variables can only be read, but not modify.

Int const * p; / / p: *p: read-only variable

Int const * P1; / / p1: *p1: read-only variables

Const int * P2; / / p2: *p2: read-only variables

Const int const * P3; / / p3: *p3: read-only read-only

Int const const * P4; / / p4: *p4: read-only read-only

Third: the use of const

1. modifies global variable => global read only variable => instead of macro

NSString * const name = @ name ‘;

2. parameter in the modification method

Int a = 0;

[self test:& a];

– – (void) test: (int, const *) a

{

/ / *a = 3;

}

Fourth: static and extern use

The use of static

1. modify local variables, modify local variables by static, and extend the life cycle associated with the entire application

* static modifies local variables and allocates only one memory

* static modifies local variables to allocate memory. Once a program runs, it allocates memory to the static modifier variables

2. modifies global variables and modifies global variables by static. Scopes are modified and can be used only in the current file

The use of extern

Declare external global variables, note that extern can only be used for declarations and cannot be used for definitions

Extern working principle: first go to the current file to find, there is no corresponding global variables, if not, go back to other documents to find

Fifth: static and const combined use

The combined use of static and const.

/ / const modified global variables

/ / static modified global variables, modify scope

Static NSString * const name = @ name ‘;

Sixth: extern and const use
together

/ / regulations: global variables cannot be defined in their own class, create a global file management global things

XMGKIT_EXTERN NSString * const discover_name;

/ / global definitions throughout the project variables

/*************** found *************/

NSString * const discover_name = @ name”;