iOS notes-macro definition, conditional compilation, and file import notes

Source: Internet
Author: User
Tags mul


1, preprocessing instructions

(1) Concept: C language in the source program before compiling, will first of some special preprocessing instructions to explain (such as #include<> file contains instructions), a new source program, this process is called compilation preprocessing, and then the usual compilation.

(2) Preprocessing instructions are all preceded by "#" and do not end with a semicolon.

(3) Preprocessing instructions can be placed anywhere in the file, his scope is from where it appears to the end of the file, we often put it in the source program head, so that its scope is the entire source program file.

(4) Preprocessing instructions can be divided into three categories: macro definition, file inclusion, conditional compilation

2, macro definition

(1) macro definition with no parameters (macro name is general capitalization)

General form: #define Macro name string such as: #define COUNT 5;

(2) macro definition with parameters

General form: #define Macro name (parameter) enclose parameters in parentheses to prevent the macro from being replaced simply because operator precedence results in inaccurate operations.

Macros with parameters are more efficient than functions, and macros only replace values (text Exchange), do not count, and do not replace strings enclosed in double quotes.

Example:

1 The use of macro definitions can be unified variable, easy maintenance.

#include <stdio.h>

#define COUNT 5//The macro name is generally uppercase, ending with a semicolon ";"

void Main ()

{

int A[count] = {1,2,3,4,5};

for (int i =0;i< count;i++) {

printf ("a[%d] =%d\n", i,a[i]);

}

}

2 The macro definition will be simply replaced, not calculated, and will not replace the strings enclosed in double quotes.

#include <stdio.h>

#define SUM (A,B) a+b//macro definition will only be replaced, not calculated, so the parameters we'd better enclose in parentheses, the following macro definition

#define MUL (A,b) ((a) * (b))//If Mul (1+2,3+4) This parameter is passed to the macro here, because the arguments are enclosed in parentheses, there is no need to worry about the problem of multiple identical macro computations being incorrect.

void Main ()

{

int b =sum (1,2);//This macro definition is similar to calling a function

printf ("sum (1,2) =%d\n", b);

int c =mul (1+2,3+4);

printf ("Mul (1+2,3+4) =%d\n", c);

int d =mul (2,3)/mul (4,2);//This corresponds to ((2) * (3))/((4) * (2)), because the parameters are enclosed in parentheses, without worrying about operator precedence, which results in inaccurate calculations. Therefore, it is best to enclose the parameters in parentheses.

printf ("Mul (2,3)/mul (4,2) =%d", D);

}

3. Conditional compilation

(1) Concept: When the set of conditions to meet the condition of the code to compile, this is conditional compilation.

(2) Format:

#if Condition 1

... code1 ...

#elif Condition 2

... code2 ...

#else

... code3 ...

#endif

Description: Preprocessing instructions are compiled before, not run-time, so conditional compilation should pay attention to if conditions, do not have not yet run, the source program in the first variable as a condition to judge, the variable is run only, and conditional compilation is compiled before running. Therefore, conditional compilation conditions are typically based on macro definitions, because both macro definitions and conditional compilation are performed before compilation.

As an example of the following error:

#include <stdio.h>

void Main ()

{

int a = 8;

#if a>7

printf ("a>7");

#elif a<7

printf ("a<7");

#else

printf ("a!=7");

#endif

}

The result of the operation is a<7, and we expect the result is different, normal should be output a>7 is right.

The correct wording is as follows:

#include <stdio.h>

#define A 8

void Main ()

{

#if a>7

printf ("a>7");

#elif a<7

printf ("a<7,%d", a);

#else

printf ("a!=7");

#endif

}

Output is a>7

(3) Other uses

1) #if defined (macro name)

... code ...

#endif

Description: Indicates that if the macro is already defined, the contents of the code block are edited.

2) #if!defined (macro name)

... code ...

#endif

If this macro is not defined, the code is compiled.

3 #ifdef The name of the macro is the same as the first.

4 #ifndef The name of the macro is the same as the second.

4. File contains

(1) Concept: In fact, #include instructions, if it is the system with the #include< file name, it will go to the C language function header files in the directory to find files. If you want to introduce your own file, then use #include "filename", the system will be in the source program under the current directory search, if not, and then to the operating system's path path to find, can not find, will be to C language Library function header file directory to find.

(2) The file contains the need to pay attention to the introduction of file duplication problem, file duplication will result in duplicate statements. To solve the problem of duplication, you can use the macro definition to determine whether the definition has already been defined, if the definition has not been imported, if no more import in.

The code is as follows:

Prevents a header file from being repeatedly included
#ifndef _one_h that if one.h is not defined
#define _ONE_H define ONE.H this macro


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.