Tuesday, July 28, 2009

The C Programming Language book has something called EOF!?

An exercise with the end-of-file! That exercise is some program that mimics anything u put in





Here is the code of the program





#include %26lt;stdio.h%26gt;





main()


{


int c;


c = getchar();


while (c != EOF)


{


putchar(c);


c = getchar();


}


}





I do not understand this program! First of all, why does it have c = getchar 2 times???? second, what does that eof do??








Here, is my version of the same program:


#include %26lt;stdio.h%26gt;





main()


{


while(1)


{


char z;


z = getchar();


putchar(z);


}


}





It is shorter, it works the same! But my question is, I used char type variable??? why does it work when I put in multiple characters at once???


It should work only with one character! I am very confused!


Pls help me

The C Programming Language book has something called EOF!?
Hello,





1) With regard to the first code listing:





The first 'c = getchar()' is executed before the loop begins, while the second 'c = getchar()' is executed as part of the loop. EOF is a special integer value returned by getchar() to indicate that the stream has ended. In pseudocode:





read a value from the input stream


while the input stream has not ended:


  write the value to the output stream


  read a value from the input stream





This program will end when the input stream ends, or if it is terminated by Ctrl-C.





2) With regard to the second code listing:





There are several differences. First, by reading a char value rather than an int value, it is unable to detect the EOF condition. Also, by not checking for EOF, this loop will not terminate when the input stream ends. Finally, when EOF is read from the input stream, it will be written out to the output stream as though it is a valid character. In pseudocode:





infinitely repeat:


  read a value from the input stream


  write a value to the output stream





This program will only end when terminated by Ctrl-C.





Because a loop is used in both programs, they will process multiple characters until they terminate. The first program will run until you end the input stream (Ctrl-Z in DOS, Ctrl-D in Linux), or terminate (Ctrl-C). The second program will run until you terminate, and has undefined behavior if you end the input stream.





----------------


UPDATE


----------------





EOF is a constant defined in stdio.h, using "#define EOF (some number)"; it is most commonly -1. It is not technically a variable, though, so you cannot assign to it nor get its address. Typing "EOF" in your program is exactly the same as typing the number that is defined in stdio.h.





The following program always prints "-1" on my system. Try it out:





#include %26lt;stdio.h%26gt;





int main(int argc, char *argv[]) {


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


}
Reply:In the first example, it does the first getchar to check if there is even a value in it. if there is a value (ie it did not encounter an EOF) then the program proceeds.





In your program, you don't do a check for EOF. If you passed it an EOF, I assume it might break.
Reply:comeon yarr u r using d


while loop


dats why its wrkin wid multiple characters


nd eof is generally used wid files stream only
Reply:EOF means End Of File. Your code will loop until it encounter the end of the file you are reading chars from. If you are reading files from keyboard input, it won't work properly. That's why your code work... it reading keyboard input, not file input.
Reply:Hopefully you're developing on a UNIX or Linux platform, where you have the immensely useful 'man' utility at your disposal. From 'man getc':





RETURN VALUES





If successful, these routines return the next requested object from the stream. Character values are returned as an unsigned char converted to an int. If the stream is at end-of-file or a read error occurs, the rou-tines return EOF. The routines feof(3) and ferror(3) must be used to distinguish between end-of-file and error. If an error occurs, the global variable errno is set to indicate the error. The end-of-file condition is remembered, even on a terminal, and all subsequent attempts to read will return EOF until the condition is cleared with clearerr(3).





EOF is just a CPP macro defined to equal -1:





clay@olympus:~%26gt; grep EOF /usr/include/stdio.h


#define EOF (-1)





To simulate an EOF condition on a terminal, you can use Ctrl-Z (ASCII 26) in DOS/Windows or Ctrl-D (ASCII 4) in UNIX or Linux.
Reply:Second getchar() is used to pause execution of program. It was needed in some older compilers, if you don't use it than you don't have chance to view result.


No comments:

Post a Comment