5


I have a question about using #undef to redefine macros.
I have a file global.h which contains a number of #define-d macros. In the code that uses these macros, I find that the values that the macros hold are not generic enough. I want to redefine the macros to make them more generic. I wrote the following code snippet to do just that:

 std::cout << endl << "Enter pitch threshold:" << endl;  
 std::cin >> pitchT;  
 #ifdef PitchThreshold  
  #undef PitchThreshold  
  #define PitchThreshold pitchT   
  #endif  

My questions are:
Does using a #undef in this manner ensure redefinition of the macro across all source files, or is it local to the function where the above lines of code are written? What is the scope of the #undef and #define operators?
What can I do (apart from changing the macros in the file where they are #define-d itself) to ensure that the macro definitions are changed across all source files?
Thanks,
Sriram

Sriram
  • 10,298
  • 21
  • 83
  • 136

3 Answers3

11

#ifdef is a preprocessor directive, this means that it will be applied before your source code is compiled. It means that only the source code 'below' will be affected. If you run your source code through the preprocessor you'll be able to see the result. That will give you more insight in the workings of the preprocessor.

Sebastiaan M
  • 5,775
  • 2
  • 27
  • 28
  • but does the #undef operator have anything to do with the value it is redefined with? For instance, in the code snippet above, pitchT is an int that is local to the function where this redefinition takes place.when pitchT goes out of scope, does that affect the #define statement? – Sriram Nov 01 '10 at 10:20
  • 3
    You can't change the value of a macro at run-time. The preprocessor only does textual substitution, so the next time `PitchThreshold` is used in your code, it is replaced by `pitchT` (the variable name, not its value). – Fred Foo Nov 01 '10 at 11:11
  • that was the answer i was looking for! thanks larsmans! i thought that #define would replace pitchT with its value. – Sriram Nov 01 '10 at 11:41
2

The scope of the #undef operator is the whole file after it's called. This includes all files that include it (because the preprocessor just chains the files together.) Because it's part of the preprocessor it doesn't have weird things like scope.

OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
  • but does the #undef operator have anything to do with the value it is redefined with? For instance, in the code snippet above, pitchT is an int that is local to the function where this redefinition takes place.when pitchT goes out of scope, does that affect the #define statement? – Sriram Nov 01 '10 at 10:21
  • All the `#define A B` operator does it replace all instances of A with B. You can do `#define INFLOOP while(true){}` and the preprocessor will happy replace it. The preprocessor simply performs operations on text, it's completely agnostic towards the C language. In the context of the code you posted though, your question makes no sense because there isn't any text that is being replaced. "PitchThreshold" doesn't appear in your C code. – OmnipotentEntity Nov 01 '10 at 10:26
  • PitchThreshold is the "name" of a macro defined in a file called global.h but redefined here in a file called sentence.C. If pitchThreshold does not appear, it is because I have not posted the entire code. – Sriram Nov 01 '10 at 10:42
0

any #define macros is not local, nor global variable, but a "constant-like" expression, so, even if some DEFINED_VALUE will be #undefined anywhere after - this un-definition will be scoped from its place to the end of this source-file.

it has pretty straight-forward logic, but may confuse due to definitions being allowed both inside and outside of any functions.

Answering last question:

We can't really comfortably redefine a macros — in mean of global scope. Uncomfortable way is to place all the code in a single file.

What can be done in practice is kinda default thing — new variable declaration (at the top of the main() function) with similar naming to use it in parallel with defined value.

Mk Kvease
  • 41
  • 5