23653

How can I undef and def preprocessor value at run time

Question:

I have below code. Where I would like to undefine the PORT after 5 iterations. But when i am running this program the output comes like

PORT MOB PORT MOB...10 times

So, if I would to change it at run time how can I do this. I have declared PORT inVS project->Propoties->C/C++->Preprocessor

int main() { int i; for(i = 0; i <= 10; i++) { #ifdef PORT printf("PORT\n"); #endif if(i == 5) { #ifdef PORT #undef PORT #endif #define MOB 1 } #if MOB printf("MOB\n"); #endif } return 0; }

Answer1:

The pre-processor (as implied by the name) does what it does as the <em>first</em> step in compilation (or, depending on your viewpoint, before compilation completely). Things that happen at run-time can't control things that happened previously. You can't change anything done by the pre-processor at run time.

To get the desired effect, you could change from trying to use a pre-processor definition to using a normal variable, or normal flow control.

for (int i=0; i<5; i++) printf("PORT\n"); for (int i=0; i<5; i++) printf("MOB\n");

...or:

for (int i=0; i<10; i++) printf(i < 5 ? "PORT\n", "MOB\n");

Although you didn't mention them, I'll add that C++ templates have much the same limitation, so trying to do this with them is <em>likely</em> (though perhaps a little less certain) to turn out essentially similar. Templates do give you <em>more</em> ability to inspect and modify their results in accordance with the rest of the program, but only a little. Ultimately, template parameters need to be compile-time constants so the templates can be resolved at compile time.

Answer2:

This is impossible. Preprocessor directives are part of compilation. There's no such thing as a runtime preprocessor, you can't change directives in runtime. A binary file does not know about preprocessor directives.

Answer3:

This can't be done.

The preprocessor runs as one of the first passes on your source file before it is compiled.

You can not manipulate the preprocessor at runtime of your program. Use normal variables instead.

Answer4:

You can't. The preprocessor is run as a separate step <em>before</em> the compilation. There is no way to do it runtime.

You have to do it using normal if and else handling with variables.

Recommend

  • Android Inside Custom ListView, TextView Weight property not working
  • Java ProcessBuilder: space within quotation marks
  • Angular and Jasmine: How to test requestError / rejection in HTTP interceptor?
  • cassandra getendpoints with partition key has space
  • Can std::cout work with UTF-8 on Windows?
  • rabbitmq-erlang-client, using rebar friendly pkg, works on dev env fails on rebar release
  • How do you prove a function works?
  • XML file with DTD displays fine in web browser after invalid change
  • What's the point of meck:validate?
  • How I fix a “GADInsterstitial.h” file not found… but it is in my list?
  • ICU support in a 32-bit build of Qt5 with the VS2012 compiler causes Qt5 build failure; Webkit is al
  • Visual C++: Exporting decorated function name in a def file
  • Sobel filter in x direction in CUDA
  • Drop-down menu is pushing the navbar down. How to avoid it?
  • QML cannot assign assign shared_ptr to [unknown property type]
  • Reusing the CQ5 Form into the mywebsite components is not showing up the End of the Form section
  • Fatal error: Call to a member function fetch() on a non-object?
  • PostgreSQL primary key auto increment crashes in C++
  • Flot returns incorrect x value (mm/dd/yy - date) after zoom for stack bar chart
  • 'Enemy' was not declared in this scope?
  • Messed up characters in webpages (especially social media)
  • Is there a way to choose which files are displayed to the user via the standard OPENFILE dialogs?
  • Why cout is producing no output on Code Blocks?
  • Setting WPF Window Background to Resource Dictionary Brush User Setting
  • PayPal API Listener Website Payments Standard URI
  • redirect_to root_url and return unless @user.activated
  • MeeGo Handset Emulator not starting on Windows 7
  • How to print columns containing value
  • Undefined references when compiling gSOAP client
  • Sencha Touch 2.0 Controller refs attribute not working?
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • What is the “return” in scheme?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Delete MySQLi record without showing the id in the URL
  • Memory offsets in inline assembly
  • How can I remove ASP.NET Designer.cs files?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?