Const vs int

This is a discussion on Const vs int within the C++ Programming forums, part of the General Programming Boards category; Hi guys! Do not really understand the difference between the two following way of declarations: int x=2; const int x=2; ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    7

    Const vs int

    Hi guys! Do not really understand the difference between the two following way of declarations:

    int x=2;

    const int x=2;

    I can figure out that the second one should be a constant and the first one a variable, but what is the real difference between them in the manner of functionality?

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    The second statement is read-only data, it would act like x is an alias for the integer value 2. In the second statement you would not be able to change what x is...



    ...unless you use a const_cast but that's for another thread.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    It would be illegal to use a const_cast on that and subsequently attempt to modify it. If you did try to, it quite likely may not allow you to change the value, as it may reside in a read-only memory location.

    You use the const case when you know x wont change, or specifically want to ensure that x is not changed, during its lifetime.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Well, you got the whole cannot change part--trying to change it is not allowed since it's const. You can argue that's part of its functionality, but I suppose you're looking for more than this.
    This part is tricky. In C++ there's something called a constant expression. What that means is that it's an expression which the compiler can deduce at compile time. For example, when creating an array, you must specify its size. The compiler must be able to determine its size at compile time. Therefore, the size must be a constant expression.
    Normal variables aren't constant expressions because they can change. However, along with certain limitations, const variables can be constant expressions. If they are declared locally or globally (and not as function parameters) and initialized with a constant expression (say, an integer), then they are considered constant expressions and are basically substituted for its value at compile time.
    That's basically all the difference between the two unless I'm forgetting something.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    Thanks guys, all of you!
    I think for now its enough for me to know that the difference is that const make the integer nonchangeable.
    By the way guys! any tips how or where to find good and easy exercises to solve? I need lots of practicing :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need help with this question:
    By marcuspax in forum C Programming
    Replies: 16
    Last Post: 08-16-2010, 09:12 AM
  2. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 01:06 PM
  3. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM
  4. 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
  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