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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:41:31+00:00 2026-06-10T07:41:31+00:00

My question is simply Why does my code on line 10 and 11 not

  • 0

My question is simply “Why does my code on line 10 and 11 not function properly?” The intended purpose of my code is to do exactly as the original K&R code intended, but to NOT count nc whenever (getchar() == ‘\n’) will you please enlighten me?

slightly modified K&R code:

/** K&R - 1.5.2 Character Counting **/
#include <stdio.h>

/* count characters in input; 1st version */
main(){
  long nc;

  nc = 0;
  while (getchar() != EOF){
    if (getchar() != '\n'){
      ++nc;
    }
  }
  printf("%ld\n", nc);
}

I use 64-bit Windows 7, CodeBlocks10.05, GNU GCC Compiler.

my current progress and understanding:

on a sample run, i type in the word two and hit enter, which equals 4 inputs, after which i press ctrl+Z to enter in a ^Z or EOF character. The program then prints 1. I was expecting it to print 3. I suppose the only logical explanation is that it is doing exactly the opposite of what I intended (it only counts newline characters?). As it turns out, if I type in the word two and press enter, lets say 4 times, it prints 4. It seems to be counting nc for every newline character entered, but yet if I press enter alone (in this case 4 times) and then EOF, it always prints 0. Upon further experimentation, by some hand unseen 4 is perhaps a magical number for this program. If I start it up and hit enter key exactly (a number divisible by 4) times and then EOF it prints 0. However if i hit enter some other number of times the EOF does nothing, and I must enter in ^Z two rows, one after the other, to end the while loop correctly, and it prints 1. This is boggling my mind!

  • 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-10T07:41:33+00:00Added an answer on June 10, 2026 at 7:41 am

    The trouble is that you need to save the value from getchar() – in an int – because you are reading two characters for each time you increment the count. One of those is in the EOF test; the second is in the newline test.

    int c;
    
    while ((c = getchar()) != EOF)
    {
        if (c != '\n')
            ++nc;
    }
    

    The reason you need to store the result of getchar() in an int and not a char is that it can return every possible char value and also a distinct value, EOF. If you don’t use int (you store direct into a char), one of two things will happen:

    1. If char is a signed type, a legitimate character (often y-umlaut, ÿ, LATIN SMALL LETTER Y WITH DIAERESIS, U+00FF — at least in codesets derived from Latin 1 or ISO 8859-1) will be interpreted as equivalent to EOF, and your program will terminate prematurely.
    2. If char is an unsigned type, no character will ever be equivalent to EOF, so the program will never stop the loop.

    Neither of these circumstances is desirable. Storing the return value of getchar() in an int prevents both problems; it is the ‘only’ (or, at least, the simplest) correct way to do it.

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

Sidebar

Related Questions

My question is this: Given a single line of isolated code (ie., does not
Is this the safest/optimal code for a simple does the record exist question: .
* Simple question : Why does this function throw an exception when evaluated ?
I'm embarrassed to ask such a simple question. My term does not start for
I think its quite a simple question but not sure. I have a class:
I have tried some of the existing solutions but it does not work. I
Not sure if my question is subjective/objective but as a JavaScript newbie i'm encountering
Simple question: does the Java memory/synchronization model guarantee atomic pointer writes? That is, if
Simple question - what does this actually do? var oq = (ObjectQuery<TEntity>)L2EQuery; return ExecuteFirstorDefault<TEntity>(oq,
Very simple question. What does the term 'seeding' mean in general? I'll put the

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.