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

  • Home
  • SEARCH
  • 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 8753869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:30:42+00:00 2026-06-13T13:30:42+00:00

This code fails randomly by correctly identifying some numeric palindromes and failing on others.

  • 0

This code fails randomly by correctly identifying some numeric palindromes and failing on others.

#include <stdio.h>


int main(int argc, char *argv[])
{
   int n, reverse = 0, temp;

   printf("Enter a number to check if it is a palindrome or not\n");
   scanf("%d",&n);

   temp = n;

   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }

   if ( n == reverse )
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d is not a palindrome number.\n", n);

   return 0;
}

For example, the above code incorrectly says “87678” isn’t a numeric palindrome.

Checking the return of scanf() shows it’s succeeding and printing the value of n is correct for input of 87678.

However the code correctly says “4554” is a palindrome.

However, by adding:

n = reverse = temp = 0;

before the first printf() the program appears to work correctly all the time. So what is happening in the first version? Is this some sort of undefined behavior when the variables aren’t initialized before use?

EDIT: Will later provide the assembly of the compiled version that is failing to see what the compiler is doing.

  • 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-06-13T13:30:43+00:00Added an answer on June 13, 2026 at 1:30 pm

    Unless sizeof(int) is less than 4, you’ve either hit a compiler bug, your hardware is malfunctioning, or you have some form of data corruption going on in your system.

    To answer the question: no, there’s no undefined behavior anywhere in your program (assuming the scanf() really doesn’t fail).

    Try running memtest on your system to rule out RAM issues: http://www.memtest.org

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

Sidebar

Related Questions

This code fails on some machines: // Parse error: syntax error, unexpected '[' ...
this code fails randomly when input comes from specific sources, it works usually OK:
This code fails to compile under VS2010: #include <functional> using namespace std; void test()
The following code example fails randomly on some PC's. On other PC's the problem
I don't understand why this code fails to compile in main() due to ambiguous
Challenge: I have this code that fails to compile. Can you figure out what's
This code does not throw an error but the query fails, that is, the
The following code fails in 'Evaluate' with: This expression was expected to have type
This code works when I try it from a .py file, but fails in
Why does this code execute perfectly on Dev-CPP but fails to execute on Borland

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.