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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:40:18+00:00 2026-06-06T00:40:18+00:00

In the book Linux System Programming I have read some like this: fgetc returns

  • 0

In the book Linux System Programming I have read some like this:

fgetc returns the character read as an unsigned char cast to an int or
EOF on end of file or error. A common error using fgetc is:

char c;
if ((c = fgetc()) != EOF) {...}

The right version of this code is:

int c;
if ((c = fgetc()) != EOF) { printf("%c", (char)c); ... }

So, why can’t I cast a return value to char before comparing with EOF? Why do I have to compare EOF exactly with int? As EOF defined as -1, isn’t it normally casted to char?
Are there platforms/compilers where it is not true?

  • 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-06T00:40:19+00:00Added an answer on June 6, 2026 at 12:40 am

    You can’t cast the return value to char because the return value could be EOF, and EOF value is system-dependent and is unequal to any valid character code. link

    Usually it is -1 but you should not assume that.

    Check this great answer from the c-faq-site:

    Two failure modes are possible if, as in the fragment above, getchar’s
    return value is assigned to a char.

    1. If type char is signed, and if EOF is defined (as is usual) as -1, the
      character with the decimal value 255 (‘\377’ or ‘\xff’ in C) will be
      sign-extended and will compare equal to EOF, prematurely terminating
      the input. (assuming 8 bits char).

    2. If type char is unsigned, an actual EOF value will be truncated (by
      having its higher-order bits discarded, probably resulting in 255 or
      0xff) and will not be recognized as EOF, resulting in effectively
      infinite input.

    Hope it helps!

    Edited: (added the @FatalError comment on this answer, this is explained on the c-faq site but this looks more clear to me)

    "If you cast it to char then EOF takes the same value as some valid character and hence becomes indistinguishable from that character. That alone should be enough justification to not make the result a char" @FatalError comment.

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

Sidebar

Related Questions

This example is taken from the Linux System Programming book, page 88. Code: http://pastebin.com/mEfmHbPP
This is sample program from Beginning Linux Programming book: #include <stdio.h> #include <term.h> #include
This is a quote from Linux programming book: % gcc -o app app.o -L.
I have some questions regarding read-write locks in POSIX Pthreads on a *nix system,
I want a more C++/Linux oriented book. I do have some basics of multithreading/parallel
I am studying the book Beginning Linux Programming 4th ed and chapter 2 is
I have started reading the book Essential Linux Device Drivers. I am following the
I am reading linux device driver book of rubini,corbet and hartmen.I have doubt regarding
I would like to learn about linux/Unix kernel programming for scalable multi processors (smps).
The Linux Programming Interface book has a piece of code (producer/consumer) to show how

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.