![]() |
#define and #ifdef/endif
If I have code that says (example only)
Quote:
Thank you in advance, |
Re: #define and #ifdef/endif
That should not work. #define statements, #ifdef, #endif, and anything starting with the # symbol are preprocessor directives. Therefore, they are not runtime code - they just tell the compiler how to compile. They do nothing at run time.
|
Re: #define and #ifdef/endif
Code:
#ifdef PROG1If we #define PROG1, then it puts the "Hello, world" printf in. If we #define PROG2, then it puts the "FIRST Robotics" printf in. I used these for a few sensor values that differed from our practice to competition robot.. I'll post the code we used when I get a chance. |
Re: #define and #ifdef/endif
...Chris posted while I was typing this....
Short answer is that #define and #if switches are processed before the compiler ever sees the code. Therefore you can't do what you are trying to do. You'd be better off storing the program number into a variable and deciding what to do based on that Code:
enum |
Re: #define and #ifdef/endif
Quote:
However, the toggling of your #defines happens before you compile, not at runtime like it looks like Dan was suggesting. |
Re: #define and #ifdef/endif
Quote:
Dave, I am unfamiliar with the 'enum' command, what does it do? Assign values to the identities inside, ascending from the value assigned to the first identity?:confused: Again, thank you very much, :D |
Re: #define and #ifdef/endif
Quote:
Code:
//This is equivalent toSay that you have 3 steps to a process defined as #defines Code:
#define STEP_A 1Code:
#define STEP_A 1If your original code was written with enums Code:
enumCode:
enumIn the context that I used in my original response, #defines would have been just fine, I just chose to go with the enum because I wasn't too concerned with the values that the constants took on....I was more interested in having unique constants. You can find some more info about enums here |
Re: #define and #ifdef/endif
Ok, now, I have another question. Can I use 'enum' in a header, and just include that header in all files that I need the constants declared in enum?
Thank you, Dan |
Re: #define and #ifdef/endif
Yup, we do it all the time. Remember that when you do a #include that it is the same thing as copy/pasting the included file into your current file.
One thing to be careful of is that enums have scope, so you will want to be careful with the way you name your constants. If you ran the following Code:
enumCode:
A: 1 B: 2 |
Re: #define and #ifdef/endif
When using the scripting/navigation code provided by Kevin Watson, u would call a switch statement with the following parameter:
Code:
command_list[current_command].commandCode:
int check_switch_box(void) Code:
switch(check_switch_box()){}Code:
switch(command_list[current_command].command){}That should work, right? Since command_list[current_command].command really is just a value inside a struct. |
Re: #define and #ifdef/endif
Quote:
if(rc_dig_in01==1) { printf("Hello, world.\r"); } else if (rc_dig_in02==1) { printf("FIRST Robotics\r"); } |
Re: #define and #ifdef/endif
Quote:
It may be a little cleaner to do it in two steps Code:
new_cmd = check_switch_box();An even cleaner way to do it would be to get your program number up front and then use pass that in to your function. Code:
//Outside of autonomous loop.... |
Re: #define and #ifdef/endif
Quote:
Thanks again, |
| All times are GMT -5. The time now is 18:18. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi