Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Intermediate C/C++ - #define  (Read 37421 times)

0 Members and 1 Guest are viewing this topic.

Dilbert

    Topic Starter
  • Moderator


  • Egghead

  • Welcome to ComputerHope!
  • Thanked: 44
    Intermediate C/C++ - #define
    « on: March 27, 2007, 07:23:40 PM »
    The #define Keyword

    For those of you who are coding in C or C++ and have to cut-paste a lot of code, this may be a big help. Normally, you shouldn't cut and paste code - it leads to horrible misunderstandings. However, in the event that an action (a block of code) needs to be done a lot, #define is excellent for helping with that.

    How it works

    #define is a pre-compiler instruction that does a simple text replacement. That's it. For example if you had this:

    Code: [Select]
    #define BIG 512

    int arrayObjects[BIG];

    By the time the actual compiler began working with your code, it would see this:

    Code: [Select]
    int arrayObjects[512];
    The pre-compiler instruction, like comments, are stripped at this point, to save space.

    This is particularly handy if you need a block of code to be executed a lot. An example is a command prompt game I wrote the other week. I needed delays in my code so users would read the text line-by-line, as opposed to the bottom of a paragraph, which was my first inclination.

    I used this (#include <ctime>) to do that:

    Code: [Select]
    time_t x, y;
    time(&x);
    do
    {
    time(&y);
    }
    while((y - x) < 1.5); //1.5 second delay

    I needed this code to repeat after each line of text. I couldn't just do this:

    Code: [Select]
    time_t x, y;
            cout << "A line of text here.\n";
    time(&x);
    do
    {
    time(&y);
    }
    while((y - x) < 1.5);
            cout << "More text\n";
    time(&x);
    do
    {
    time(&y);
    }
    while((y - x) < 1.5);

    Doing this ad infinitum is painstaking, annoying, and hard to read or maintain. There are two ways to do this. One is to put the declarations time_t x,y as a global variable and place the while loop in a function, then I can simply call

    Code: [Select]
    cout << "Line of text\n";
    smallDelay();

    Assuming smallDelay() has the delay code, this works. But this is even neater, in that you can write as few as three letters each time (less is possible, but the point of this is clarity, so...). Here's an example:

    Code: [Select]
    #define PAU time(&x); \
    do \
    { \
    time(&y); \
    } ]
    while((y - x) < 1.5)
     
            cout << "Line of text...\n";
            PAU;
            cout << "Second line of text...\n";
            PAU;

    And so on.

    Possible problems with #define

    #define is useful, but it can lead to trouble. #define does not do any type casting, which means it does not make sure you are assigning proper values to an int, string, or other variable. You could do this:

    Code: [Select]
    #define BIG 512
    using std::string;
    int theInt = BIG;
    string theString = BIG; //Oops, meant to have 'char theString = "BIG";'!

    The compiler would see

    string theString = 512;

    An int cannot be assigned to a char[]. In fact, you will probably see an error message almost exactly like the previous sentence. Having quotes around "BIG" makes the intended assignment: The character array 'B', 'I', 'G', '\0'. (Strings end in null 0)
    « Last Edit: March 30, 2007, 02:55:55 AM by Dilbert »
    "The geek shall inherit the Earth."