Skip to main content
GameDev.net gamedev.net
🔒 Locked

#define

Started by CodeZ Dec 13, 2002 at 8:49 PM 4 replies 200+ views
Original Post
CodeZ
CodeZ
What happens when you are using the define keyword in c++? Where are the value stored and how can the compiler find a #define''d keyword? Let say I do something like this:
  

#ifnef _MY_HEADER_FILE
   #define _MY_HEADER_FILE
#endif

  
How can the compiler now, the second time it compile the text, that _MY_HEADER_FILE already is defined? Thanks.
Zipster
Zipster
My guess is that the compiler has an internal way of storing what''s defined at any given time. You can add things that are automatically defined from the beginning via the project options, and as the compiler encounters these define statements, it adds it to the list, as well as what text is will replace with what. In the case of multiple header inclusion, we don''t use it to substitute stuff.
billybob
billybob
they are preprocessor directives (?) so they don''t get compiled, they change what code gets compiled among other things, so you can prevent headers from being included more than once like you have there.
Doc
Doc
When you tell it to compile, the source gets sent through the "preprocessor" before heading to the actual compiler. This preprocessor goes through the source doing certain things according to what "#" keywords are in the source. In the case of #define it keeps track of the defined keyword and when it next spots that keyword it replaces it with whatever you set in the #define. Specifically how it keeps track of the keyword, I don''t know - probably some standard type of list. Another use for the preprocessor is the #include keyword, which literally replaces the #include statement with the whole contents of the file indicated.

Once this bit''s done, the processed source then heads to the compiler. The compiler itself has no idea of these things, and does not really care.

Peace,
Doc
Zipster
Zipster
Right, I didn''t mean the compiler does this, but I couldn''t think of preprocessor off the top of my head.
CodeZ
CodeZ
Thank you Doc! That clear things up.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.