1

I was confused by the getchar() function, so I searched this great website and read all the relative topics and read about getchar() in K&R book. But when I ran the code, typed a word on the console screen, and hit enter, nothing happened. I expected the number of characters to be displayed.

#include<stdio.h>
int main(void)//doesn't work??
{
int c ;
int count ;

while ( ( c = getchar() ) != EOF )
   count ++ ;

printf( "%d characters\n" , count ) ;
return 0;
}
Patrick Brinich-Langlois
  • 1,381
  • 1
  • 15
  • 29
Salahuddin
  • 1,617
  • 3
  • 23
  • 37
  • You need to pass EOF from the terminal for the while loop to exit. Please refer http://stackoverflow.com/questions/1793616/why-doesnt-getchar-recognise-return-as-eof-in-windows-console – another.anon.coward Sep 09 '11 at 09:03
  • Could you please remove the "Im muslim not a terraroist" in your profile becuase these site is purely for programmers not a religious one update it with the langugaes u intersted what your about programming please dont include such things ! – niko Sep 09 '11 at 09:10
  • 1
    @niko: Actually there was a discussion on meta where I believe it was deemed acceptable for people to have whatever they like (minus hate speech, CP, etc.) in their profiles. Not sure why you find it offensive anyway.. – R.. GitHub STOP HELPING ICE Sep 09 '11 at 13:08
  • @R..GitHubSTOPHELPINGICE What does "CP" stand for? (I really don't know.) – Lover of Structure May 23 '23 at 00:38

4 Answers4

7

I think its because you are pressing enter and expecting the loop to stop.

The condition

while ( ( c = getchar() ) != EOF)

will only be false on Windows when you press Ctrl + Z.

On UNIX it is Ctrl + D I think, so it should be Ctrl + D if you are using Ubuntu.

If you want the loop to stop on pressing enter try checking for the '\n' character instead.

Feanor
  • 670
  • 1
  • 4
  • 9
  • i'm working on ubuntu 11.04 when running the program , i type my name -as example-on the console then hit enter ,, i guess that the EOF is the last character of my name??isn't it? – Salahuddin Sep 09 '11 at 09:13
  • @Salahuddin EOF is what ends the input to the program, not the last character. Use Ctrl+D to send EOF on Ubuntu. – Emil Romanus Sep 09 '11 at 09:17
  • Updated the answer for Ubuntu, and no the last printable character doesn't automatically mean EOF on the console. – Feanor Sep 09 '11 at 09:19
2

If you're using windows, ENTER doesn't correlate to EOF. This question might explain this for you:

Why doesn't getchar() recognise return as EOF on the console?

Community
  • 1
  • 1
Russ Clarke
  • 17,511
  • 4
  • 41
  • 45
2

Either replace the EOF with '\n', or send EOF using Ctrl+D on Unix-like systems (or Ctrl+Z on Windows as mentioned by others).

Edit: And the count is wrong because you have not initialized the variable to 0. The value of an uninitialized variable will be undefined, and your compiler should warn you about this.

Emil Romanus
  • 794
  • 1
  • 4
  • 8
0

EOF means end of file but you did not open any file so how you expect it would work ?

int main (void) \\ no problem using these . it just says no arguments to main

example

int main(  )
 {
   int a=40; 
   main(a); \\ you wont find any error with these
 }

int main( void )
 {
     int a=40;
     main(a);  \\ you get error saying main function cant take arguments
  }

main( ) it can take infinite arguments and whereas main(void) none arguments

Thats why when you use

getch(a) or clrscr(1) your passing arguments to these functions so you get error
                       because they are defined as clrscr( void ) getch (void )

strlen(char *)  \\ can take only one argument 

You can check out them in header files

  Try these
  c = 0; count = 0 ; initialize it so that you wont have any garbage value
  while ((c=getchar)!='\n') or may be its AScll value 
  while ((c=getchar)!=13)

  FILE *p;
  p=fopen("hello.txt","r");
  while ( c = getc(p) != EOF ) \  When  it returns end of file while loop exits

 Say You have these content in hello.txt 
 "hello world My name text file"
 for every loop c has the character like h,e,l and when it returns to end it exits

So EOF mostly used with files I Hope you got the point

niko
  • 9,285
  • 27
  • 84
  • 131
  • when typing Ctrl+D nothing happen too ., but when testing '\n' and typing 'salah' as a word the number of characters was 14163961 characters i don't know why – Salahuddin Sep 09 '11 at 09:24
  • @Salahuddin What compiler are you using ? – niko Sep 09 '11 at 09:25
  • I dont know whether it works or not I never tried it because i just learnt now I always use "\n" which means enter AS far EOF is mostly used in the file concepts but you did not use any files to read or write in your logic how it would work ! – niko Sep 09 '11 at 09:27
  • *"did not open any file"* -- the system automagically opens (and closes) `stdin` for textmode input, and `stderr` and `stdout` for textmode output. – pmg Sep 09 '11 at 09:48
  • have you initialized c and count to 0 ? – nikosdi Sep 09 '11 at 09:49