iOS Development Learning Note 008

Source: Internet
Author: User

pretreatment

1. Macro definition

2. Conditional compilation

3. The file contains

Attention:

1. All preprocessing starts with # and ends without semicolons.

2. macro names are generally capitalized in order to distinguish them from variable names, but there is no syntax error in lowercase

  3, scope also from definition to code block end

4. When defining a macro, you can refer to a macro name that has already been defined

5, when the compilation preprocessing uses the string substitution macro name, does not make the syntax check, is simply the string substitution. The source program that has expanded the macro name is checked for syntax only at compile time

One, the macro definition

1. Macro Definition without parameters

#define Macro Name string

#define COUNT 6

2. Macro definition with parameters

#define Macro Name (argument list) string

#define SUM (VA,VB) ((VA) + (VB))

Note: parentheses are added to each parameter, and the result of the operation is also parentheses

If you need to cancel the macro earlier, define the available #undef count implementation.

code example

1 //macro names are generally capitalized in order to distinguish them from variable names, but there is no syntax error in lowercase2 //1, macro definition 1, macro definition without parameters, commonly used to define constants3 #defineCOUNT 6//is there a '; ' In the back?4 5#include <stdio.h>6 //1, macro definition 2, with the parameters of the macro definition, each parameter must be parentheses, the result of the operation will be parentheses7 #defineSUM (V1,V2) ((v1) + (v2))8 9 Ten intMain () One { A     //1, macro definition use -     intArray[count] = {1,2,3,4,5,6}; -      for(inti =0; i < COUNT; i++) the     { -printf"%d", Array[i]); -     } - #undefCOUNT//Cancel macro definition in advance +     //Macros with Parameters -printf"\n%d\n", SUM (Ten, -)); +printf"%d\n", SUM (3,3) *sum (4,4));//so the actual effect is 3+3*4+4=19, because the macro is simply a text substitution, so the definition of the addition () is better A  at     //2. Conditional compilation -      -      -     return 0; -}

Second, conditional compilation

The code is compiled if the condition is met, otherwise it is not compiled.

#if conditions 1

.....

#elif conditions

......

#else

.....

#endif//Indispensable

1 #defineCOUNT 6//is there a '; ' In the back?2   //2, conditional compilation, generally judge the value of the macro, can not operate on the variable3 #if(COUNT = = 6)4printf"conditional Compilation:%d\n", COUNT);5 #elif(COUNT = = 4)6     7 #else8     9 #endif//not missing
use of #if defined () and # if!defined ()
#if defined (COUNT)// COUNT is a macro that executes the following code if the macro is defined
.... Code ...
#endif
#if!defined (COUNT)// COUNT is a macro that executes the following code if the macro is not defined
.... Code ...
#endif
Code:
1 //usage of #if defined () and # if!defined ()2 #ifDefined (COUNT)//If you define a macro, compile the code below, or do not compile3printf"defined:%d\n", COUNT);4 #endif5     6 #if!defined (COUNT1)//If you define a macro, compile the code below, or do not compile7printf"! defined\n");8 #endif

#ifdef和 The use of #ifndef
#ifdef Count//count is a macro, if you define this macro, compile the following code ...  #endif

#ifndef Count//count is a macro, if you do not define this macro, compile the following code ...  #endif
Code:
1 //#ifdef和 The use of #ifndef2#ifdef COUNT//If you define a macro, compile the code below, or do not compile3printf"ifdef:%d\n", COUNT);4 #endif5  6#ifndef COUNT1//If you define a macro, compile the code below, or do not compile7printf"ifndef\n");8 #endif

Third, the document contains

#include

#include <>//system header file

#include ""//Custom header file

The file contains a macro definition that may be repeated containing the same file, which is a file that can be included again to eliminate duplicate inclusions.

The format is as follows

#ifndef a_h

#def a_h

..........

#endif

This way, even if multiple # include the same header file, there is no duplication of files included

2015-4-14 today, and tomorrow remains.

iOS Development Learning Note 008

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.