Thread: how does the getint() function from "THE C PROGRAMMING BOOK" works ?

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    1

    Exclamation how does the getint() function from "THE C PROGRAMMING BOOK" works ?

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Does getchar() have it's own buffer where it stores all the inout characters and fetch them 1 by 1.
    Yes.

    Or more specifically, the standard input stream is usually line buffered, meaning the OS / standard library will buffer characters outside your program until return is pressed. Once pressed, your program code will read each character in turn up to the newline. At which point, the whole thing begins again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-29-2016, 10:45 AM
  2. Replies: 5
    Last Post: 10-12-2012, 06:17 AM
  3. Replies: 4
    Last Post: 11-19-2011, 07:57 PM
  4. Question on the book "C Programming Language" by K&R
    By noahian in forum C Programming
    Replies: 3
    Last Post: 09-04-2011, 02:56 PM
  5. Question about the book "The C Programming Language"
    By caduardo21 in forum C Programming
    Replies: 4
    Last Post: 05-15-2005, 01:22 PM

Tags for this Thread