Home > front end >  What is the limit to output numbers in C?
What is the limit to output numbers in C?

Time:10-05

I was practicing programming in C, and when I ran this code, it came to a point where the outputting numbers just gave up lol, it was around the 30th number of the sequence. What is the limit to output numbers in C? (I was trying the Fibonacci sequence)

int main() {

int fibo, i, n, a, aux;

printf("Enter a number: ");
scanf("%d", &fibo);

n = 1; a = 1;

printf("1 1 ");

for(i = 3; i <= fibo; i  ){
   /* aux = n;
    n = n   a;
    a = aux;*/
    
    n  = a;
    a = n - a;
    printf("%d ", n);
}

}

CodePudding user response:

What is the limit to output numbers in C?

There is no such limit. You can create a library to do arithmetic operations on arbitrary large numbers - and output them.

The question should probably be "what ranges can the fundamental types in C represent?" - and there are different limits for the different fundamental types.

Here's an incomplete list of some types taken from limits.h:

  • INT_MIN - minimum value of int
  • INT_MAX - maximum value of int
  • LLONG_MIN - minimum value of long long int
  • LLONG_MAX - maximum value of long long int

And from float.h:

  • DBL_MIN - minimum, normalized, positive value of double (typically 0.)
  • -DBL_MAX - lowest finite value representable by double
  • DBL_MAX - maximum finite value of duuble

Note that

  • an int is required to be at least 16 bit wide, but is often 32.
  • a long int is required to be at least 32 bit wide and often is.
  • a long long int is required to be at least 64 bites wide.

Depending on the type's bit width and how the implementation make use of these bits (two's complement, ones' complement, sign–magnitude and for floating points, if they use IEEE 754 or something else) affects the ranges they can represent.

CodePudding user response:

Fibonacci numbers get very large very quickly, and will exceed the range of native integer types for relatively small n. On my system, the largest Fibonacci number I can compute with the following code using a regular signed int is F(44):

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

int main( int argc, char **argv )
{
  if ( argc < 2 )
  {
    fprintf( stderr, "USAGE: %s n\n", argv[0] );
    exit( 0 );
  }

  int n = strtol( argv[1], NULL, 10 );
  int f[3] = { 0, 1, 0 };

  printf( "INT_MAX = %d\n", INT_MAX );

  for ( int i = 1; i <= n && INT_MAX - f[2] > f[1]   f[0]; i   )
  {
    f[2] = f[1]   f[0];
    f[0] = f[1];
    f[1] = f[2];
    printf( "fib = = d\n", i, f[2] );
  }

  return 0;
}

Output:

$ ./fib 50
INT_MAX = 2147483647
fib   1 =          1
fib   2 =          2
fib   3 =          3
fib   4 =          5
fib   5 =          8
fib   6 =         13
fib   7 =         21
fib   8 =         34
fib   9 =         55
fib  10 =         89
fib  11 =        144
fib  12 =        233
fib  13 =        377
fib  14 =        610
fib  15 =        987
fib  16 =       1597
fib  17 =       2584
fib  18 =       4181
fib  19 =       6765
fib  20 =      10946
fib  21 =      17711
fib  22 =      28657
fib  23 =      46368
fib  24 =      75025
fib  25 =     121393
fib  26 =     196418
fib  27 =     317811
fib  28 =     514229
fib  29 =     832040
fib  30 =    1346269
fib  31 =    2178309
fib  32 =    3524578
fib  33 =    5702887
fib  34 =    9227465
fib  35 =   14930352
fib  36 =   24157817
fib  37 =   39088169
fib  38 =   63245986
fib  39 =  102334155
fib  40 =  165580141
fib  41 =  267914296
fib  42 =  433494437
fib  43 =  701408733
fib  44 = 1134903170

If I switch to unsigned int, I can compute up to F(45). If I use long, I can get up to F(90). But even if I use unsigned long long, I'll still exceed its range with relatively small n.

To compute Fibonacci sequences for arbitrarily large n, you'll need a third-party bignum library like GMP:

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <gmp.h>

int main( int argc, char **argv )
{
  if ( argc < 2 )
  {
    fprintf( stderr, "USAGE: %s n\n", argv[0] );
    exit( 0 );
  }

  int n = strtol( argv[1], NULL, 10 );
  mpz_t f[3];

  mpz_init_set_str( f[0], "1", 10 );
  mpz_init_set_str( f[1], "1", 10 );

  for ( int i = 1; i <= n; i   )
  {
    mpz_add( f[2], f[1], f[0] );
    mpz_set( f[0], f[1] );
    mpz_set( f[1], f[2] );
    gmp_printf( "fib %d = %Zd\n", i, f[2] );
  }

  return 0;
}

For n == 1000, I get

fib 1000 = 113796925398360272257523782552224175572745930353730513145086634176691092536145985470146129334641866902783673042322088625863396052888690096969577173696370562180400527049497109023054114771394568040040412172632376

Edit

Gah, the sequence starts off wrong - it should be 1, 1, 2, .... But my main point remains.

CodePudding user response:

The largest number the int data type can hold in C is INT_MAX which is 2147483647.

  • Related