Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 840971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:42:54+00:00 2026-05-15T05:42:54+00:00

I am new to C++ (just starting). I come from a Java background and

  • 0

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.

Edit:
Im using g++. Here is the output from g++ –version:
Im on Mac OS X and am using g++.

nom24837c:~ omarestrella$ g++ --version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:42:55+00:00Added an answer on May 15, 2026 at 5:42 am

    Because it’s not part of the C++ standard. The variable will just take whatever value is currently sitting in the memory it’s assigned. This saves operations which can sometimes be unnecessary because the variable will be assigned a value later.

    It’s interesting to note and very important for Java/.Net programmers to note when switching to C/C++. A program written in C++ is native and machine-level. It is not running on a VM or a some other sort of framework. It is a collection of raw operations (for the most part). You do not have a virtual machine running in the background checking you variables and catching exceptions or segfaults for you. This is a big difference which can lead to a lot of confusion in the way C++ handles variables and memory, as opposed to Java or a .Net language. Hell, in .Net all your integers are implicitly initialised to 0!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 494k
  • Answers 494k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Each time you call a method, an entry is place… May 16, 2026 at 10:56 am
  • Editorial Team
    Editorial Team added an answer You can't do that with WPF only. You need to… May 16, 2026 at 10:56 am
  • Editorial Team
    Editorial Team added an answer Does this give you what you want: SELECT Table2.JobCategoryId, Table2.JobCategoryName,… May 16, 2026 at 10:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm very new to programming and I'm just starting to learn VBA with excel.
Just starting to poke around iPhone development. I am trying to add an external
I am just starting with OSGi programming and have come across two ways to
I'm just starting to do web pages in ASP and all of the code
Just starting out with jQuery and this problem seems to be impossible to me.
I am just starting to learn javascript, so I don't have the skills to
I'm trying to roll my own implementation of IShellBrowser because I need to have
We are implementing a C# application that needs to make large numbers of socket
Edit: Just wanted to make the question I have more clear. I pretty much
I am using TouchXml because of the given limitations of NSXML on the actual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.