const vs define

This is a discussion on const vs define within the C Programming forums, part of the General Programming Boards category; Hello! I have a program that depends on some 11 values. These values do not change or get defined/calculated during ...

  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
    21,650
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

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