#define

This is a discussion on #define within the C++ Programming forums, part of the General Programming Boards category; What is #define? I've seen people use it like #include but what does #define do? Thanks -Chris...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up #define

    What is #define? I've seen people use it like #include but what does #define do?

    Thanks
    -Chris

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    5
    hi,
    #define is a preprocessor directive. In a simple term it is some thing like keyword which hints the preprocessor (the earlier phase of compilation.)

    The preprocessor replaces the string which is defined with #define with its value.
    That is

    #define OK 1

    if(m_answer == OK)
    {
    }

    than OK will be replaced by 1 before compilation. Thus you can make your program more readable and understandable by using descriptive strings for numbers.

    Also #define is used to define a macro. The macro is a small piece of code which is used more than once in the program.
    Before compilation this code will be placed wherever this macro has been called.

    #define SQURE(x) x*x

    b=SQURE(3); // code
    This code will be converted automatically to
    b=3*3; //before compilation

    If you make the function for this purpose. The compiler will generated many more instructions for this small function(to do sequencing purpose) , which will be negative for performance of the program.

    But make sure that don't use macro for more than 2 to 3 statments of code because it doesn't make sense in terms of programming style.

    Refer any C book which has covered Preprocessor directives chapter. You will get many more help for that..

    Bye..
    jatan (porecha_jp@hotmail.com)

  3. #3
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    It's a macro. With #define you can make a word represent something, for example a number:
    Code:
    #define MAX 5
    
    int main()
    {
        int array[MAX];
    
         for(int i=0; i< MAX; i++)
        {
           array[i] = i;
           printf("array[%d] = %d", array[i],array[i]);
        }
        return 0;
    }
    Here MAX is the same thing as 5. Now, say you wanted a bigger array. Say you wanted an array of 10 elements instead of 5 elements. In that case the only code that you would have to change in your program would be:

    #define MAX 10
    I compile code with:
    Visual Studio.NET beta2

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    but that looks just like a declaration of a variable but without the type (int, double ect.)

    Is there anything difference?

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Variables take up space in your program at run time. Items which have been defined using #define are processed by the pre-processor at compile time, hence use no resources at run time.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    but that looks just like a declaration of a variable but without the type (int, double ect.)

    Is there anything difference?
    Sure there is a difference, it is faster due to Adrians explanation, and also if you had to use a variable to represent something such as:
    Code:
    double Pie = 3.14;
    That varable can accidently get changed during calculations due to human error unless you wrote:
    Code:
    const double Pie = 3.14;
    Another thing is that it has global scope. It can be used in any function and does not have to be passed as an arguement. It can make your program easier to read too. It's more organized. But other than that, it's nothing special.
    I compile code with:
    Visual Studio.NET beta2

  7. #7
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Use of define is not encouraged as much in C++ as it is in C due to the lack of type safety - constants and inline functions should be used instead. However, there are situations when using define is much simpler.

  8. #8
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Okay, thanks zen. Indeed I was explaining it from the 'C' perspective. I'll keep this in mind as I continue to study C++.
    I compile code with:
    Visual Studio.NET beta2

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    And beware. Definining something like this might happen:

    #define func1(x) Afunction(x *x); <--Notice the semi-colen

    when used like this:

    func1(3);

    Expands to:

    Afunction(3 *3) ; ; <--- Notice double insertion of semi-colen

    I know this may sound stupid, but all the same, like array indexing accidents, sometimes this happens...



    ITSA
    Socket Library!

  10. #10
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    Much worse, of course, is the related semicolon problem for #define constant values.

    Code:
    #define PI 3.141593;
    
    /*...*/
    
    cos (radval * PI * 2);
    is interpreted by the compiler as

    Code:
    cos (radval * 3.141593; * 2);
    And of course, the result is a compiler error. But at least that's easy enough to catch.

    So, (almost) never use semicolons with #define. (I've made that mistake way too many times.)

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > #define SQURE(x) x*x
    The compiler will not diagnose the problems caused by doing something like
    &nbsp; y = SQURE( x + 1 );

    Which would expand into
    &nbsp; y = x + 1 * x + 1;
    which is almost certainly not what you meant, but it's perfectly valid code.

    And the utter chaos of trying
    &nbsp; y = SQURE( x++ );

    C++ inline functions are much better - you don't have all those surprises which will bite you if you use a macro in some unexpected way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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