0

I want to write a code that gets a number with unknown number of digits (means it can both 100 and 1000000). I thought of using getchar and wrote the following code:

#include <stdio.h>

int main()
{
  char nextdigit;
  int finalnumber=0;
  nextdigit=getchar(); 

  while(nextdigit!='\n')
  {
    finalnumber=finalnumer*10+(int)nextdigit;
    nextdigit=getchar();
  }
  printf("%d",finalnumber);
}

but seems like when I enter for example 3 it prints 51 (maybe the ascii value of 3 ) and in fact doesn't allow me to enter digits anymore. What am I doing wrong?

haccks
  • 104,019
  • 25
  • 176
  • 264
Danis Fischer
  • 375
  • 1
  • 7
  • 27

5 Answers5

1

Yes, 51 is the ASCII value of '3'. There is an easy way to get a characters ASCII value to the integer value though, just subtract the ASCII Value of '0':

finalnumber = finalnumer * 10 + nextdigit - '0';

Also, be careful when using char variables with getchar, as getchar is declared to return an int. You might also want to check for EOF being returned (which is why you need an int variable).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Side-Insight: Interesting here is that the standard guarantees 0..9 to be contiguous. Otherwise, this would be, by C++ law, not by fact, a non-portable trick (http://stackoverflow.com/questions/9416926/are-the-character-digits-0-9-required-to-have-contiguous-numeric-values). – Sebastian Mach Nov 18 '13 at 13:51
  • @phresnel My guess is that it's in the standard because so many applications already had that. I find it funny though that the same is not said for letters, even though it's just as common. But that's probably because there already was at least one character set (EBDIC) where that wasn't true. – Some programmer dude Nov 18 '13 at 13:59
1

getchar() returns int. Declare nextdigit as int.

int nextdigit;   

and subtract 48 (the ascii value of '0') from the expression finalnumber = finalnumber*10 + nextdigit;

finalnumber = finalnumber*10 + nextdigit - '0';
haccks
  • 104,019
  • 25
  • 176
  • 264
0

You will get ASCII so remove : 0x30 ('0')

You probably will have to do something about the '-' (because you are entering an int) otherwise use unsigned int.

Joze
  • 1,285
  • 4
  • 19
  • 33
0

You can drastically simplify your code:

#include <stdio.h>

int main(void) {
    int number;
    printf("Enter an integer:\n");
    scanf("%d", &number);

    printf("Number was: %d\n", number);
    return 0;
}
Rmn
  • 111
  • 1
  • 6
0

that depends on how big your input number is

short int          - 32768 ~ 32767      scanf("%d", &number);
unsigned int         0 ~ 2^32 -1        scanf("%u", &number);
int                 - 2^31 ~ 2^31 - 1   scanf("%d", &number);
int 64              - 2^63 ~ 2^63 - 1   scanf("%I64d", &number);

if your input is bigger than 2^63 - 1 you should try use char as input and each input character of type char is its ASCII value

  char nextdigit;
  int finalnumber=0;
  nextdigit=getchar(); 
  finalnumber=nextdigit-'0';
Patato
  • 1,464
  • 10
  • 12