Thread: const vs define

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question const vs define

    Hello!

    I have a program that depends on some 11 values.
    These values do not change or get defined/calculated during the program.
    Instead they are a set of values that depend on something I will call mood.

    What I am trying to understand is if I should find a way to have them as #define or as const.
    This is a program for a robot so all optimization I can get is valuable.

    Is it possible to pass at compile time an argument with mood?
    What is the real gain in speed of using #define instead of cons?

    Thanks for any info.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Consider this:
    Code:
    #include <stdio.h>
    
    #define _Y_
    #ifdef _X_
    #define VAL1 1
    #define VAL2 2
    #elif defined _Y_
    #define VAL1 3
    #define VAL2 4
    #else
    #define VAL1 5
    #define VAL2 6
    #endif
    
    int main () {
    	printf("%d %d\n", VAL1,VAL2);
    }
    You can have as many "elif's" as you want, then make your choice using the first #define before you compile. Alternately, you can leave that line out and give the compiler your choice, eg with gcc:

    gcc myprog.c -D _X_
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    Thank you!

    That was what I wanted to know!

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    The advantage of const is that it has a type, whereas #define values don't. Consider these two similar lines:

    Code:
    const unsigned SOMEVAL = 100;
    #define SOMEVAL 100
    With const, and appropriate compiler warnings turned on, the compiler will warn you that this assignment truncates the value:
    Code:
    char x = SOMEVAL;
    Personally, I find the use of const in those situations to be of no value. I just #define macro values.

    The value of const, in my view, is to let the compiler know you don't plan on modifying a value once it's calculated. That could possibly result in slightly more efficient code. And it prevents you passing the variable to a deviant function that modifies variables secretly passed by reference (i.e. declared with the & operator).

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    A const will also create an object that will take memory. A #define won't. So space-wise I believe it would be better to have a #define. For simple occassion a compiler can optimize the const and don't create an object and just replace the values. Speed-wise there should be no difference.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_ntua
    Theoritically, a const will also create an object that will take memory. A #define won't. So space-wise I believe it would be better to have a #define.
    Without optimisation, it should actually be the opposite: there would be numerous copies of the value throughout the code if you use #define, whereas there would be only one copy of that object if you use const.

    Quote Originally Posted by C_ntua
    For simple occassion a compiler can optimize the const and don't create an object and just replace the values.
    Rather, the compiler may be able to recognise that the same literal constant is used several times, and thus make it such that only one copy is loaded in the end.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  3. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 01:53 PM
  4. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM
  5. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM