/*
**test26.cpp : Defines the entry point for the console application.
**系統(tǒng)winXP SP3 32位.
**編譯預處理之條件編譯、#運算符
*/
#include "stdafx.h"
#include "iostream.h"
#define TEST 0
#define TEST1(x) #x #x #x #x
#define TEST2(x) "hello," #x " end."
//#的功能是將其后面的宏參數(shù)進行字符串化操作。
#ifndef TEST
#error ERROR: TEST is not define
#endif
#pragma comment(lib, "user32.lib")
int main(int argc, char* argv[])
{
int iNum = 123;
cout<<TEST1(iNum)<<endl; //iNumiNumiNumiNum,轉(zhuǎn)換的不是實際參數(shù)的值,不會進行數(shù)值的轉(zhuǎn)換
cout<<TEST2(123)<<endl; //hello, 123 end,成功
return 0;
}
Tips1:條件編譯可以控制預處理器選擇不同的代碼段作為編譯器的輸入,使得源程序在不同編譯條件下產(chǎn)生不同的代碼。與程序條件語句(ifelse)類似。主要有#if、#ifdef、#ifndef、#elif、#else、#endif、define(define不能單獨使用,需要與#if、#elif結(jié)合使用)
如:#if 0
/***
不想編譯的代碼段,要想編譯此處,需要將#if后面改成非0值
***/
#endif
Tips2:#error指令用于輸出平臺、環(huán)境等先關(guān)信息。
如:#ifndef TEST
#error ERROR:TEST is not define編譯時,如沒有定義TEST,則會報錯。