const's and #Defines

This is a discussion on const's and #Defines within the C Programming forums, part of the General Programming Boards category; Hello. I know this is sort of a dumb question, but I'm still learning C, and really enjoying it for ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    const's and #Defines

    Hello. I know this is sort of a dumb question, but I'm still learning C, and really enjoying it for that matter.
    My question is, in my book it explains that const and #define's are not different. If that is so, which one should I use most often?

    Thanks

  2. #2
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    They are different , they cannot be used the same way
    for example , the preprocessor replaces N with 100 and the compiler sees int a[N] changed to int a[100], there is no variable named N

    Code:
    #define N 100
    int
    main(void)
    {
        int a[N];/*right*/
       return 0;
    }
    The code below is wrong in C89

    Code:
    const int s=100;
    
    int
    main(void)
    {
        int a[s];/*wrong*/
       return 0;
    }
    In C++ things are different .

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Its arguable which one is better. In C the #define does a direct replace of everything it see's with something else. In C++ it gives you a constant but it is a variable which requires space to store. Also C++ const keeps type safety for you.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Originally posted by MrWizard
    Its arguable which one is better. In C the #define does a direct replace of everything it see's with something else. In C++ it gives you a constant but it is a variable which requires space to store. Also C++ const keeps type safety for you.
    but the space taken up by a const variable is very negligable. use const whenever possible, and use #define whenever necessary. (like giving the compiler special options).

    but since you're using C i'm not sure you have much of a choice but to use #defines. c is much stricter where and what you can initialize, and when you can assign values to variables.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    I'm no master of assembly, but let me throw out a question.....

    const int x = 100;
    #define Y 100
    --------------------
    int a = x; //case 1
    int b = Y; //case 2

    doesn't case 1 have to use an offset to the memory (of x)?
    doesn't case 2 use the value directly, thus taking up no more memory than the offset?

    If this is true, why on earth would you "Use const where possible"?

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    const #define

    Thanks for the input.. I now have rough idea what to use and when to use it. Thanks again guys

  7. #7
    TK
    Guest

    Re: const's and #Defines

    Originally posted by Dimeslime99
    Hello. I know this is sort of a dumb question, but I'm still learning C, and really enjoying it for that matter.
    My question is, in my book it explains that const and #define's are not different. If that is so, which one should I use most often?

    Thanks
    Here is one corellation, however in other cases the #define is a preprocessor directive and the keyword const is a type qualifier. The type qualifier has more diverse usages.
    Code:
    #include<stdio.h>
    
    #define SIZE 10
    
    int main()
    {
      int i;
      const int ciSize = 10;
      int iSize = 10;
    
    
      //Test each definition individually
      //double dArray[SIZE] = {}; //works fine
      double dArray[ciSize] = {}; //works fine
      //double dArray[iSize] = {}; //doesn't work!
    
      for(i = 0; i < SIZE; i++)
        printf("Indexes %d = %lf \n",i, dArray[i]);
    
      return 0;
    
    }
    : gcc -o prog prog.cpp
    : ./prog

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >In C++ it gives you a constant but it is a variable which requires space to store.<

    Not true. Any C++ compiler worth it's price will only allocate space for a const if its address is explicity used. Otherwise a substitution is done after the type checking. But as has been stated C consts are different.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    const Vs #define

    --->#define tells the compiler that whereever the compiler encounters the #defined variable below the #define statement ,replace the variable wth the #defined value.
    --->const tells the compiler that the value of the variable cannot be changed after initialisation.

    in C, "#define"s are replaced at compile-time (or link-time whatever) ,but "const" are replaced at runtime.
    so the following code will work in C:
    #define CONSTANT 10
    int main(int argv,char*argv)
    {
    int intarray1[10]; // normal declaration of an array
    int intarray2[CONSTANT] ; // this is perfectle aceptable
    return 0 ;
    }

    but not the following:
    int main(int argv,char*argv)
    {
    const int CONSTANT =10;
    int intarray1[10]; // normal declaration of an array
    // int intarray2[CONSTANT] ; // this is not aceptable in C,
    //but acceptable in C++ ,as resolution of const is done at
    //compile time in C++
    return 0 ;
    }



    A #defined value can be assigned to any type of avriable.
    like:
    #define x 20;
    int myint=x;
    float myfloat=x;
    both the above are legal.

    but const is a assosiated with a predefined data type., (u may type cast and use it , i have not checked that...)


    now comes the question where to use what..
    const is used for making it sure that the value of the variable is not changed in any part of the program.
    #define is generally used for macros.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'm looking at the question in a totally different light than anyone else--excluding Hershlag (being a video game programmer).

    Code:
    //code using const
    const long clong = 100;
    
    int main(int argc, char **argv) {
        long num;
    
        num = clong;
    
        return 0;
    }
    Now
    Code:
    #define CLONG 100
    
    int main(int argc, char **argv) {
        long num;
    
        num = CLONG;
    
        return 0;
    }
    Okay, here is the big difference I see with these example. The first declares clong--a constant long--which happens to be global. The second declares no global variable, rather it has a number to put in the place of the word CLONG during compilation. The advantage of the #define here is that no extra memory is taken up to hold that 4bytes of space that could have easily been saved by using a #define. However, this doesn't typically alter the size of an executable. But it does optimize the amount of memory used.

Popular pages Recent additions subscribe to a feed

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