#define

This is a discussion on #define within the C++ Programming forums, part of the General Programming Boards category; Can someone explain this to me. I see it being used all the time, I am brand new to the ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    19

    Angry #define

    Can someone explain this to me. I see it being used all the time, I am brand new to the language. Is it used to declare a certain type of a variable or constants or?

    Can you use examples if possible, it's greatly appreciated.
    jotun

    n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    think of #define as using constants, except they are can hold any type of data and can even hold operations.

    For example:

    Code:
    #define MY_NAME  "Jeff"
    // Is basically the same as:
    const char name[]="Jeff";
    Code:
    #define MY_AGE  16
    // Is the same as
    const int age=16;
    those are the most basic examples of #define, but you can also use #define for things called "macros" which are basically shortened ways of calling complex operations of code.

    For example:

    Code:
    #define CLEAN(data) { if(data) { delete [] data; data=NULL; } }
     
    int main()
    {
    	  int* dynamicMem=new int[34];
     // We create the dynamic memory
     // Then, instead of having to call of the stuff to delete the memory, we can just use CLEAN
    	  CLEAN(dynamicMem);
     
    	  return 0;
    }
    If you care to know what goes on in the background, basically during preprocessor-phase of the compiling, the compiler reads in all of these #define statements and remembers what they are, then, when you use them in your code, it in a sense "copies" whatever you had to the right of the definition in to your code.

    So in our example above, it would replace that CLEAN(dynamicMem) macro with the code like so:

    Code:
    CLEAN(dynamicMem)
    // vvvvvvv
    // becomes
    // vvvvvvv
    {
    	 if(dynamicMem)
    	 {
    		   delete [] dynamicMem;
    		   dynamicMem=NULL;
    	 }
    }

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Jotun
    Can someone explain this to me. I see it being used all the time, I am brand new to the language. Is it used to declare a certain type of a variable or constants or?

    Can you use examples if possible, it's greatly appreciated.
    #define will define something before compilation takes place, so constants are one use.

    ex)
    #define PI (3.14)

    ....

    float thing = 3 * PI;


    // at compile time the "defined" value will be substituted in, ie) the compiler will see

    float thing = 3 * (3.14);


    #define can also be used for flags when including files. at the top of a header one might have something like

    #ifndef MYFILE_H
    #define MYFILE_H

    // header

    #endif

    This is to ensure the file is only included once. The first time the file is encountered, MYFILE_H is not defined, so the next line is processed which will define MYFILE_H. If the file is included again, MYFILE_H will already be defined so the file is not processed twice.

    These are just two examples of uses of #define. There are many others as well

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    19
    Thank you, I appreciate the help guys.
    jotun

    n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Just because you can do something doesn't mean you should, though. It is bad practice to define constants using #define. Use the keyword const instead. Macros had greater usefullness in C, but since C++ has function templates, those are generally better to use than #defines. Remember that the preprocessor knows nothing about C++ or type checking. It will hurt you ruthlessly, and not raise the warnings it should.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 11:52 AM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  5. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21