Macro Definitions

One thing I have never understood in RobotC is Macro Definitions? Is there any reason for their existence beyond as an alternative to globals? Thank you very much for all of your help! :o

One example is to use them as a type of function. For example, you can create a macro to compute the minimum of two values:

#define min(a, b)  ((a) < (b) ? (a) : (b))

It can be useful for small functions such as this one, but should not be used for larger functions.

Well macro definitions in general are a C thing kind of grandfathered in from assembly and make plenty of sense / are used in every major C/C++ program.

The one question I can think of is why wouldn’t you just use a variable?


#define CODY 1337


const int cody = 1337;

Both can be used to do certain tasks, but they both go about it in different ways and ultimately do different things.

The #define stores 1337 in the executable wherever CODY appears. This adds 4 bytes of information carried by your program for every CODY. Which could definitely add up and may not be the right idea for certain applications.

On the other hand the const int takes 4 bytes of RAM, which might not matter, but it requires a memory lookup every time you want to use it. Whereas the #define information is loaded in with the instruction data, similarly to how literals are handled.

I could have just as easily written the following…

#define CODY (1000 + var1 + var2 + var3)

Which is not simply data, it’s an expression which will be text replaced whenever CODY shows up just before compilation.

WHICH I SUPPOSE you could write as follows.

inline int cody(var1, var2, var3) {
	return 1000 + var1 + var2 + var3;

But in this case, the implementation changes:

int var1 = 300, var2 = 30, var3 = 7;

int foo = CODY;

int bar = cody(var1, var2, var3);

But at least the inline forces the code to be inserted into the instruction stream.

When you get right down to it, marcos are just another tool.

Who doesn’t want more tools?


There’s an old post showing some use of macros here.
RobotC programming tips Post #12

A macro is used by the pre-processor. Your code is examined and a substitution is made before the code is compiled. I use them most of the time to remove “magic numbers” from the code, they don’t have to be integers but can be anything at all, any type, the pre-processor just does a find and replace.

A “const int” may often not use RAM, it’s constant, so the linker generally puts them in non-volatile memory.

Also, #defines are nice because, unlike functions, you can put the whole thing at the beginning of your code so they are easy to modify/change.

Defines are very powerful but also dangerous:

#define while if

Someone (besides James or Cody:)) guess what this does

Code never runs, Freshmen freak out, reset cortex, vex keys, etc.

They post on the forum, but only posts the body of the code, so no one can notice that it is wrong. Finally, they trash their beautiful sensor code for far simpler time based code, and never realize that it was that simple of an issue.

On the other hand, this would be great to mess with people you know very well :slight_smile:

Or how about

#define operatorControl autonomous :slight_smile:

I prefer #define ; :

Many errors, much fun. :slight_smile:

OK, Dr. Evil.