# 開頭,巨集名慣例全大寫,行尾無分號。#define 識別碼 替換文本
預處理階段將原始碼中所有 識別碼 替換為 替換文本。常用於:
#define N 10)#define IN scanf)#include <stdio.h>
#define IN scanf
#define OUT printf
#define N 2
void main()
{
int i = 0;
char name[N][20];
long num[N];
OUT("Please input name and ID: ");
for(i; i<N; i++) { IN("%s", name[i]); IN("%ld", &num[i]); }
for(i=0; i<N; i++) { OUT("%s ", name[i]); OUT("%ld ", num[i]); }
}
VC6.0 專案設定 /P 參數僅預處理產生 .i 檔,打開可見:
char name[2][20];
long num[2];
printf("Please input name and ID: ");
for(i; i<2; i++) { scanf("%s", name[i]); scanf("%ld", &num[i]); }
...
所有 N、IN、OUT 均已替換。
#define dodolook int g_a1 = 0x12345678; static int g_a2 = 0x87654321;
dodolook void go() { static int x = 0x88888888; printf("%08x", x); }
int main() { static int x = 0x12312312; dodolook go(); return 0; }
巨集可插入聲明、定義、甚至整個程式碼區塊,極具靈活性但也易濫用,需謹慎。