#define or const?

This is a discussion on #define or const? within the C Programming forums, part of the General Programming Boards category; What's better? and why? Thanks...

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    #define or const?

    What's better?
    and why?

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Asking which is better is not a very good question. They have their place depending on the situation.

    I use #defines more than consts, but that is me. I think const can get confusing depending on there you place it.

    But one place const can be used where defines can't (and this is where I get almost all of my const usage) is in functions where I do not want the incoming variable to be modified (in a function). Look at the prototype for things liek strcat, printf, etc..

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    you can use either #define or const in C. But traditionally and the way I still see it taught is:

    #define -C const- C++
    Mr. C: Author and Instructor

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    const is more correct in either language imo. defines can be very dangerous. but, they do have their place
    hello, internet!

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Ok, thanks guys!

    Thanks.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Also, it depend on where you want your thing handled. As I recalle defines are handled in preprocessor, and consts during runtime.

    For macros, i always equate them to inline functions in my head and they might cause an executable to be large, depending on how much you use it and such. But that is a bit more complicated, and better off not even used.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Here are several threads with additional thoughts on this matter.

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, 11:20 PM
  2. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 06:33 AM
  3. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 02:53 PM
  4. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 03:27 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

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