Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:08:00+00:00 2026-06-09T15:08:00+00:00

I have a big problem understanding several lines from the following code (numbers marked

  • 0

I have a big problem understanding several lines from the following code (numbers marked in comments):

First – the code of a loop that fills an array with the input data:

int n, array[SIZE], getint(int *);
for (n = 0; n < SIZE && getint(&array[n]) != EOF; n++)
      ;

Now the function definition:

/* getint:  get next integer from input into *pn */

int getint(int *pn)

{

int c, sign;

while (isspace(c = getch()))   /* skip white space */
        ;

if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
        ungetch(c); /* [1] */ /* it is not a number */
        return 0;  /* [2] */
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
        c = getch();
for (*pn = 0; isdigit(c); c = getch())
        *pn = 10 * *pn + (c - '0'); [3]
*pn *= sign;
if (c != EOF) /* [4a] */
        ungetch(c); /* [4b] */
return c; 
}



#define BUFSIZE 100

char buf[BUFSIZE];      /* buffer for ungetch */
int bufp = 0;           /* next free position in buf */

int getch(void) /* get a (possibly pushed-back) character */
{
   return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c)     /* push character back on input */
{
   if(bufp >= BUFSIZE)
      printf(" ungetch too many characters\n");

   else
      buf[bufp++] = c;
}

So:

[1] I’ve read similar posts here, that taking back such an unwanted char somehow blocks the buffer, so that we need to clear it using another function. For me it’s strange that is not included in K&R and authors don’t even mention the necessity to use it?

[2] Why do we return 0? Does that stop the whole main() program? Or does it simply put 0 in an array ? ( getint(&array[n] ) ?

[3] Why do we need to implement such a formula to calculate “big numbers” ? Since that function just gets numbers one after the other (getchar not getword) and then create “big number” by means of several single integers.

[4a][4b] Why does it ungetch if c != EOF? This condition is fulfilled most of the time, so we would end up with rejecting every inputted number?

Thanks in advance for answering!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:08:01+00:00Added an answer on June 9, 2026 at 3:08 pm
    1. It doesn’t, and even if it did with some other library, not with these 2 functions
    2. Nope. It simply returns zero and leaves the integer uninitialized (this case isn’t handled in the loop)
    3. What do you mean? This is simply how you calculate an integer from it’s digits.
    4. It will simply ungetch the character that was trailing the integer the function just read and processed – unless there was no character but an end of stream marked by EOF. That marker isn’t returned to the buffer.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big problem. I want to extract text from html table that
The Android documentation being the mess that it is, I have big problem understanding
I'm using editor from Kendo UI, so I have big problem. I don't know
I have a big problem that a recursive treeview doesn't update any childs if
I have a big problem that is I have implemented a scheduler in my
I have a (big) problem that all of you that work with Webforms might
I am going over the Big-Oh notation, and I have a problem understanding the
I have a big problem. There are devices in live that send the URL
I have a problem with the understanding MVC architecture. It's not that I don't
I have big problem. Lets look on the code below: protected void Application_AuthenticateRequest(object sender,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.