Can anybody tell me what is the difference between macros and constants?

Recommended Answers

All 4 Replies

A macro is something that is expanded by the compiler's preprocessor. A constant is like a normal variable but whose value can not be changed, nor is a constant affected by the preprocessor.

macro's cannot have a data type wherein constant must have a data type.

Macros definition is automatically expand by the compiler..
It works just like functions..
There are no. of benefits using macros over constant...
for ex- you have used the constant pi many times in your program and now you want to change the value of pi.. with macros you have to change only where you define the value but with constants you have to change everywhere in the program..

This isn't really a general computer science question, it's more of a language-specific one. The definition of 'macro' and 'constant' varies from language to language. In some, constants aren't constants, and in others, macros aren't macros.

And in some, macros must "have a data type" and in some, constants don't have to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.