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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:55:36+00:00 2026-06-01T07:55:36+00:00

Below is the program that I wrote. /******************************************************************************* * This program reads EOF from

  • 0

Below is the program that I wrote.

/*******************************************************************************
 * This program reads EOF from standard input stream to store in an integer 
 * variable and in a character variable. Both values are then output to see the
 * stored value as integer.
*******************************************************************************/
#include<stdio.h>

int main (void)
{
    /* Local Declarations */
    int        num;
    char       ch;

    /* Read EOF as character & integer and display values stored */
    printf("\nPlease only input EOF for all input prompts below");
    printf("\nNumber? ");
    scanf("%d", &num);
    printf("\nThat integer input converts to %d", num);
    printf("\nCharacter? ");
    scanf(" %c", &ch);
    printf("\nThat character input converts to %d", ch);

    /* Check if any of the stored values are recognized as EOF */
    if(num == EOF)
        printf("\nNumber is an EOF");
    if(ch == EOF)
        printf("\nCharacter is an EOF");

    /* Exit Program */
    printf("\n");
    return 0;
}// main()

I compiled using gcc on Ubuntu 11.10. It does not recognize Ctrl–D for EOF in the program and outputs 0 when trying to print its value. The output for the above program on my terminal is below.

Please only input EOF for all input prompts below
Number? 
That integer input converts to 0
Character? 
That character input converts to 0

NOTE: CTRL-D is not echoed by the terminal when I press it for the two inputs, hence not visible in the above program execution.

I have read that EOF is defined as an integer in stdio.h and stdlib.h and is traditionally defined as -1. Also, I understand that Ctrl–D simulates EOF for standard input. Then why does it not translate to -1 when storing it to an integer variable?

  • 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-01T07:55:38+00:00Added an answer on June 1, 2026 at 7:55 am

    Because scanf("%d") will attempt to read characters representing a number and then store that into the given variable.

    It does not store EOF into the number if you close the stream. Otherwise how would you tell the difference between end of file and entering the numeric value of EOF (-1).

    The scanf will return the number of items scanned (which will be zero if format was wrong, or EOF if the stream closed or certain errors occurred). It returns this as the return code from the scanf function, not via the pointer variable you passed to it.

    You should only assume the variables (that you pass in the addresses for) are populated if the return value from scanf tells you so.

    In other words, you’re looking for something like:

    int rc = scanf ("%d", &num);
    switch (rc) {
        case EOF: {
            printf ("EOF returned\n");
            break;
        }
        case 0: {
            printf ("No items scanned\n");
            break;
        }
        default: {
            printf ("Scanned %d\n", num);
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program below that tries to take input from the user and
Below I have written a sample program that I have written to learn about
I need to create program that creates a XML schema like below using System.Xml.XmlSchema
In the program below, there are two things that I don't understand. How can
In the below program: class Main { static string staticVariable = Static Variable; string
In this below program i am printing the contents of the div when i
So I wrote a program that depending upon a given 'model' it generates a
I am writing this chat program that uses ncurses as the interface. How am
The short program below is intended to iterate over argv passed from the command
I wrote one program that load a text file. it work well. I try

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.