#define vs int/char/float etc...

Title says it all: what is the difference between using #define to create variables rather than float or int.

This is in RobotC.


#define is more of a constant vs the float or int which can change

for example
int motorspeed;

can change over the course of the program as the motor speed changes but #define would be used to set a value for the whole duration of the program

get some other opinions first though to make sure I’m conveying the info right. good luck

So the best way to think of #define is that the compiler before reading the code replaces all the “NAME” with “value”. This can allow some useful things but it does not allow the value to be changed. Using a #define is identical to putting a number in each of the locations where you put the name. Here is a good explanation and sample program showing some of the useful things that can be done with #define.


#define fullspeed 127
motor[drive] = fullspeed; // will always be 127

int fullspeed;

if (sonic sensor == close_to_object) then
fullspeed = 64;
else fullspeed=127; // when driving and close to wall slow down.
motor[drive] = fullspeed; // safest speed

Like Tabor said, defines for things that are constants across the program, variables for things that can change.