![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 14
| Question about EOF. Code: #include <stdio.h>
int main() {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
The author said that the type of c must be int, because EOF needs to fit inside too, along with all the other characters (in range from -128 to 127). My question is: What is EOF? What do I type to get out of the loop? |
| Junky is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 11,292
| EOF depends on your OS, shell or environment. Try either CTRL D or CTRL Z (no, no matter what any idiot that comes after me might say, it isn't CTRL C, ever). Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 |
| Registered User Join Date: Sep 2006
Posts: 3,720
| From the CTRL D or CTRL Z Quzah mentioned, your compiler will probably give EOF a value of -1, if the file pointer is at the end of the file, or zero if it's not. CTRL Z and 0 or -1, was the standard from the DOS day. It's still in use by Windows. If you'd like to see CTRL Z in action, in Windows, open up a console (DOS) window. type: copy con Ancient.txt Water, water, everywhere And all the boards did shrink Water, water, everywhere Nor any drop to drink <and press CTRL z> You'll now have a file called Ancient.txt in your directory, with these few lines from The Rhyme of the Ancient Mariner Last edited by Adak; 12-05-2009 at 12:52 PM. |
| Adak is offline | |
| | #4 |
| Registered User Join Date: Nov 2009
Posts: 14
| Tnx, it doesn't work for CTRL D/Z, but it works for CTRL C ![]() One more question, but I don't want to open a new thread. The author said that you can do this: Code: #include <stdio.h>
int main() {
char c;
while((c = getchar()) != 'A') printf("Lol\n");
}
Code: #include <stdio.h>
int main() {
char c;
c = getchar();
while (c != 'A') {
printf("Lol\n");
c = getchar();
}
}
|
| Junky is offline | |
| | #5 | |
| Registered User Join Date: Sep 2006
Posts: 3,720
| Stop! On Windows it works. I just did it (again), on Windows XP, and I've done the same thing with CTRL z with nearly every version of Windows since Windows 3.1. That's how I write up very small bat and text files. I haven't tried it yet with Windows 7. The posted program does the following: First, it fills up the keyboard buffer with your keystrokes, until you hit 'A'. Each letter is also echo'd to stdout (usually the screen). When the keyboard buffer is full, you will hear a beep every time you hit a key. If you hit enter, it prints the Lol message, once for every key stroke, which allows the getchar() to empty the keyboard buffer. In my case, that's about 256 "Lol"'s. If you still haven't hit 'A' key, then it waits for your next keystroke. Quote:
| |
| Adak is offline | |
| | #6 |
| Registered User Join Date: Nov 2009
Posts: 14
| Windows XP, I'm working on Dev-C++. Thanks, I think I understand now what does it actually do. I realized that it prints "Lol\n" 1 time if I just press "Enter", 2 times if I enter 1 character, 3 times if I enter 2 characters, although I still don't understand why does it print 2 times for 1 character. Last edited by Junky; 12-06-2009 at 05:42 AM. |
| Junky is offline | |
| | #7 |
| Registered User Join Date: Sep 2006
Posts: 3,720
| if you mean it prints twice with every char you enter, then the answer is that the char you typed also has a char behind it, the newline, that was sent to the keyboard buffer when you hit the enter key. the newline is also a char (until it becomes two char's (CR & LF, when opened as a text file). |
| Adak is offline | |
| | #8 |
| Registered User Join Date: Nov 2009
Posts: 14
| Omg, you're right!! Tnx |
| Junky is offline | |
| | #9 |
| Registered User Join Date: Nov 2009
Posts: 14
| Ok, I'm getting really annoying with this, but I want to understand EOF completely. Code: #include <stdio.h>
int main() {
int c,nl;
nl = 0;
while ((c = getchar()) != EOF) if (c == '\n') nl++;
printf("%d\n",nl);
}
(When I was testing it, I entered system("pause"), so that's not the problem). |
| Junky is offline | |
| | #10 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 803
| Quote:
__________________ HOPE YOU UNDERSTAND....... By associating with wise people you will become wise yourself It's fine to celebrate success but it is more important to heed the lessons of failure We've got to put a lot of money into changing behavior PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition | |
| BEN10 is offline | |
| | #11 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 803
| EOF explained C99 defines EOF as a macro "which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file".
__________________ HOPE YOU UNDERSTAND....... By associating with wise people you will become wise yourself It's fine to celebrate success but it is more important to heed the lessons of failure We've got to put a lot of money into changing behavior PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition |
| BEN10 is offline | |
| | #12 |
| Registered User Join Date: Nov 2009
Posts: 14
| I understand now. I was confused, because CTRL-Z gave me "^Z", but I just have to press enter. Tnx |
| Junky is offline | |
| | #13 |
| Registered User Join Date: Jul 2010
Posts: 3
| I'm reading the same book on C programming as well. Though, Ctrl + Z dosen't seem to work for me. I'm using VS 2008 and windows 7 to run the code. I read somewhere that sometimes you'd have to use Ctrl + V first followed by Ctrl + Z. Is that the right way? Seems to have worked. Has anyone else tried it on windows 7? |
| dareck is offline | |
| | #14 |
| Registered User Join Date: Jul 2010
Posts: 3
| and I had to press 'Enter' after Ctrl+v AND Ctrl+z |
| dareck is offline | |
| | #15 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 12,460
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| EOF messing up my input stream? | Decrypt | C++ Programming | 4 | 09-30-2005 03:00 PM |
| question about eof | ssjnamek | C++ Programming | 17 | 05-05-2005 10:05 AM |
| files won't stop being read!!! | jverkoey | C++ Programming | 15 | 04-10-2003 05:28 AM |
| EOF question, scanf also | newbie123 | C Programming | 4 | 01-31-2003 04:15 PM |
| what does this warningmean??? | kreyes | C Programming | 5 | 03-04-2002 07:53 AM |