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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:56:36+00:00 2026-06-06T06:56:36+00:00

I am reading PrenticeHall. The C Programming Language – 2nd Ed.Kernighan,Ritchie. In this book(pg-20)

  • 0

I am reading PrenticeHall. The C Programming Language – 2nd Ed.Kernighan,Ritchie.

In this book(pg-20) an example of a program is given which is supposed to print the number of characters that user types in the console window, and here is its code .

#include <stdio.h>

main()
{
    double nc;
    for (nc = 0; getchar() != EOF; ++nc)
        ;

    printf("%.0f\n", nc);
}

But when I run this , and type something in the console, it wont print anything at all, the cursor would keep on blinking .
and this is exactly the way code is written in that book.

I have tried it another way also, but this also didn’t work out, same result as that previous code.

#include <stdio.h>

main()
{
    long nc;
    nc = 0;
    while (getchar() != EOF)
            ++nc;

    printf("%ld\n", nc);
}

any ideas to how to make this thing work ?

P.S.
I am using windows OS. (still)

  • 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-06T06:56:38+00:00Added an answer on June 6, 2026 at 6:56 am

    The reason is that the code tries to read all the input until the end of file. If this program was reading from a file, it would know when it ended, but since it’s reading from console, you have to explicitly tell it that the input is over. On Linux you do this by pressing ^D (Ctrl+D), on Windows ^Z (Ctrl+Z).

    Note that it has to be done at the start of a new line, i.e. after pressing Enter, you press ^D (^Z on Win).

    I am reading PrenticeHall. The Ansi C Programming Language – 2nd
    Ed.Kernighan,Ritchie.

    That’s a very good book you’re reading. It’s that good, I guess, it contains the answer to your question.

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

Sidebar

Related Questions

Reading the Scala by Example book and there is this example when Martin explains
Reading PHP & jQuery Cookbook right now and it gives this example for using
Reading this brief example of using memcached with PHP, I was wondering how memcached
reading the SCJP book, I've found something like this in the chapter 1 self-test
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
Reading code from other posts, I'm seeing something like this. struct Foo { Foo()
Reading this site, I've found this : [The] line private static final Foo INSTANCE
Reading the Google Docs API I find this: Downloading Files cannot be downloaded in
Reading Real World Haskell, on page 95 the author provides an example: myFoldl f
Reading over some example Objective C code just now. @property (nonatomic, strong) IBOutlet UILabel

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.