Advertisement

How does #define work?

Started by October 22, 2000 04:59 PM
14 comments, last by ryanc 24 years, 2 months ago
I''m learning C++ and need some help. I am having trouble finding out how and when to use #define. I need the know the syntax for declaring it and I also need to know when to use it why. RyanC --------------------------- Games are only as good as you make them...
---------------------------<code>!ERR(0493): An error occured in cerebral cortex at location 0x00f230f8!: Buffer Overflow
All that define does is basically replace something with a word.

Example:

    #define NAZRIX 1    


Now anywhere you''d use NAZRIX in the code, the compiler would see it as a 1...Just lets you use familiar names for numbers or other things.



""You see... I'm not crazy... you see?!? Nazrix believes me!" --Wavinator

"All you touch and all you see, is all your life will ever be." -Pink Floyd

Need help? Well, go FAQ yourself.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Advertisement
#define can be used for an endless amount of things.
It is basically to an instruction to the compiler to replace your symbol or expression with the defined value anywhere you have placed it in the code. Here are a few examples.


#define BUFFER_SIZE 10

/*....later on in the code any where you place the BUFFER_SIZE will be replaced with a value of 10 this way if you wanna change it later you can easily without going through the whole code*/

//EX 1

int Buffer[BUFFER_SIZE];

for(int i=0; i{
Buffer = i;
}

/* You can also use it with other keywords to make sure things are only declared once or platform specific code */

//EX 2

#define WINBLOWS
//#define LINSUX


#ifdef WINBLOWS
//WINDOWS SPECIFIC CODE HERE
#endif

#ifdef LINSUX
//DO LINUX CODE HERE
#endif

/*ECT ECT you should check it out un the help of your compiler for more info*/










Check out my project @ www.exitearth.com

you can also have macro functions
e.g.:

    #define SQUARE(a,b) (a * b)int num = SQUARE(num1, num2);//after preprocessing(thats where the compiler replaces defines//with the actual values), it looks like this:int num = (num1 * num2);    


quote: Original post by Quantum

you can also have macro functions



Or better, "sort-of" inlined functions, that neccesarily CAN''T be recursive. :-)


Bye,

Karmalaa


---[home page] [[email=karmalaa@inwind.it]e-mail[/email]]
lol.. i''d like to try making a recursive macro.... does the compiler complain? i cant imagine it trying to expand that
Advertisement
#define could often be replaced by

// for variables :
static const int NB_CASES = 10;

// for function
template
inline T MAX(T& a,T& b){return (a>b)?a:b;}




good luck !!!
GLOUK
good luck !!! GLOUK
You should go overboard with ()''s in #define''s.

Consider your example:
#define SQUARE(a,b) (a * b)

(why''s it called square and not mult? hehe)

If we call this like:

x=SQUARE(3+4,2);

It expands to:
x=(3+4 * 2); // 11

You probably intended to get (3+4)*2 which is 14..

The solution is to set up your define like this:

#define SQUARE(a,b) ((a) * (b))

which will expand to:

x=SQUARE((3+4) * (2)); // 14

Hope this saved some of you some evil unintuitive bug tracking .

aakks





this would be something you could figure out by cruising the help files on your compiler. I''ve learned a lot that way.

Are you even trying to be intelligent?
'cuz if you are, it ain't workin'

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Anyone else ever wonder what could be achieved if the pre-processor had a Y combinator?
It''s probably just me.

This topic is closed to new replies.

Advertisement