Thread: Variable initialization

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    Variable initialization

    One of the reasons I allmost never get any real work done, I think, is that I allmost allways stumble over small things that I don't understand. And then, instead of pragmatically solving it another way, I stop everything.

    This time it's about declaring and initializing integer variables in C++:

    Code:
    int l, h, s;
    if (!l)
    	cout << "Not l!" <<endl;
    if (!h)
    	cout << "Not h!" <<endl;
    if (!s)
    	cout << "Not s!" <<endl;
    Silly me though that !<variable> would return true if the variable was initialized, and false if it wasn't. And for the first two integers in this example - l and h - it seems to work like that. But not for the third, s. The printout of the above is:

    Not l!
    Not h!

    That's it. Apparently, it has decided that s is indeed intialized, even though it doesn't happen anywhere. If I change the variable declaration to this:

    Code:
    int l, s, h;
    Then the printout is:

    Not l!
    Not s!

    So it's obviously the third one that's different. I might be completely off here, of course, since I've barely started looking at C++. Is this a non-valid way to check if a variable is initialized? Even if it is, I'm curious as to why the result is different for the third variable. There's probably a basic and obvious explanation, I know :-)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well none of them are initialised.

    However, all of them will contain some garbage value.

    If said garbage value is zero, then !var will return true.
    But this is just pure dumb luck at the moment.

    > Is this a non-valid way to check if a variable is initialized?
    No.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Thanks - good answer :-)

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot check via some means in the language if a variable has been initialized or not.
    If you want to test if they've been assigned some value or not, initialize them to some invalid input, and then check if they're equal to that.
    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
    Nov 2009
    Posts
    111
    Elysia: That was going to be my follow-up question. I know that it's possible, but I wondered if it would be a more "tidy" way of doing it. In the example I was playing with, where the values needs to be in a specific range for the program to continue, the solution seems to be to initialize the integers with "wrong" (too high, too low etc.) values. That way I can use a while-loop to wait for input in the target range.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cnewbie1 View Post
    ...the solution seems to be to initialize the integers with "wrong" (too high, too low etc.) values. That way I can use a while-loop to wait for input in the target range.
    That is typically how you do it. That and have a loop that verifies input to see it is within a sane range. Ensuring a variable it initialized to some non-legit range just makes sure that loop gets started.
    Of course, you can avoid initializing it altogether and making sure that it is initialized later, with for example, some input before use (a do ... while loop may be appropriate here).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-15-2011, 04:18 PM
  2. Array initialization with int variable
    By tsantana in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2009, 02:48 PM
  3. member variable initialization
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2007, 09:52 AM
  4. variable initialization error...
    By sunoflight77 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2005, 12:01 AM
  5. Variable Declaration & Initialization :: C++
    By kuphryn in forum C++ Programming
    Replies: 6
    Last Post: 12-26-2001, 10:22 AM