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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:29:43+00:00 2026-05-26T11:29:43+00:00

For reference, I’m copying this example (nearly) letter for letter from page 18 in

  • 0

For reference, I’m copying this example (nearly) letter for letter from page 18 in The C Programming Language, Second Edition.

#include <stdio.h>
/*count characters in input, 2nd version*/
main(){
    double n;
    for (n = 0; getchar() != EOF; ++n)
        ;
    printf("%.0f\n", n); /*this never prints*/
}

I wasn’t sure if it was my version of gcc (I’m a noob):

% gcc --version
gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.

Because

for (n = 0; getchar() != EOF; ++n)
    printf("%.0f",n); /*returns 0123456...n*/

I tried printing “foo” instead of value n, just to see. It still isn’t printing.

What am I missing here? I don’t like moving ahead while ignoring little problems like these.

=====================================

EDIT

So the end result should be:

% gcc ./counter.c -o ./counter
% ./counter
foo
3

Right now, this is the output from the first snippet:

% ./counter
foo
0123

And the second one:

% ./counter
foo

^C
%
  • 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-05-26T11:29:44+00:00Added an answer on May 26, 2026 at 11:29 am

    If you are on an OSX or linux box, you need to type Ctrl+D on its own line to generate an EOF character. On Windows, Ctrl+Z on its own line. Don’t type Ctrl+Z on a unix box because that will just send your proces to the background.

    You are typing Ctrl+C which is break, and will send a SIGTERM to your program.

    On my mac, I get:

    $ ./foo
    hello
    6D
    

    Or if you don’t want to signal the EOF condition, use echo and a pipe:

    $ echo "hello" | ./foo 
    6
    

    Note that the EOF has to be on its own line. a ^D is printed, and then 6 overwrites the ^, so it looks like the output is 6D.

    Of course, in the two above examples, the characters being counted are h e l l o \n. If you don’t want a newline, do this:

    $ echo -n "hello" | ./foo 
    5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In reference to this bug from Sonar: http://jira.codehaus.org/browse/SONAR-1865 and this one (which cross references
For reference, please visit this page here . When trying to get a view
The reference says this: You should call this function from the main thread of
Reference is this page: http://demo.mypreviewbetasite.com/laverona/menu.html File in question: http://demo.mypreviewbetasite.com/laverona/scripts/menu.js The page works as expected
With reference to this programming game I am currently building. I wrote the below
With reference to this programming game I am currently building. Thanks to the answers
With reference to this programming game I am currently building. I have a Class
With reference to this programming game I am currently building. When the game is
With reference to this MSDN article , at the end (Section: Robust Programming) it
Please reference the this example : I'm trying to create vertical dividers between my

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.