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 8923279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:04:44+00:00 2026-06-15T07:04:44+00:00

I’m working my way through The C Programming Language (K&R), and did one of

  • 0

I’m working my way through “The C Programming Language” (K&R), and did one of the exercises. The exercise was to print the number of spaces, tabs and newlines until EOF. Great, that works. But, I started getting clever and added the printf()’s you see below that actually say, “Space” or “Tab” or “newline”. It should print that as I type, but, it the while loop only seems to run once I hit enter.

The code:

#include <stdio.h>

int main(int argc, const char * argv[])
{

    int c, nb, nt, nl;

    nb = nt = nl = 0;

    while ((c = getchar()) != EOF) {
        if (c == ' ') {
            nb += 1;
            printf("Space\n");
        }
        if (c == '\t') {
            nt += 1;
            printf("Tab\n");
        }
        if (c == '\n') {
            nl += 1;
            printf("New Line\n");
        }

    }

    printf("%d spaces, %d tabs, %d newlines",nb,nt,nl);

    return 0;
}

The output:

a b
Space
New Line
c d e
Space
Space
New Line
f   g
Tab
New Line
3 spaces, 1 tabs, 3 newlines

What I expected to see was something more like this:

a Spaceb
New Line
c Spaced Spacee
New Line
f   tabg
New Line
3 spaces, 1 tabs, 3 newlines

So, why do I get what I get, and not what I expect?

And, yes, if I was really caring, I’d make the words ‘space[s]’ ‘tab[s]’ and ‘newline[s]’ plural vs. not based on the value, but, it’s just an exercise. 😉

  • 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-15T07:04:45+00:00Added an answer on June 15, 2026 at 7:04 am

    Your terminal is in canonical mode – characters are accumulated until end of line. Only then are they send to user (one by one).

    If you want immediate response, you have to switch the terminal to raw mode.

    PS. More or less, like this:

    struct termios t;
    
    tcgetattr (0, &t);
    
    t.c_lflag &= ~ICANON;
    t.c_cc [VMIN] = 1;
    t.c_cc [VTIME] = 0;
    
    tcsetattr (0, TCSANOW, &t);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am reading a book about Javascript and jQuery and using one of the
I am trying to loop through a bunch of documents I have to put
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.