I am going through the "C Programming Book" by K&R.
Now the code for the function "getint()" is as follows :-->
Code:
#include<stdio.h>
#include"getch.h"
int getint(int *pn) {
int c, sign;
while(isspace(c = getch()));
if(!isdigit(c) && c != EOF && c != '-' && c != '+') {
ungetch(c);
return 0;
}
sign = (c == '-')?-1:1;
if(c == '+' || c == '-')
c = getch();
*pn = 0;
while(isdigit(c)) {
*pn = (*pn * 10) + (c - '0');
c = getch();
}
*pn *= sign;
if(c != EOF)
ungetch(c);
return c;
}
int main(int argc, char** argv) {
int r, i;
while((r = getint(&i)) != EOF)
if(r != 0)
printf("res: %d\n", i);
return 0;
}
Now I don't get the step by step working procedure of this function, even though I tried to run it theoretically on a paper.
And the fact that when I input "23". how does it converted to 23 , I know there is the logic to convert "23" to 23 but c = getch() doesn't store the remaining "3" in the buffer after input then how does it get back the 3, during the conversion.
Does getchar() have it's own buffer where it stores all the inout characters and fetch them 1 by 1.
Any help is highly appriciated.