Alex Rivera | Logout

Shall I prefer constants over defines?

Asked 2010-02-22T00:55:01.073
50

In C, shall I prefer constants over defines? I've reading a lot of code lately, and all of the examples make heavy use of defines.

Edit
Report

1 Answer

12

Constants should be preferred over defines. There are several advantages:

  • Type safety. While C is a weakly typed languaged, using a define loses all of the type safety, which will allow the compiler to pick up problems for you.

  • Ease of debugging. You can change the value of constants through the debugger, while defines are automatically changed in the code by the pre-processor to the actual value, meaning that if you want to change the value for test/debugging purposes, you need to re-compile.

answered 2010-02-22T00:58:48.530

Your Answer