Constants or #define

This is a discussion on Constants or #define within the C++ Programming forums, part of the General Programming Boards category; Which is the best method of storing data that doesn't change? Some people use #define and others constants. It would ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    Question Constants or #define

    Which is the best method of storing data that doesn't change? Some people use #define and others constants. It would be good to know when to use const's or #define's.
    Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    In C++ use const.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    a #define is part of the preprocessor which isn't included in the compiled code. let's say you #define PI 3.1415926535876. before anything else happens, your preprocessor will replace every 'PI' with 3.1415926535876. then your compiler will do its work as usual.

    a const is a constant variable. all the rules of c and c++ apply to it.

    the biggest advantage to using const variables is type checking. errors are much easier to spot using a const variable than a #define. #defines also tend to mess up in unusual expressions.

    in short: use a const. it gives your compiler extra information that can sometimes prevent errors (or at least make them more obvious.)

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Thanks for your replies. So when I make a call, to include a #define in my code is a local variable created and then destroyed when the statement has called. Or is the variable destroyed when the program exits? e.g.

    Code:
    #include <iostream.h>
    
    #define MAX_NUM 512
    
    int main(void)
    {
      cout << "The maximum num is: " << MAX_NUM;
      getchar();
    
    
      return 0;
    }

  5. #5
    Unregistered
    Guest
    #define does not create any variables. The preprocessor changes
    Code:
    #include <iostream.h>
    
    #define MAX_NUM 512
    
    int main(void)
    {
      cout << "The maximum num is: " << MAX_NUM;
      getchar();
    
    
      return 0;
    }
    To be:
    Code:
    #include <iostream.h> //This is replaced by the contents of iostream.h
    
    
    
    int main(void)
    {
      cout << "The maximum num is: " <<512;
      getchar();
    
      return 0;
    }
    These codes are equal to the compiler.

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    With you now. Thanks.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Would someone solve my problem?
    By Lonners in forum C Programming
    Replies: 9
    Last Post: 01-19-2008, 05:58 PM
  2. define constants
    By shuo in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 03:17 PM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  4. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 03:59 PM

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