Limit scope of #define

Posted by Ujjwal Singh on Stack Overflow See other posts from Stack Overflow or by Ujjwal Singh
Published on 2012-09-28T13:36:33Z Indexed on 2012/09/28 15:38 UTC
Read the original article Hit count: 228

Filed under:
|
|
|
|

What is the correct strategy to limit the scope of #define and avoid unwarrented token collisions.

In the following configuration:

Main.c

# include "Utility_1.h"
# include "Utility_2.h"

VOID Utility()           // Was written without knowing of: Utility_1 & Utility_2
{
    const UINT ZERO = 0;
}

VOID Main()
{
    ...
}

// Collision; for Line:5 Compiler does not indicate what replaced

Utility_1.h

# define ZERO "Zero"
# define ONE  "One"

BOOL Utility_1();

Utility_2.h

# define ZERO '0'
# define ONE  '1'

BOOL Utility_2();

Utility_2.c

# include "Utility_2.h"

BOOL Utility_2()
{
   // Using: ZERO & ONE
}

//Collision: Character Literal replaced with String

{Edit} Note: This is supposed to be a generic quesition so do not limit yourself to enum or other defined types. i.e. What to do when: I MUST USE #define

Please comment on my proposed solution below.. _

© Stack Overflow or respective owner

Related posts about c++

Related posts about c