1

I want to redefine the value of a macro constant on a certain condition in my C source code. I don´t be able to use conditional preprocessing directives for defining LEN_OSG because whether it is redefined with a another value or not is decided at run-time.

What I already have is:

if(condition)                      // proof condition for replacing the value in LEN_OSG.
{
   if(LEN_OSG != CBS_LEN)          // check if LEN_OS already has the value in CBS_LEN.
   {
      #undef LEN_OSG               // undefine macro LEN_OSG by using `#undef` directive.
      #define LEN_OSG CBS_LEN      // recreate the macro LEN_OSG with new value.
   }
}

Does the C syntax allow another way to abbreviate this instead of to using the #undef directive and thereafter recreating the macro with a new value by another #define directive (in just a single line)?


Desired solution would be something like that:

if(LEN_OSG != CBS_LEN)          // check if LEN_OS already has the value in CBS_LEN.
{
   #redefine LEN_OSG CBS_LEN    // redefine LEN_OS with another value in one line. 
}
7
  • 1
    No. All preprocessor directives (#define, #if, ...) have disappeared when the compiler itself starts to run. Commented Mar 10, 2020 at 12:24
  • 2
    You are mixing compile-time and run-time stuff here. Your ifs are evaluated at run-time by your program and the target's processor, but the preprocessor lines are evaluated at compile-time by the compiler and the host's processor. -- What do you really want to achieve? Please show us a minimal reproducible example. Commented Mar 10, 2020 at 12:24
  • 1
    It's impossible to redefine a macro at runtime. C scopes do not affect preprocessor directives. Commented Mar 10, 2020 at 12:24
  • @pmg: Preprocessing is integrated into GCC and Clang, so the preprocessing directives are certainly still present when the compiler starts, unless a user explicitly requests a separate preprocessing execution. It is even possible to get phase 7 (semantic analysis) error messages from code containing an #error directive, thus proving that some traditional compilation is performed before phase 4 (preprocessing) is complete. Commented Mar 10, 2020 at 12:40
  • @thebusybee Idiotic mistake from me. I edited the question, but my issue still remains - I´m asking for a way to abbreviate the redefining of a macro without using #undef and #define. Commented Mar 10, 2020 at 13:01

2 Answers 2

5

Your code does not work as you expect. #define is executed at compile time, so you must use compile-time #if with it. Otherwise it is always executed.

Also, there is no #redefine, so two commands are needed.

If you have a runtime condition, use a normal variable and change its value during runtime.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I didn´t compiled it before - sorry for being dumb - but the case I´m asking for still remains. I´ve edited my question accordingly.
@RobertSsupportsMonicaCellio See my answer: "Also, there is no #redefine, so two commands are needed."
3

You are trying to mix runtime code execution with preprocessor directives, i.e. things which have an effect even before compiling.
That does not work.

1 Comment

Yes, that was mistake. I apoligize for being so idiotic. I´ve edited the question, so that my intention comes clear and valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.