#define 与 const 区别
程序1
#include "stdafx.h"
#define M 10
int main(int argc, char* argv[])
{
char cTest[M];
printf("Hello World!\n");
return 0;
}
程序2
#include "stdafx.h"
const int B = 10;
int main(int argc, char* argv[])
{
char cTest[B];
printf("Hello World!\n");
return 0;
}
上面的程序那个好一些。
问题点数:20、回复次数:3Top
1 楼kunp(一天一小步)回复于 2006-02-07 17:33:28 得分 10
就你上面的程序而言
没什么分别
#define M 10
是宏定义,表示以后程序中出现 的 M 都将被替换为10,文本级的替换
这里的M没有类型,不是变量,不占空间
const int B = 10;
是定义常量,表示定义了一个全局变量B,类型为int,值为10。
#define这种宏定义,能不用就不用,这是大师说的Top
2 楼runall(龙行天下)回复于 2006-02-07 17:58:32 得分 5
#define 没有类型检查,而const有类型检查Top
3 楼ericqxg007(还有很多东西要学(卡卡一米阳光))回复于 2006-02-07 20:13:09 得分 5
恩 同意楼上说的
编程时应尽量使用const来代替#defineTop
相关问题
- const 的区别
- static readonly与const有何区别?
- ???char*,const char*有什么区别???char*与LPSTR,const char*与LPCSTR有什么区别??
- 他们有何区别?const char* str="abc" 与 const char str[]= "abc"
- #define定义一个量和const定义的常量有什么区别?
- #define定义一个量和const定义的常量有什么区别?
- const int a=4与int a=4有什么区别??Convert.ToDecimal与decimal.Parse()又有什么区别???
- define和const
- 函数的返回为const与不加有什么区别么?
- const int a=3 和const a=3有何区别?




