#define命令用于把指定的字符串替换文件中的宏名称 . 也就是说, #define使编译器把文件中每一个macro-name替换为replacement-string. 替换的字符串结束于行末. 这里是一个经典的#define应用 (至少是在C中):
1。简单的宏定义:宏名 字符串(与类型相匹配)后面不能跟有分号(;)
2。带参数的宏定义: 宏名(形参) 宏体(就是要做的事情)
例子1:// console.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h";
#define str(ch) ch
void main()
{
printf("%s",str("huhuoyun"));
}
输出结果为:huhuoyun
例子2:// console.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h";
#define str(ch) ch*3
void main()
{
printf("%d",str(2));
}
输出结果为:6
例子4:// console.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h";
#define i 4
void main()
{
int a=4;
int str=i*a;
printf("%d",str);
}
输出结果为:16
1。简单的宏定义:宏名 字符串(与类型相匹配)后面不能跟有分号(;)
2。带参数的宏定义: 宏名(形参) 宏体(就是要做的事情)
例子1:// console.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h";
#define str(ch) ch
void main()
{
printf("%s",str("huhuoyun"));
}
输出结果为:huhuoyun
例子2:// console.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h";
#define str(ch) ch*3
void main()
{
printf("%d",str(2));
}
输出结果为:6
例子4:// console.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h";
#define i 4
void main()
{
int a=4;
int str=i*a;
printf("%d",str);
}
输出结果为:16