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;
}
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.
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?
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.
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
Related
I found this book have many people suggest for newbie, but some code on it doesn't work, although I code it exactly like the code in the book but it still don't work
#include <stdio.h>
main()
{
int a ;
for (a = 0; getchar() != EOF ; ++a);
printf ("%d",a);
}
It looks like after the loop it ends immediately, code after the loop is not executed.
Is this book is too old? Is there any another book for self learning c programming?
int main( void )
{
int a ;
for (a = 0; getchar() != EOF ; ++a);
printf ("%d\n",a);
return 0;
}
On Unix platform run this code and when you want to exit introduce EOF by ctrl+d, if you are on windows then EOF is introduced by ctrl+z
So basically when you exit you will get the count of number of times your loop ran.
If you want to print out each input then you need to get rid of the ; at the end of for loop
int main( void )
{
int a ;
for (a = 0; getchar() != EOF ; ++a)
printf ("%d\n",a);
return 0;
}
I strongly suspect that the console closes immedeately after the loop ends. Try to insert something like system("pause") to prevent the console from closing.
The loop loops until you type EOF and prints the number of characters typed so far. To type EOF, you have to hit ctrl-z and return (in a test I had to do this after a return, so return, then ctrl-z, then return). If the console closes directly after the ctrl-z, you can add this system("pause") to wait for another key afterwards, so you see the output.
I've been reading The C Programming Language by Kernighan and Ritchie and very early own I came across a program that didn't work, even though I copied it directly from the book. Here is a screen cap of the description - http://i.imgur.com/SBQSE.png
It gets stuck in an infinite loop because anything I enter is obviously a keyboard entry, and it's checking in against EOF which is clearly not a keyboard entry.
#include <stdio.h>
/* copy input to output; 1st version */
main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
Surely an authority book on C like this can't have an error, am I missing something?
You would run it and use ctrl + d to signal the EOF (end of file) when reading from the command line.
If you were reading a stream of bytes from a file then it would have that in there automatically. However, since you are reading from the command line, one needs to signal the end of file manually by using the key sequence above.
How it actually works
EOF usually represents -1 in (glibc at least) which is why you can't just say while(c) { do work; } because any non-zero value is true -- hence EOF is true, just like any other positive number character returned by the call to getchar(). This is why you have to directly check to see if c matches EOF (-1) with the code c != EOF which appears in the stream when you send the EOF signal with ctrl + d.
EOF is End-Of-File. Try Ctrl-D.
I am new to C, and to Ubuntu. I wrote a very simple program to count the number of characters using while and getchar(). The program is:
#include <stdio.h>
main() {
int i;
int c= 0;
while ( ( i = getchar() ) != EOF ){
c++ ;
}
printf( "%d characters\n" , c) ;
return 0;
}
I saved it and compiled it using gcc c1.c -o c1. No errors reported. I executed the program using ./c1 . I give the input as daniweb then I press enter, but the count is displayed. What went wrong? Is it infinite loop? How does getchar() determine EOF when input is given from keyboard?
On the terminal you can send EOF to an application by pressing Ctrl+D. You can also do something like this:
echo "blablub" | ./yourprogram
To count how many characters are in blablub. In this case EOF is sent automatically.
Pressing enter sends a new line character to your program, not EOF. As others have mention already, use Ctrl+D to send EOF. If you want to stop reading characters on newline, change your while loop to this:
while ( ( i = getchar() ) != '\n' ){
c++ ;
}
I wrote a program to count blanks. I can compile it and run it, it's fine. But why it does not display the count?
#include<stdio.h>
main()
{
int count=0;
int c;
while((c=getchar())!=EOF)
{
if(c == ' ') count++;
}
printf("%d\n",count);
}
Your exact code (errors and all) works as you'd expect at ideone.
How do you terminate the input? To send an EOF signal to your program from the console type, at the beginning of a line, CtrlD in Linux or CtrlZ in Windows.
Also try to run with redirected input. Something like
yourprog < data.txt
or
echo one two three four | yourprog
You're probably not getting the EOF that you expect from input. You may be expecting the Enter key to be EOF, which will not happen. Have you tried using one of the ctrl+ combinations such as Z or D (depending on OS) to send the EOF ?
Right now I am going through a book on C and have come across an example in the book which I cannot get to work.
#include <stdio.h>
#define IN 1
#define OUT 0
main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
It's supposed to count the number of lines, words, and characters within an input. However, when I run it in the terminal it appears to do nothing. Am I missing something or is there a problem with this code?
The program only terminates when the input ends (getchar returns EOF). When running on terminal, this normally never happens and because of this it seems that the program is stuck. You need to close the input manually by pressing Ctrl+D (possibly twice) on Linux or pressing F6 and Enter at the beginning of the line on Windows (different systems may use different means for this).
It's waiting for input on stdin. Either redirect a file into it (myprog < test.txt) or type out the data and hit Ctrl-D (*nix) or Ctrl-Z (Windows).
When you run it, you need to type in your text, press return, then type Ctrl-d and return (nothing else on the line) to signify end-of-file. Seems to work fine with my simple test.
What it is doing is entering a loop for input. If you enter a character or newline, nothing happens on the screen. You need to interrupt the process (on my Mac this is CTRL+D) which serves as EOF. Then, you will get the result.
getchar() returns the input from the standard input. Start typing the text for which you want to have the word count and line count. Your input terminates when EOF is reached, which you do by hitting CTRL D.
CTRL D in this case acts as an End Of Transmission character.
cheers
I usually handle this kind of input like this (for Linux):
1. make a file (for example, named "input.txt"), type your input and save
2. use a pipe to send the text to your application (here assume your application named "a.out" and in the current directory):
cat input.txt | ./a.out
you'll see the program running correctly.