Sign in to follow this  

properties of "#define" in C++

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive just recently started going through tutorials, looking at sample code etc. Now, in one of the sample codes that ive seen, they use #define, I assume to define a method. It could be SDL for all I know, since thats the only other package the .cpp file imports. I have never seen this syntax before, anyone know what it does? if it does just define a method, why use it over void <method name>(<parameter>)? heres the sample code: #define PUTPIXEL(x,y,c) ((unsigned int*)screen->pixels)[(x) + (y) * (screen->pitch / 4)] = (c); ive tried to search google, but ive been unsusscesful since # does not register on the search engine

Share this post


Link to post
Share on other sites
Mike's resource might be helpful to you, but possibly a little over your head if you have never seen the syntax before. #define can be used in a couple of ways:
You can define an 'identifier' to represent a certain value, like this:

#include <iostream>
#define X 5

int main()
{
cout << X << endl;
}

The program will display 5 on the console

#define can also be used to define macros. A macro is a piece of code that has some of the functionality of a function :), but should not be confused with one:

#include <iostream>
#define R(x) x*2

int main()
{
cout << R(9) << endl;
}

The preceeding snippet will print 18 on the console. Be aware that macros can have bad side effects that are hard to detect, and should be used minimally, especially if you are coding in C++.

There are other ways that #define can be used, but I think that the example you showed was a macro. Hope that was helpful...

Share this post


Link to post
Share on other sites
#define is used to replace text in your source code before it gets send off to the compiler.

For example, with:


#define R(x) x*2

int main()
{
cout << R(9) << endl;
}




After the preprocessor gets through with it, all the compiler is going to see is this:


int main()
{
cout << 9*2 << endl;
}




Since it is just substituting text, unexpected things can happen. For example:


#define SIX 1 + 5
#define NINE 8 + 1

int main()
{
cout << (SIX * NINE) << endl;
}




This becomes:


int main()
{
cout << (1 + 5 * 8 + 1) << endl;
}




Since multiplication has a higher precedence than adding, this will print 42 instead of the expected 54.

Share this post


Link to post
Share on other sites
Notes:

  • The preprocessor language (which #define is a part of) is not C++. It is used in C++ programs, but is not itself C++ code.

  • The preprocessor is not intelligent. It essentially performs copy/paste operations.

  • Because the preprocessor is a non-intelligent text replacement tool, it does not respect scopes and has no notion of type rules. If macro names or #defined constants conflict with extant variable names, this will not raise errors; the preprocessor will blindly replace them.



Personally, I think that the preprocessor is useful for conditional compilation (#ifdef and its compatriots), and it is of course necessary to #include header files, but should otherwise be avoided as much as possible.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kryodus
Ive just recently started going through tutorials, looking at sample code etc.

Now, in one of the sample codes that ive seen, they use #define, I assume to define a method.

It could be SDL for all I know, since thats the only other package the .cpp file imports.

I have never seen this syntax before, anyone know what it does?
if it does just define a method, why use it over
void <method name>(<parameter>)?

heres the sample code:
#define PUTPIXEL(x,y,c) ((unsigned int*)screen->pixels)[(x) + (y) * (screen->pitch / 4)] = (c);

ive tried to search google, but ive been unsusscesful since # does not register on the search engine

i'll try to answer your question the best i can...
given the PUTPIXEL macro you gave, if you put that as a normal function that function would have to have direct access to the buffer.
so in actuality your function would like this:

void putPixel (int x, int y, int c, WORD* buffer) {
//code here....
}

the function would need to be able to get to the buffer. in a macro, as the others have said it's cut and paste. plus SDL has already defined several things for you (such as the struct screen). so as opposed to looking up the function, it just plugs the macro in.

other reasons could be (but not necssarily true): speed and less memory used.

hopefully, i hit upon a few true points. but as usual if anyone sees anything wrong or incorrect, please correct (but read carefully first)

Share this post


Link to post
Share on other sites
wow thanks guys, thorough and fast!
I think I got it down now, i was totally unaware that #<name> is actually not part of C++ but is another language.
gives me some new insight thanks! I have never met such a newbie friendly board for anything ever :P

I have real difficulty understanding the MSDN help page, as it defines terms using definitons im unfamiliar with. If I look up those unfamiliar terms, they're composed of definitions of other unfamiliar terms, recursivly repeat and then I get lost :)


Share this post


Link to post
Share on other sites

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this