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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:08:28+00:00 2026-05-27T16:08:28+00:00

Quoting from Kernighan and Ritchie’s ‘The C Programming Language’ Page 16 – #include<stdio.h> main()

  • 0

Quoting from Kernighan and Ritchie’s ‘The C Programming Language’ Page 16 –

#include<stdio.h>

main()
{
int c;
c = getchar();

while(c!=EOF)
{
    putchar(c);
    c = getchar();
} 

getchar();
return 0;
}

“The type char is specifically meant for storing such character
data, but any integer type can be used. We used int for a subtle but
important reason. The problem is distinguishing the end of the input
from valid data. The solution is that getchar returns a distinctive
value when there is no more input, a value that cannot be confused
with any real character. This value is called EOF, for “end of
file”. We must declare c to be a type big enough to hold any value
that getchar returns. We can’t use char since c must be big
enough to hold EOF in addition to any possible char. Therefore we
use int.”.

I looked up in stdio.h, it says #define EOF (-1)

The book conclusively states that char cannot be used whereas this program “works just fine” (See EDIT) with c as char data type as well. What is going on? Can anyone explain in terms of bits and signed values?

EDIT:
As Oli mentioned in the answer, the program cannot distinguish between EOF and 255. So it will not work fine. I want to know what’s happening – Are you saying that when we do the comparison c!=EOF, the EOF value gets cast to a char value = 255 (11111111 in binary; i.e. the bits 0 through 7 of EOF when written in 2’s complement notation)?

  • 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-27T16:08:29+00:00Added an answer on May 27, 2026 at 4:08 pm

    Your program doesn’t work fine; it won’t be able to distinguish between EOF and 255.

    The reason it appears to work correctly is because char is probably signed on your platform, so it’s still capable of representing -1.

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

Sidebar

Related Questions

Quoting from the C-std section 6.7.2.1, struct s { int n; double d[]; };
quoting from The C++ Standard Library by N M Jousttis, Section 5.9 #include <
Quoting from Code Complete 2 , int Factorial( int number ) { if (
Quoting The C++ programming language (special edition, section 4.9.6, Objects and Lvalues), and as
Quoting from the cakephp Book (ver 1.3): Note that only fields of the model
I've been quoting this segment from Sun's document for the past few days, and
I am reading the dragon book. Quoting the text from the book (3.1.4 Lexical
Quoting the code for computing the integer absolute value (abs) without branching from http://graphics.stanford.edu/~seander/bithacks.html
Quoting from Perl::Critic::Policy::InputOutput::ProhibitExplicitStdin Perl has a useful magic filehandle called *ARGV that checks the
Quoting the instructions for building OpenSSL: Then from the VC++ environment at a prompt

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.