Search Results

Search found 1 results on 1 pages for 'omizzle'.

Page 1/1 | 1 

  • Why is it that an int in C++ that isnt initialized (then used) doesn't return an error?

    - by omizzle
    I am new to C++ (just starting). I come from a Java background and I was trying out the following piece of code that would sum the numbers between 1 and 10 (inclusive) and then print out the sum: /* * File: main.cpp * Author: omarestrella * * Created on June 7, 2010, 8:02 PM */ #include <cstdlib> #include <iostream> using namespace std; int main() { int sum; for(int x = 1; x <= 10; x++) { sum += x; } cout << "The sum is: " << sum << endl; return 0; } When I ran it it kept printing 32822 for the sum. I knew the answer was supposed to be 55 and realized that its print the max value for a short (32767) plus 55. Changing int sum; to int sum = 0; would work (as it should, since the variable needs to be initialized!). Why does this behavior happen, though? Why doesnt the compiler warn you about something like this? I know Java screams at you when something isnt initialized. Thank you.

    Read the article

1