views:

108

answers:

4

Hi All,

I want to define some constants in my C file.

Its assembly code like this:

Const1 = 0

Const2 = 0

IF Condition1_SUPPORT

    Const1 = Const1 or (1 shl 6)

    Const2 = Const2 or (1 shl 3)

ENDIF

IF Condition2_SUPPORT

    Const1 = Const1 or (1 shl 5)

    Const2 = Const2 or (1 shl 2)

ENDIF

Could you tell me the simplest way to implement this?

And it should be flexiable enough because the number of both my constants and conditions is over 10.

After seeing the first 3 guys answers, I guess I need to explain more.

What I want to know is how to redefine my constant based on its previous value.

+2  A: 

You can do so using preprocessing directives:

#if Condition1_SUPPORT
    #define Const1 (1 << 6)
    // ...
#elif Condition2_SUPPORT
    #define Const1 (1 << 5)
    // ...
#endif

To address the edit to the question: you can't redefine a macro based on its previous value. A macro can only have one value at a time and its replacement list is only evaluated when it is invoked, not when it is defined. For example, this is not possible:

#define A 10
#define A A + 10

First, it is an illicit redefinition of the macro: when the second line is handled, A is already defined as a macro, and so it cannot be redefined with a different replacement (you have to #undef the macro name first).

Second, were this licit (and many compilers do accept it), the second line, when invoked, would evaluate to A + 10, not 10 + 10 or 20 as you want: by the time the second macro definition could be invoked, the first definition no longer exists.

You can, however, use different names, like so:

#define INITIAL_A 10
#define A INITIAL_A + 10

You should consider getting one of the introductory books from The Definitive C Book Guide and List; any of them would cover what can be accomplished using the preprocessing directives in some detail.

James McNellis
You missed the Or operator....And my compiler(VC7.1) will prompt error: macro redefinition
Celia Zou
@Celia: You can't redefine the macros; they have to be uniquely named (so, for example, in your code, the first two lines where you assign zero to the two variables, those two variables would need different names from `Const1` and `Const2`).
James McNellis
I know I can't redefine macro..this is why I came here...I just want to get a accumlation value for my constant.
Celia Zou
+1  A: 

You can use pre processor macros to conditionally define a constant variable, like

#if SOME_CONDITION
    const int my_constant = 10;
#else
    const int my_constant = 5;
#endif
Akusete
Note that in C, [a `const` object is not really a constant.](http://stackoverflow.com/questions/1674032/static-const-vs-define-in-c/1674118#1674118)
James McNellis
If your saying that a const pointer's value can still be modified I agree, although 'const int' is effectively an immutable value no?
Akusete
A: 

In C, you would use preprocessor macros to accomplish that:

#ifdef COND1_SUPPORT
#define CONST1 CONST1_VAL1
#define CONST2 CONST2_VAL1
#elif COND2_SUPPORT
#define CONST1 CONST1_VAL2
#define CONST2 CONST2_VAL2
#endif
Pablo Santa Cruz
A: 

You can't redefine the value of a constant once you assign it -- if you could, it wouldn't be a constant, would it? Since it looks like you're trying to set various bits in a constant based on preprocessor flags, you could #define separate constants for each condition's contribution, then build the final value at the end:

#if Condition1_SUPPORT
#  define Const1_Cond1_Bit (1 << 6)
#  define Const2_Cond1_Bit (1 << 3)
#else
#  define Const1_Cond1_Bit (0)
#  define Const2_Cond1_Bit (0)
#endif

#if Condition2_SUPPORT
#  define Const1_Cond2_Bit (1 << 5)
#  define Const2_Cond2_Bit (1 << 2)
#else
#  define Const1_Cond2_Bit (0)
#  define Const2_Cond2_Bit (0)
#endif

#define Const1 (Const1_Cond1_Bit | Const1_Cond2_Bit)
#define Const2 (Const2_Cond1_Bit | Const2_Cond2_Bit)

You can then #undef all the intermediate constants, if namespace pollution is a concern.

Paul Kuliniewicz
So far, your answer is the most flexiable...It seems no other way.....
Celia Zou
I forget something..Thank you very much~
Celia Zou