6

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.

Chimera
  • 5,884
  • 7
  • 49
  • 81
  • 1
    Check the return value of `scanf`? – Daniel Fischer Oct 29 '12 at 09:15
  • An attempt to debug this was made, and the results were still ambiguous as to the actual cause of this behavior, therefore the question – Tony The Lion Oct 29 '12 at 09:17
  • why you are passing arguments in main ? – Code Spy Oct 29 '12 at 09:17
  • 4
    Please give an example input for which this fails. – Nikos C. Oct 29 '12 at 09:17
  • @DextOr They don't have an effect on the behavior of this code. – Nikos C. Oct 29 '12 at 09:18
  • Can you be specific on test (numbers) for which it is passing only when you add n = reverse = temp = 0; line? – sahaj Oct 29 '12 at 09:20
  • try printing temp (after the scanf) on the cases it fails. As @Daniel Fischer Id expect something going wrong there. Though in any case, it is good practice to initialize variables – Thomas Oct 29 '12 at 09:21
  • The code is fine but you should strictly check the input given to scanf() – Omkant Oct 29 '12 at 09:21
  • this code works - http://ideone.com/q7ycBU – Abyx Oct 29 '12 at 09:26
  • What system are you running this on? – Lundin Oct 29 '12 at 09:31
  • What happens when you replace the line containing the `%` operation with this: `reverse = reverse + (temp - 10 * (temp / 10));` – Nikos C. Oct 29 '12 at 09:32
  • Can you tell us what version compiler you are using, and optionally paste bin the output of the equivalent of gcc -S (generate assembly). You might have a compiler bug?? – dave Oct 29 '12 at 09:36
  • @Lundin 32 Bit Linux with GCC 4.5.2 - ubuntu 11.04. Others have noted the code works fine on a newer version of GCC... which is rather surprising to me. – Chimera Oct 29 '12 at 09:38
  • Please show the result you get for 87678 (`printf()` the `reverse` var after the `while` loop finished.) – Nikos C. Oct 29 '12 at 09:39
  • This works just fine with Mingw GCC 4.6.2, as well as Embarcadero C++, tested on 64-bit Windows 7. – Lundin Oct 29 '12 at 09:44
  • @NikosChantziaras after the loop the value of reverse was a rather large negative number. – Chimera Oct 29 '12 at 09:53
  • I suppose the alternative code I gave 5 comments above doesn't change the result? – Nikos C. Oct 29 '12 at 09:55
  • @NikosChantziaras Didn't make a difference. Thanks for your help. I'm running memtest now. – Chimera Oct 29 '12 at 09:56
  • I don't see an error in the code either. Either run it with a debugger or add some debugging statements to show the value of "reverse" each time through the loop. Maybe that will give a clue. – Jay Jun 25 '15 at 13:55

3 Answers3

4

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

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • Running memtest now. Thanks for taking the time to help out. – Chimera Oct 29 '12 at 09:59
  • That pretty much leaves a compiler toolchain bug as the most likely reason. Especially if it's reproducible constantly (hardware failure usually doesn't give 100% reproducibility.) – Nikos C. Oct 29 '12 at 16:30
2

It sounds very much like you have a compiler error since this works with later versions of gcc. I'd be very interested to see the output of gcc -S (pastebin please?) and also to know the compile command you are using. (optimization level especially).

dave
  • 4,812
  • 4
  • 25
  • 38
  • I will provide that assembly output when I get back into the office. Thanks. It is odd that different compilers are showing different results. – Chimera Oct 29 '12 at 09:59
-3

Unlike Java, C does not have a default value for int. You can refer to this post as it discuss this similar problem.

Community
  • 1
  • 1
Ian
  • 691
  • 3
  • 9