C preprocess

  1. Preprocessor
    1. 1. Macros using
      1. Macro Vs. Inline
    2. 2. Including head files
    3. 3. Conditional Compilation
  2. References

Preprocessor

The C preprocessor is a macro preprocessor which allows you to define macros that transforms program before it compiled. These transformations can be inclusion of header file, macro expansions etc. There are three common preprocessor strategies:

  • Macros using #define
  • Including Header Files
  • Conditional Compilation

1. Macros using

A macro is a fragment of the code that is given a name. We can use that fragment of code in our program by using this name.

The core idea of the #define macro is the text substitution. It’s better that adding bracket for each variable.

A simple macro which is used to get the maximum value from two values:

#define min(a,b) ((a)<(b)?(a):(b))

For example:

#define SQR(X) X*X
...
a /= SQR(k+m)/SQR(k+m);

The result will be: a = a / k+m*k+m/k+m*k+m. Obviously, this is not what we want. Here, the problem is that the macro is the pure text substitution. It means that X will be substituted by k+m in any case. Therefore, the better method of using macro is adding bracket for each variable:

#define SQR(X) ((X)*(X))
...
a /= SQR(k+m)/SQR(k+m);

Then the final result is ((k+m)*(k+m))/((k+m)*(k+m)).

A more complicated example of macro is using macro to handle error:

#define CHECK_ERROR(error_status, error_func)\
({\
    if(error_status!=ERROR_SUCCESS)\
    {\
        if(error_status!=ERROR_SUCCESS_HELP)\
        {\
            fprintf(stderr, "ERROR::%s:%d:%s: Function %s returned error %d!\n",\
                        __FILE__, __LINE__, __func__, error_func, error_status);\
        }\
        return(error_status);\
    }\
})

Macro Vs. Inline

  • Inline is just a request to the compiler in C++, macro is the preprocessor in C.
  • Inline replaces a call to a function with the body of the function, macro is expanded by the preprocessor before compilation(macro is text substitution).
  • Inline can do the type checked, macro can not.
  • Inline is used to optimize the program, macro is used to achieve inline function before C99 standard.

2. Including head files

/* "" will find the .h file in the current project firstly, then to the system*/
#include <stdio.h>. // used for standard .h
#include "my_header.h" // used for user-defined .h

The #include preprocessor directive will replace the above line with the contents of stdio.h head file which contains the function and macro definitions.

3. Conditional Compilation

Conditional compilation is the process of defining compiler directives that cause different parts of the code to be compiled, and others to be ignored. This technique can be used in a cross-platform development scenario to specify parts of the code that are compiled specific to a particular platform.

#ifdef MACRO     
     conditional codes
#endif

Here, the conditional codes are included in the program only if MACRO is defined.

Note: the MACRO is not necessary defined in the program, it also can be defined out of the program by compilation. For example, if we use icc compiler, we can use -D flag to define the custom macro to the program. Example

#if expression
   conditional codes if expression is non-zero
#elif expression1
    conditional codes if expression is non-zero
#elif expression2
    conditional codes if expression is non-zero
...
#else
   conditional if all expressions are 0
#endif

Here, expression is a expression of integer type (can be integers, characters, arithmetic expression, macros and so on). The conditional codes are included in the program only if the expression is evaluated to a non-zero value.

#if defined BUFFER_SIZE && BUFFER_SIZE >= 2048
  conditional codes

The special operator #defined is used to test whether certain macro is defined or not. It’s often used with #if directive.

References


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 gzrjzcx@qq.com

文章标题:C preprocess

文章字数:608

本文作者:Alex Zou

发布时间:2019-02-22, 23:23:13

最后更新:2024-07-10, 03:02:36

原始链接:https://www.hellscript.cc/2019/02/22/subposts_c/c-preprocess/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

有钱的捧个钱场,没钱的借钱也捧个钱场