0

Obviously the code below is too naive. How can an very large input be successfully compared with the largest int possible?

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

int main ( void ) 
{
    unsigned long long input;
    
    printf("Enter a big integer: ");
    scanf ( "%llu", & input) ;
    
    if ( input > ULLONG_MAX ) printf ( " Too big!\n" ) ;
    else printf ( "%llu is not that big.\n" , input ) ; 
    
    return 0;
}
Marcos Gonzalez
  • 1,086
  • 2
  • 12
  • 19
  • 2
    Read it into a string, then try to convert it to an integer with `sscanf()`. – Barmar Feb 27 '21 at 20:58
  • 1
    How would you, as a human, recognize that a number is bigger than 100 if you could only handle numbers from 1-100 and I gave you the digits `283746`? – mkrieger1 Feb 27 '21 at 20:58
  • To build off of @Barmar's answer, while it's not explicitly _performant_, you could then compare the string form of the converted number to the original input. – Rogue Feb 27 '21 at 21:38
  • @Barmar No. *scanf functions are not suitable for this task. They don't detect overflows or wraparounds. – M. Nejat Aydin Feb 27 '21 at 22:02
  • 1
    Use one of the strtol, strtoll, strtoul, strtoull. – M. Nejat Aydin Feb 27 '21 at 22:04

0 Answers0