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

  • Home
  • SEARCH
  • 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 8821921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:58:24+00:00 2026-06-14T05:58:24+00:00

When I do practice on K&R,I found a very interesting question: code as follows:

  • 0

When I do practice on K&R,I found a very interesting question:

code as follows:


include <stdio.h>
main()                                                                                              
{                                                                                                   
    int c;
    int bn;
    bn=0;
    while((c=getchar())!=EOF)                                                                       
    {                                                                                               
        if(c==' ')                                                                                  
        bn++;                                                                                   
    }                                                                                           
    printf("blanks counter:%d\n",bn);                                                               
} 

code function is number of statistical blankspaces

I enter all the words at once ,no use backspace key,it is done.


$ ./a.out

I have a dream

blanks counter:3

however,if I use backspace key,what happen may be in input process?

I speculate when a blankspace key is pressed ,getchar() function should get this event,and then perform bn++ ,so even if I later press a backspace key to delete this blankspace ,the value of bn variable is not change.
but the result of practice is different of my speculate, the programe of practice as follows:

the first step:input first blankspace
enter image description here

the second step:use backspace key delete thie blankspace
enter image description here
the third step:finish the remaining input of characters
enter image description here

why result is 3 not 4?why value of bn variable would be changed by pressed backspace key?

please give me some ideas,thanks!

  • 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-14T05:58:25+00:00Added an answer on June 14, 2026 at 5:58 am

    Your console is line buffering stdin. This means that your program will not receive any input until you hit Return, and it will receive the edited text, not the actual sequence of keystrokes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A while ago, I found in a website some code examples of utility functions
So I'm trying to do the practice exercises in K&R. It wants me to
I'm currently working on some R&D for improving the process and practice that we
Many client side code libraries & tool kits, for example Yahoo's YUI and Google's
Which is best practice (in this case): bool Foo::operator==(const Foo& other) { return bar
Although there are quite a lot of Q&As regarding IDisposable to be found on
I understand that this question is based on a very old programming language, and
I have been searching for best practice, but I did not found it. I
I have a question for which I found a rather long solution of which
Its been a while I have ready Mcconnell's Code Complete . Now I read

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.