const and #define

This is a discussion on const and #define within the C Programming forums, part of the General Programming Boards category; what,where and how will there be differences in constant declaration when this is done: #define a 10 (or) const int ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    const and #define

    what,where and how will there be differences in constant declaration when this is done:

    #define a 10
    (or)
    const int a=10;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do you think the differences are?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    #define can be a macro as well as label a simple value, const is an indicator to the compiler that this value will not be changed in the function.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Just going to keep posting your quiz questions until you get an answer on all of them I guess. Sigh.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mohnish_khiani View Post
    what,where and how will there be differences in constant declaration when this is done:

    #define a 10
    (or)
    const int a=10;
    It's not bad enough they expect us to write code for them...
    Now they expect us to do their exam questions too?

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    Why wouldn't he? You've only got to look at his other thread to realise it's an effective method of getting a grade. If you don't have a clue, I'd wager the time spent spamming it about the place is much less than the time to work it out. All it takes is one person.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    @rogster001....thank you.

    @adeyblue...who ever u may be.....these are not my "QUIZ QUESTIONS" as u think....can't answer them...just mind our own work rather than scribbling some crap here...no one's forcing u to answer

    @all.....i have joined here just yesterday coz i thought i could ask my doubts in C which i just started to learn....i thought u guys might help as this is a doubts forum....but rather i don't know u guys are acting in a rather hostile manner or maybe i am asking the wrong people....coz if u guys were to help i wouldn't have mattered if a question was a genuine doubt or a "QUIZ ASSIGNMENT".

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mohnish_khiani View Post
    @all.....i have joined here just yesterday coz i thought i could ask my doubts in C which i just started to learn....i thought u guys might help as this is a doubts forum....but rather i don't know u guys are acting in a rather hostile manner or maybe i am asking the wrong people....coz if u guys were to help i wouldn't have mattered if a question was a genuine doubt or a "QUIZ ASSIGNMENT".
    First of all, this forum has a homework policy and, yes, people do try to ignore it or get around it just about any way they can think of. We even had one goofball in here offering to pay us to do it for him.

    Second, these things you are asking are usually very well covered in the various books and tutorials about C... to us it looks like you'd rather pepper us with very basic questions than spend a few minutes looking things up.

    Understand? This is not a "doubts forum" where people come to ask questions from the first chapter of a book... it's mostly a place you come to when your code isn't working or you are totally baffled about something that's NOT in the books... Look it up first, google second, then maybe come here...

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    thank you Tater.....but the question i asked was just out of my curiosity...i couldn't find it in my books which goes on comparing the effects of keywords in this case coz there can then be many combinations then...i wanted to know was i right in my thinking coz i even executed the code and couldn't get the answer....so thought of getting help from an expert

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mohnish_khiani View Post
    thank you Tater.....but the question i asked was just out of my curiosity...i couldn't find it in my books which goes on comparing the effects of keywords in this case coz there can then be many combinations then...i wanted to know was i right in my thinking coz i even executed the code and couldn't get the answer....so thought of getting help from an expert
    Ok... to answer your question... #define is a preprocessor function, it doesn't actually create a variable, all it does is character substitution. The second one const int does create a variable as an integer that you're not to change the value of.

    If you write #define MAX_VERB 10 wherever you see MAX_VERB in your code, the compiler sees the number 10 which as no type and is not a variable.

    If you write const int MyVariable = 10; the preprocessor does not see it, it is a variable, it has a type and it has a value of 10.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,674
    Quote Originally Posted by CommonTater View Post
    First of all, this forum has a homework policy and, yes, people do try to ignore it or get around it just about any way they can think of. We even had one goofball in here offering to pay us to do it for him.
    It may be considered somewhat ironic that you are now making these comments about the homework policy, given that (over the last few hours) you knowingly posted a solution to an obvious homework problem. Look here and subsequent posts.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  12. #12
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    const int a = 10 reserves four bytes for the value of "a". All #define a 10 does is replace every occurrence of "a" with "10" at compile time. No memory is reserved for a.

    I recommend using #define macros instead, since they don't take up any memory during execution. When you define macros, be sure to use ALL CAPS FOR THEIR NAMES so you don't get confused with macros and variables. It's not required, but it's recommended.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,674
    Quote Originally Posted by Babkockdood View Post
    const int a = 10 reserves four bytes for the value of "a". All #define a 10 does is replace every occurrence of "a" with "10" at compile time. No memory is reserved for a.
    That is not true on either count.

    Neither approach guarantees that 4 bytes will be reserved for "a", but neither approach is prevented from doing so.

    The effects actually depend on the workings of the compiler (or, in the case of a macro, the compiler phase after preprocessing is complete).

    Specifically, with "const int a = 10;" the compiler is free to substitute the literal value 10 every time the name a is used. That would give exactly the same effect as if a macro had been used. If the compiler does that - and most modern compilers do - there is no need to reserve memory for a, over and above what it would do if the literal value 10 was used directly in code (which is what is achieved when the macro is used).

    Depending on how a literal value (say 10) is used, the compiler is potentially required to allocate memory for it (eg in order to pass it as an argument to the function). The compiler's need to do that (or ability to avoid doing it) is unaffected by whether that literal value is obtained from a const declaration or a macro.
    Quote Originally Posted by Babkockdood View Post
    I recommend using #define macros instead, since they don't take up any memory during execution.
    I would actually recommend exactly the opposite. Macros do not honour scope, so can break subsequent code in unintended - and, more importantly, hard to find - ways.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    @all....thank you everyone...got it.....
    ......can i continue asking my doubts on this forum...or am i at the wrong place???

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mohnish_khiani View Post
    @all....thank you everyone...got it.....
    ......can i continue asking my doubts on this forum...or am i at the wrong place???
    There's nothing wrong with asking questions... but you will do a lot better if you spend a bit of time with your books and maybe do some google searching first.

    Think about how much time and effort you invoke when you ask a simple question... dozens of people spend the time to read your question, think about the answer and then write a response... It can amount to you demanding many hours of our cumulative time for something you can answer yourself in 2 minutes of online searching.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdio.h?
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 05-21-2010, 08:09 PM
  2. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 10:36 AM
  3. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

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