iOS development minutes to the C language-macro definition and keywords

Source: Internet
Author: User

One, the macro definition

Concept: the macro definition is essentially a precompiled instruction that pays certain instructions to the appropriate variables before the program is run. The general preprocessor directives start with the # number, so the macro definition is also developed in #, the keyword is # define (definition macro definition), #undef (end macro definition).

defining formats and scopes
The General macro definition is defined in the first paragraph of the program: #define the macro name value.
Macro definition scope: From the beginning of the line defined, until the end of the file, although by default the scope of the macro definition starts from the defined line, until the end of the file. But we can also use the corresponding keyword #under to end the scope of the macro definition prematurely.

macro Definition Specification
In general, macro names are capitalized, and multiple words are separated by _, and each word is capitalized and written in the program's head.
Some companies also require the macro name to start with K, multiple words with the name of the hump
Attention:
Do not use a semicolon after the macro definition.

macro definition usage scenarios in iOS development
Get the width of the screen
Get the phone system version number
Make a single case
Judging the system version
··························
macro definition with Parameters
Macro definition with parameters Note points:
1. In general, when writing a macro with parameters, add one () to each parameter
2. In general, it is recommended to write a macro with a parameter, add a () to the result

Second, the const keyword
Const acts on the base data type to make the variable of the base data type a constant.
The const is written in two ways:
1. Write to the left of the data type
2. Write to the right of the data type

the role of the const
(1) You can define const constants, which have immutability.
2) easy to type check, so that the compiler to deal with the content of more understanding, eliminate some hidden dangers.
(3) can avoid the ambiguity of the number appears, the same can be easily adjusted and modified parameters. As with the definition of a macro, you can do it without changing it.
(4) Can protect the modified things, prevent accidental modification, enhance the robustness of the program.
(5) Can save space and avoid unnecessary memory allocation.
(6) Improve efficiency. The compiler typically does not allocate storage space for ordinary const constants, but instead saves them in the symbol table, which makes it a constant during compilation, without the storage and read memory operations, making it highly efficient.

How to use the const

1. Modifier constant pointer: (interview frequently test)
const int *a; const modifier pointer, a variable, a-pointing value cannot be modified
int const *A; The const modifier points to the object, a variable, the object that a points to is not mutable
int *const A; Const modifier pointer A, a immutable, variable a pointing to an object
const int *CONST a;//Pointer A and a point object are not mutable

tips for modifying regular pointers
look at the position of "*" first.
if the const on the left side of the * indicates that the value cannot be modified, the point can be changed.
if the const on the right side of the * indicates that the pointer cannot be changed, the value can be changed
If both sides of "*" have a const identity point and the value cannot be changed.

2. Modifying General constants
The modifier const can be used before the type specifier, or it can be used in the type description specifier.

3. Modifier Constants Group
Modifier constant Group (value cannot be changed again) defines or describes a constant group that can be in the following format:
int const A[5]={1, 2, 3, 4, 5};
const int A[5]={1, 2, 3, 4, 5};
const int A[5]={1, 2, 3, 4, 5};
A[1] = 55; Error

4. Constant parameters of the modifier function
The constant-parameter const modifier of the modifier function can also modify the function's pass-through parameters in the following format: void fun (const int Var); Tells the compiler that Var cannot be changed in the body of the function, thus preventing some unintentional or erroneous modifications by the user.

5. The return value of the modifier function
The return value of the modifier function: The const modifier can also modify the return value of the function, which is the return value that cannot be changed, in the following format:
const int FUN1 ();
Const MyClass FUN2 ();

Third, static and extern keywords

internal and external functions
intrinsic function: A function that can be accessed only in this file.
External functions: Functions that can be accessed in this file and in other files.
By default, all functions are external functions.

Static keyword
1. As long as the function is preceded by the return value of static to make the function into an internal function, the other files can not be accessed.
2. If static is written in the implementation of the function, it represents the definition of an intrinsic function.
3. If static is written in the declaration of a function, the delegate declares an intrinsic function

The "interview" keyword static has three distinct effects:

1) in the body of a function, a variable declared as static maintains its value in the process of the function being called (the variable is stored in the static variable area).

2) within the module (but outside the function body), a variable declared as static can be accessed by a function within the module, but not by other functions outside the module. It is a local global variable.

3) within the module, a function declared as static can only be called by other functions within this module. That is, this function is restricted to the local scope of the module that declares it.

extern keyword
1. As long as you precede the return value of the function with an extern, you can make the function an external function, because the default is the external function, so in the development of the general situation extern no one writes.

2. If extern is written in the implementation of a function, it represents the definition of an external function.

3. If extern is written in the declaration of a function, the delegate declares an external function.

iv. typedef keywords
typedef can alias a known data type (nickname), which is often used in iOS development, so it must be mastered.

format of typedef:
typedef the original data type alias (nickname);

typedef NOTE:
1. typedef can not only alias the original data type of the system, but also can alias a custom data type.
2. Using typedef to alias a data type does not generate a new data type, just an alias for the original type.

to alias an enumeration type
1. Define the enumeration type first, and then alias the enumeration type
Enum Gender
{
Kgendermale,
Kgenderfemale
};
typedef enum GENDER SEX;

2. Alias an enumeration type while defining an enumeration type
typedef enum GENDER
{
Kgendermale,
Kgenderfemale
} SEX;

3. Alias The enumeration type while defining the enumeration type, and omit enumerating the original type names
typedef enum
{
Kgendermale,
Kgenderfemale
} SEX;

There are 3 ways of defining enumeration variables
1. Define the enumeration type first, and then define the enumeration variables
2. Defining enumeration variables while defining enumeration types
3. Define an enumeration variable at the same time as the enum type, and omit the enumeration type name

alias struct type
1. Define the struct type first, and then alias the type
struct person
{
int age;
Double height;
Char *name;
};
Sperson = = struct person
typedef struct person Sperson;

2. Define the struct type and alias the struct type
typedef struct PERSON
{
int age;
Double height;
Char *name;
} Sperson;

3. Define the struct type, alias the struct type, and omit the name of the original type
typedef struct
{
int age;
Double height;
Char *name;
} Sperson;

Copyright NOTICE: This article for Bo Master original article, in order to be able to promote each other, learn from each other, please follow Sina Weibo: Geek James

iOS development minutes to the C language-macro definitions and keywords

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.