How to use #const in RobotC?

I would like to create variables that are constant and couldn’t be changed. One, what’s the difference between using #const and const [var type] “var name” = (var value);. Two, how do you use each way. Three, what situation would I use one over the other. I’m using RobotC as well.

I’ve never actually seen #const preprocessor directive used before, and it seems that my machines MSVC C++ compiler does not recognize it. However, it might be RobotC specific or require some flags that I am unaware of, so maybe @jpearman can give you more info on that.

Perhaps you meant #define. If so, #define creates a macro which is replaced by the specified value whenever it occurs in your program. This can be used for more than variables, ex. also for statements. A const is a modifier on a type variable that makes it immutable.

They are different things, but have a somewhat similar effect on a broad scale. Which one you need is up to you, and will vary from situation to situation. In general, its probably best to stick to const if you don’t know what you need. Const variable is a proper type that can be checked by a compiler whereas define can cause all sorts of issues when not used correctly.

I would also urge you to read this for more detail: https://stackoverflow.com/a/1637367/4484072

As for how to use them:


#define MACRO_NAME value 
const type name = value

edit: checked it with RobotC, also doesn’t recognize #const.

No such thing as #const in ROBOTC as far as I know.

We do support the const keyword, this is a hint to the compiler, how the compiler uses the declaration depends on context, sometimes the behavior is similar to a pre-processor declaration (ie. #define) sometimes the value will be stored in a different part of memory we call the “constants pool”, usually things like strings are stored there.

Thank you for responding. I will use const type name = value;