LKBEN11191: Howto replace #define into const variables in C/C++ applications


This article has not been checked!

LKB | Created: 02/04/2020 | Version: 0 | Language: EN | Rating: 0 | Outdated: False | Marked for deletion: False

Author: Wim Peeters - Keskon GmbH & Co. KG


Symptom

You have an old version of some program and want to exchange defines into constants

Cause

This is by design

Solution

Replacing #define into const has a few advantages when the macro is used as a named constant. The most important one is the possibility to debug easier. The const symbol obeys the scoping rules and is type safe so your compiler will help you. Using define is widely used in old C programs.

Nowadays it is common practice to use const variables, enums, inline functions or templates instead.

Have a look at the following line of code:

#define KENN_06 "06"

this can be rewritten to:

const char* KENN_06 = "06";

or

const char KENN_06[] = "06";

After this change even your IDE will show you the value of the variable which is most rarely the case with defines.

For integers you will have the following:

#define TARIF 1

can be written as:

const int TARIFDAT = 1;

Defines can not always be exchanged into const. In this case the macro is being used as a function-like entity. A #define is parsed and replaced by its definition by the preprocessor, after the replacement the compiler can only see the work of the preprocessor which you cannot see in your debugger. A preprocessor does nothing but text replacement and does not know anything about the C/C++ language and The compiler does not know anything about what the preprocessor has substituted. Here are a few examples of defines which cannot be easily replaced by const because they are

used to define macros:

#define PTR pDef
#define OBJ pDef->p_Obj

or

#define VALUE(i) PTR##->aData [i]


Aviding the preprocessor has the following advantages:

- macros might produce hard to find side-effects
- macros have an own scopes
- macros have no type and are handled as pure text
- you cannot find the address of a macro
- debugging is easier without macros.

Especially avoid the preprocessor when it comes to:

- definitions of constants
- definitions of enums
- try to use an inline functions instead of #define when possible
- try to use templates instead of #define when possible

There are places where the preprocessor is doing a good job so we cannot eliminate it completely e.g. #ifdef, ...

About the Author

Wim Peeters is electronics engineer with an additional master in IT and over 30 years of experience including time spent in support, development, consulting, training and database administration. Wim has worked with SQL Server since version 6.5. He has developed in C/C++, Java and C# on Windows and Linux in different European countries and different European languages. He writes knowledge base articles to solve IT problems and publishes them on the Lubby Knowledge Platform where he is one of the most important contributors and the main developer.

Disclaimer:

The information provided in this document is intended for your information only. Lubby makes no claims to the validity of this information. Use of this information is at own risk!