C中的宏常量和常量变量有什么区别? [英] What is the difference between macro constants and constant variables in C?

查看:21
本文介绍了C中的宏常量和常量变量有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Possible Duplicate:
"static const" vs "#define" in C

I started to learn C and couldn't understand clearly the differences between macros and constant variables.

What changes when I write,

#define A 8

and

const int A = 8

?

解决方案

Macros are handled by the pre-processor - the pre-processor does text replacement in your source file, replacing all occurances of 'A' with the literal 8.

Constants are handled by the compiler. They have the added benefit of type safety.

For the actual compiled code, with any modern compiler, there should be zero performance difference between the two.

这篇关于C中的宏常量和常量变量有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆