0

I have a simple program that finds automorphic numbers up to given number. The problem is it just finds the number which square parameter is bound of int.

for example : 90625 x 90625 = 8212890625 but program could't find it.

Question : What is the problem with long type here? It act like int. Is there a cast to int for the square parameter anywhere of the code?

#include <stdio.h>
int main(void)
{
   int n;
   long i;

   printf ("Enter a value:\n") ;
   scanf ("%d",&n) ;
   printf ("Automorphic numbers:\n") ;
   for (i=1L ; i<=n ; i = i+1L)
   {
       long square = i*i ;

       if (square % 10L == i || square % 100L == i || square % 1000L == i || square % 10000L == i || square % 100000L == i || square % 1000000L == i || square % 10000000L == i)
       printf ("%li\n", i) ;
   }
}
Berke Cagkan Toptas
  • 1,034
  • 3
  • 21
  • 31
  • Are you sure a `long` is the number of bits you think it is? (at least, on your computer) – Dennis Meng Nov 19 '13 at 00:07
  • @DennisMeng actually I checked the size of long it is 4byte :/ I thought that it is 8 byte. So why is there a long type in C since its 4 byte just as int ? – Berke Cagkan Toptas Nov 19 '13 at 00:14
  • http://stackoverflow.com/questions/18971732/difference-between-long-long-long-long-int-and-long-long-int-in-c Check this for more differences between the int types and in your scenario usage of long long int should definitely help! – nagraz007 Nov 19 '13 at 00:37
  • In C, `long` has at _least_ the range from `-2147483647` to `+2147483647`, thus `long` takes at _least_ 32 bits. `long` can have a much larger range, as OP hoped, but that is less common. – chux - Reinstate Monica Nov 19 '13 at 02:48
  • @EricPostpischil Well, it looks like `i` was a long to begin with. Unless the question got edited after you posted? – Dennis Meng Nov 19 '13 at 23:47
  • @DennisMeng: Yes, I thought `i` was `int` originally. Stack Overflow does not show revisions if they are made quickly enough, but we all would have had to be quick to get four comments in before that window closed and the OP edited. Well, no matter, since the comment is not applicable now, I will delete it. – Eric Postpischil Nov 20 '13 at 02:24

2 Answers2

5

C guarantees the following limits (n869 C99 draft):

-- minimum value for an object of type int
    INT_MIN                     -32767 // -(215-1)

-- maximum value for an object of type int
    INT_MAX                     +32767 // 215-1

-- maximum value for an object of type unsigned int
    UINT_MAX                     65535 // 216-1

-- minimum value for an object of type long int
    LONG_MIN               -2147483647 // -(231-1)

-- maximum value for an object of type long int
    LONG_MAX               +2147483647 // 231-1

-- maximum value for an object of type unsigned long int
    ULONG_MAX               4294967295 // 232-1

-- minimum value for an object of type long long int
    LLONG_MIN     -9223372036854775807 // -(263-1)

-- maximum value for an object of type long long int
    LLONG_MAX     +9223372036854775807 // 263-1

[5.2.4.2.1  Sizes of integer types <limits.h>]

When you need defined sizes, use uint64_t and similar datatypes.

Kninnug
  • 7,992
  • 1
  • 30
  • 42
ensc
  • 6,704
  • 14
  • 22
1

For 64 bits you shall use long long.

EDIT: long corresponds to long int which can have the same size as int. You can use sizeof to check the size taken.

Bruno Costa
  • 324
  • 1
  • 5
  • 23