Uninitialized variables

This is a discussion on Uninitialized variables within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I'm new here, and I have a question, very simple for most of you... I guess. Here's the ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Uninitialized variables

    Hi everyone.
    I'm new here, and I have a question, very simple for most of you... I guess.
    Here's the code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int a, b, c;
        cout << a << endl << b << endl << c <<endl;
        system ("pause");
        return (0);
    }
    The result is:

    2
    16
    2293672

    How and why got the three variables those values??

    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Quote Originally Posted by Rife
    How and why got the three variables those values??
    They happened to be the contents of the memory that those variables were allocated.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Thank you.
    I presumed they should get value of 0.

    So, the variables always should be initialiyed if we want to operate with them.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Rife View Post
    Thank you.
    I presumed they should get value of 0.

    So, the variables always should be initialiyed if we want to operate with them.
    Yes, all local variables must be initialized. Local variables have whatever they happens to be in the memory they are given in the function [unless it's a debug build from V7.0+ of MS Visual studio, in which case uninitialized variables have a specific bit-pattern (e.g. 0xCC) filled into them to make it easier to detect uninitialized variables - but this is also not zero].

    GLOBAL (and static) variables are initialized to zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    As Matsp noted, in certain situations your variables will be initialized to 0 but it's considered very bad form and poor practice to expect your compiler to do the work you should be doing.

    The long and short of it -- always initialize your variables.
    - Leeor

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Depends on what you consider the "work you should be doing". I personally like passing as much work as possible to the compiler.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    In this case it's initializing variables/pointers/whatever. I generally take the approach "If I'm not initializing it myself expect bugs."
    - Leeor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 08:01 AM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM

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