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 6992029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:34:55+00:00 2026-05-27T19:34:55+00:00

I just started learning the very basics of c++, I have experience with vb.net

  • 0

I just started learning the very basics of c++, I have experience with vb.net and c#

Now I thought to get the basics covered some Project euler problems would be a good excercise. I ran into a very strange problem by trying to solve Problem 4, probably because of a lack of understanding of c++ yet.

I got 2 scenario’s, one is working, one is not(> the a variable just jumps from 999 to 768 when the sprintf command is executed as shown below), now my question is: Why is this happening in solution 1? The only difference is the loop variables a and b are declared before the loop as in solution 2 they are declared with the loop.

Solution 1 (not working):

int a =999;
int b =999;
int lb = 100;
int c = 0;
char strNumber[6];
int result = 0;
int ra = 0;
int rb = 0;
for (a=999; a>=100; a--){
    for (b = a; b>=lb; b--){
        c = a * b;
        sprintf(strNumber, "%d", c);
        if (isPalindrome(strNumber)){
            if (c > result){
                result = c;
                lb = b;
                rb = b;
                ra = a;
            }
            b = 0;
        }
    }
}

Solution 2 (working):

int lb = 100;
int c = 0;
char strNumber[6];
int result = 0;
int ra = 0;
int rb = 0;

for (int a=999; a>=100; a--){
    for (int b = a; b>=lb; b--){
        c = a * b;
        sprintf(strNumber, "%d", c);
        if (isPalindrome(strNumber)){
            if (c > result){
                result = c;
                lb = b;
                rb = b;
                ra = a;
            }
            b = 0;
        }
    }
}
  • 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-27T19:34:56+00:00Added an answer on May 27, 2026 at 7:34 pm

    The first time through the loop, c is going to be a 6-digit number (999²).
    The sprintf(strNumber, "%d", c); will thus write 7 chars to strNumber: the six digits and the famous null terminator. Since you only reserved six chars for strNumber, that last write will clobber something.

    It happens not to appear to clobber something important in your second case, but that’s just “luck”, your code’s behavior is undefined in both cases.

    Reserve an extra char in strNumber and both versions should work the same as far as I can tell.

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

Sidebar

Related Questions

I have just started learning Erlang and am trying out some Project Euler problems
Very confused over this. I've just started learning about pointers and have now decided
I started learning jQuery just yesterday, and I like it very much. And now
I just started learning Objective-C and OOP ... so I have very stupid question
I just started learning about Lucene and I get how it can be very
I've just started learning F# (with little prior experience with .NET) so forgive me
I have just started learning Ruby. Very cool language, liking it a lot. I
I just started learning C++ (coming from Java ) and am having some serious
I have just started learning ruby on rails. I have a doubt wrt routing.
I've just started learning the templating system Dwoo, and so far the basics are

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.