I was reading a book and this part confuses me a bit regarding getchar()
int peekchar(void){
int c;
c = getchar();
if(c!=EOF) ungetc(c,stdin);
return c;
}
int readNumber(void){
int accumulator=0; /*Number read so far*/
int c;
while((c = peekchar()) != EOF && isdigit(c)) {
c = getchar(); /* consume it */
accumulator *= 10; /* shift previous digits over */
accumulator += (c - '0'); /* add decimal value of new digit */
}
return accumulator;
}
What exactly happens here in accumulator += (c - '0');
And the input would be digits
i.e 123
would be given as input and that will be printed as output 123
again. This is what the program does. Any explanation would be helpful guys.