0

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

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. (No memory required)

So, people use it as a method to define constants instead of the syntax: const int num = 1;

Is this a good habit? Is the MACRO set to do another things additionally to #include and #define?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
nEAnnam
  • 1,246
  • 2
  • 16
  • 22

2 Answers2

3

Macros can be used for other reasons than just defining constants. Nowadays, macro usage is generally frowned upon unless there are absolutely no other options available.

Debugging complex code with macros littered throughout can cause you more than one headache.

From GCC docs:

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro.

Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
3

Consts are generally preferred for several reasons. For example, they are a lenguage construct (not an external value processed by the preprocessor). Also, they contain type information, so they help the compiler to detect compiling errors, etc. In most cases, you could say they are equivalent, but const is "semantically more correct".

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87