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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:46:05+00:00 2026-05-25T12:46:05+00:00

Code: #include <stdio.h> #define NEWLINE ‘\n’ #define SPACE ‘ ‘ int main(void) { int

  • 0

Code:

#include <stdio.h>
#define NEWLINE '\n'
#define SPACE ' '

int main(void)
{
    int ch;
    int count = 0;

    while((ch = getchar()) != EOF)
    {
        if(ch != NEWLINE  && ch != SPACE)
            count++;
    }
    printf("There are %d characters input\n" , count);

    return 0;
}

Question:

  1. Everything works just fine, it will ignore spaces and newline and output the number of characters input to the screen (in this program I just treat comma, exclamation mark, numbers or any printable special symbol character like ampersand as character too) when I hit the EOF simulation which is ^z.

  2. But there’s something wrong when I input this line to the program. For example I input this: abcdefg^z, which means I input some character before and on the same line as ^z. Instead of terminating the program and print out total characters, the program would continue to ask for input.

  3. The EOF terminating character input only works when I specify ^z on a single line or by doing this: ^zabvcjdjsjsj. Why is this happening?

  • 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-25T12:46:05+00:00Added an answer on May 25, 2026 at 12:46 pm

    This is true in almost every terminal driver. You’ll get the same behavior using Linux.

    Your program isn’t actually executing the loop until \n or ^z has been entered by you at the end of a line. The terminal driver is buffering the input and it hasn’t been sent to your process until that occurs.

    At the end of a line, hitting ^z (or ^d on Linux) does not cause the terminal driver to send EOF. It only makes it flush the buffer to your process (with no \n).

    Hitting ^z (or ^d on Linux) at the start of a line is interpreted by the terminal as “I want to signal EOF”.

    You can observe this behavior if you add the following inside your loop:

    printf("%d\n",ch);
    

    Run your program:

    $ ./test
    abc                      <- type "abc" and hit "enter"
    97
    98
    99
    10
    abc97                    <- type "abc" and hit "^z"
    98
    99
    

    To better understand this, you have to realize that EOF is not a character. ^z is a user command for the terminal itself. Because the terminal is responsible for taking user input and passing it to processes, this gets tricky and thus the confusion.

    A way to see this is by hitting ^v then hitting ^z as input to your program.

    ^v is another terminal command that tells the terminal, “Hey, the next thing I type – don’t interpret that as a terminal command; pass it to the process’ input instead”.

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

Sidebar

Related Questions

consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
#include <stdio.h> #define MAXLENGTH 77 int getline(char[], int); main() { char in_line[MAXLENGTH]; int count,
Consider this code: #include <stdio.h> #define N 5 void printMatrix(int (*matrix)[N],int n) { int
I came across this following code: #include<stdio.h> #define d(x) x(#x[3]) int main(){ d(putchar); }
The following code works fine #define open { #define close } #include<stdio.h> #define int
I have the following code: #include <stdlib.h> #include <stdio.h> #define SIZE 100 int* arr;
#include vss.h #include vswriter.h #include <VsBackup.h> #include <stdio.h> #define CHECK_PRINT(result) printf(%s\n,result==S_OK?S_OK:error) int main(int argc,
I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100

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.