Latest web development tutorials

C preprocessor

Part of theC preprocessor is not the compiler, but it is the process of compiling a separate step.In short, C preprocessor is just a text substitution tool only, they will instruct the compiler to compile complete pretreatment before the actual need. We will C preprocessor (C Preprocessor) abbreviated as CPP.

All orders are pre-processor with a pound sign (#) at the beginning. It must be the first non-blank character, in order to enhance readability, preprocessor directives should begin in the first column. Listed below are all the important pre-processor instructions:

指令描述
#define定义宏
#include包含一个源代码文件
#undef取消已定义的宏
#ifdef如果宏已经定义,则返回真
#ifndef如果宏没有定义,则返回真
#if如果给定条件为真,则编译下面代码
#else#if 的替代方案
#elif如果前面的 #if 给定条件不为真,当前条件为真,则编译下面代码
#endif结束一个 #if……#else 条件编译块
#error当遇到标准错误时,输出错误消息
#pragma使用标准化方法,向编译器发布特殊的命令到编译器中

Examples of pre-processor

Analysis to understand the following examples different instructions.

#define MAX_ARRAY_LENGTH 20

This directive tells CPP to replace all MAX_ARRAY_LENGTH 20. Use#defineto define constants to enhance readability.

#include <stdio.h>
#include "myheader.h"

These instructions tell the CPP stdio.h get from thesystem library, and add text to the current source file.The next line tells CPPmyheader.h get from the local directory and add content to the current source file.

#undef  FILE_SIZE
#define FILE_SIZE 42

This command tells CPP cancel FILE_SIZE defined, and define it as 42.

#ifndef MESSAGE
   #define MESSAGE "You wish!"
#endif

This command tells CPP only when MESSAGE undefined only when defined MESSAGE.

#ifdef DEBUG
   /* Your debugging statements here */
#endif

This command tells CPP if you define DEBUG, then performs processing statement. At compile time, if you pass-DDEBUGswitch to gcc compiler, this command is useful. It defines DEBUG, you can always turn on or turn off debugging during compilation.

Predefined macros

ANSI C defines a number of macros. In programming, you can use these macros, but different to directly modify these predefined macros.

描述
__DATE__当前日期,一个以 "MMM DD YYYY" 格式表示的字符常量。
__TIME__当前时间,一个以 "HH:MM:SS" 格式表示的字符常量。
__FILE__这会包含当前文件名,一个字符串常量。
__LINE__这会包含当前行号,一个十进制常量。
__STDC__当编译器以 ANSI 标准编译时,则定义为 1。

Let's try the following examples:

#include <stdio.h>

main()
{
   printf("File :%s\n", __FILE__ );
   printf("Date :%s\n", __DATE__ );
   printf("Time :%s\n", __TIME__ );
   printf("Line :%d\n", __LINE__ );
   printf("ANSI :%d\n", __STDC__ );

}

When the above code (in the filetest.c) are compiled and executed, it produces the following results:

File :test.c
Date :Jun 2 2012
Time :03:36:24
Line :8
ANSI :1

Preprocessor operator

C preprocessor provides the following operators to help you create a macro:

The continuation of macro operator (\)

A macro is usually written on a single line. However, if the macro is too long to fit in a single line, use the continuation of macro operator (\). E.g:

#define  message_for(a, b)  \
    printf(#a " and " #b ": We love you!\n")

String literals quantization operator (#)

In macro definitions, when you need to put a macro argument into a string constant, the string constant quantization operator (#). Macro using the operator has a specific parameter or parameter list. E.g:

#include <stdio.h>

#define  message_for(a, b)  \
    printf(#a " and " #b ": We love you!\n")

int main(void)
{
   message_for(Carole, Debra);
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Carole and Debra: We love you!
Token pasting operator (##)

Token pasting operator (##) within the definition of the macro will merge the two parameters. It allows two independent macro definition tags are combined into a tag. E.g:

#include <stdio.h>

#define tokenpaster(n) printf ("token" #n " = %d", token##n)

int main(void)
{
   int token34 = 40;
   
   tokenpaster(34);
   return 0;
}

When the above code is compiled and executed, it produces the following results:

token34 = 40

This is how it happened, because this example will produce the following actual output from the compiler:

printf ("token34 = %d", token34);

This example demonstrates the token ## n connects to token34 in here, we used thestring constant quantization operator (#), and token pasting operator (##).

defined () operator

Defined preprocessor operator is used in a constant expression used to determine whether an identifier has been defined using the #define.If the specified identifier is already defined, the value is true (non-zero). If the specified identifier is not defined, the value is false (zero). The following example illustrates the defined () operator usage:

#include <stdio.h>

#if !defined (MESSAGE)
   #define MESSAGE "You wish!"
#endif

int main(void)
{
   printf("Here is the message: %s\n", MESSAGE);  
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Here is the message: You wish!

Parameterized macro

CPP is a powerful feature can be used to simulate parameterized macro function. For example, the following code is to calculate the square of a number:

int square(int x) {
   return x * x;
}

We can use a macro to rewrite the above code, as follows:

#define square(x) ((x) * (x))

Before using macros with parameters, you must usea #define directive.Parameter list is enclosed in parentheses, and must be immediately behind the macro name. A space between the macro name and the left parenthesis is not allowed. E.g:

#include <stdio.h>

#define MAX(x,y) ((x) > (y) ? (x) : (y))

int main(void)
{
   printf("Max between 20 and 10 is %d\n", MAX(10, 20));  
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Max between 20 and 10 is 20