#1
  1. No Profile Picture
    PerlGuy
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Posts
    720
    Rep Power
    42

    Symbolic Constants - Advantages


    Hey,
    I recently started using C and I've got a question about the advantages of using Symbolic Constants versus variables.

    I'm guessing that there is some kind of advantage as far as resources go since you can't change a Symbolic Constant after its defined(as opposed to changing a var after its declared), but I can't quite work out what that would be.

    Can anyone clear this up?

    Thanks in advance.
    - dsb -
    Perl Guy
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    Yes.

    Symbolic constants are evaluated by the PRE-processor, i.e. they are re-written as literal values, not as variables. this has some nice side-effects that you can do using symbolic defines eg. macros and functions inside them.

    run-time access to symbolic constants should be significantly faster than access to variables.

    opposite to that, variables (and constants) are evaluated by the processor (compiler).
  4. #3
  5. No Profile Picture
    PerlGuy
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Posts
    720
    Rep Power
    42

    Smile


    Thanks.

    I figured it was something like that but didn't know exactly what, or where to look something like that up. Perhaps it would've been explained further in the book I'm using to learn, but I didn't want to go further without knowing why I should use them.

    I guess they are pretty much like hardcoding values, except that you can give them a meaningful name for the sake of code clarity.

    Thanks again. You da man ;)
    - dsb -
    Perl Guy
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Originally posted by dsb
    I guess they are pretty much like hardcoding values, except that you can give them a meaningful name for the sake of code clarity.
    And, if you use the constant in several places, all you need to do is change the line

    #define MAX_SIZE 225

    (or whatever it looks like in C), rather than going code mining for all appearances of the value 225, and determining if it needs changing. Saves alot of headaches. General practice for me is, if I'm going to hardcode something, I use a symbolic constant defined in one of my #include files, that way I can modify the program behavior easily without hunting through obscene amounts of code.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    you can read everything about C (ansi C(!)) - and all the background in the book by itīs inventors Kernighan and Ritchie. The german title is "Programmieren in C". English is probably literally translated.

    but i donīt think this book is for beginners in programming. if you have some pascal or delphi background or you are already doing some C - go for it. itīs worth every single penny :)
  10. #6
  11. No Profile Picture
    PerlGuy
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Posts
    720
    Rep Power
    42
    originall posted by M.Hirsch

    you can read everything about C (ansi C(!)) - and all the background in the book by itīs inventors Kernighan and Ritchie.
    That's the book I'm using. ;)

    At this point I need a crash course so I'm going to have to skip some chapters. I've got a programming background though so I'm not that worried about it ;).
    - dsb -
    Perl Guy

IMN logo majestic logo threadwatch logo seochat tools logo