Take the 2-minute tour ×

I wrote a program in C that prints English that represents input number (digit by digit) for example, when you enter 938, it will print out nine three eight It doesn't work when the input gets too large (more than 9 digits). Can anyone explain to me why this happens? I have tried to use unsigned int instead of int for the variables, but it still does not work.

#include <stdio.h>
Programming in C
Ch6 no. 6
Program doesn't work when number gets too large (>9 digits)
Print english that represents input number (digit by digit)

int main(void){

int digit;//use to hold digit

int number;//hold input number

int revNumber = 0; //the reversed digit of the input number

int no_zero = 0;//number of zero needed to be printed at the end

int i = 1;//hold how many digits does the number have (+1 = i*10)

printf("Please enter a number\n");

scanf("%u", &number);

int testing = number;//a copy of input number for

//counts how many digits does the number have (+1 = i*10)

for(; testing != 0;i *= 10){

    testing /= 10;


//make the reversed number


    i /= 10;

    digit = number % 10;

    if(digit == 0)


    revNumber += (digit*i);

    number /= 10;

}while(number != 0);

//print the result using the reversed number


    digit = revNumber % 10;

    revNumber /= 10;



     case 0:

     printf("zero ");

     no_zero--; //minus zero not at the end


     case 1:

     printf("one ");


     case 2:

     printf("two ");


     case 3:

     printf("three ");


     case 4:

     printf("four ");


     case 5:

     printf("five ");


     case 6:

     printf("six ");


     case 7:

     printf("seven ");


     case 8:

     printf("eight ");



     printf("nine ");


}while(revNumber != 0);

//add back the ending zero


printf("zero ");

return 0;

share|improve this question
9 digits is about the limit of a 32-bit integer. –  Mysticial Oct 17 '12 at 6:03

4 Answers 4

The standard representation of ints in C is limited. Basically, you have 8 * sizeof (int) bits available (let's say 32). Having 32 bits available, an unsigned int can be as large as 2^32 - 1 which is 4294967295 any number greater than this is not ok.

You can try with unsigned long long but this would still be limited. Or, you can try using the BigNum library.

However, for your specific problem, reading the number as a string and acting on each letter of this string should be enough.

share|improve this answer
Thank you. I get it now : ) –  jackyokboy Oct 17 '12 at 6:15
CHAR_BIT * sizeof (int). –  Wiz Oct 17 '12 at 6:28

An int can (usually) only hold a 32-bit number, so it is limited to storing numbers in the range [-2147483648, 2147483647]. So, it can work for any 9 digit number, but fails for most 10-digit numbers.

Instead of putting the number in an int, why don't you just read each digit one-at-a-time (with, e.g. getc) and process them separately? Then you can handle numbers of any size easily, without needing to use strings.

share|improve this answer
The 32 depends on the platform. –  Mihai Maruseac Oct 17 '12 at 6:07

From what i gather, you don't really need to store it in a int. Store it as a string (char[]) and then display each digit.

You can infact remove the array entirely and just print the word representation of each digit while it is being entered then discard it.

share|improve this answer

An integer consists of 32 bits. If you enter a number greater than 2^32, it won't work. Try getting a string from the user instead of an integer.

You can also use unsigned long long int which is a 64-bit variable.

share|improve this answer
The 32 and the 64 depend on the platform. –  Mihai Maruseac Oct 17 '12 at 6:07

Your Answer


