C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-05-2009, 11:14 AM   #1
Registered User
 
Join Date: Nov 2009
Posts: 14
Question about EOF.

I was reading a book about C programming, and I came across this program example:
Code:
#include <stdio.h>

int main() {
    int c;
    
    c = getchar();
    while (c != EOF) {
          putchar(c);
          c = getchar();
    }
}
Ok, the author was obviously explaining getchar() and putchar() :P
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   Reply With Quote
Old 12-05-2009, 11:24 AM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 12-05-2009, 12:21 PM   #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   Reply With Quote
Old 12-06-2009, 04:41 AM   #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");
}
instead of this:
Code:
#include <stdio.h>

int main() {
    char c;
    
    c = getchar();
    while (c != 'A') {
           printf("Lol\n");
           c = getchar();
    }
}
But, why do both cases print "Lol\n" 2 times (in case I don't enter 'A')?
Junky is offline   Reply With Quote
Old 12-06-2009, 05:24 AM   #5
Registered User
 
Join Date: Sep 2006
Posts: 3,720
Quote:
Originally Posted by Junky View Post
Tnx, it doesn't work for CTRL D/Z, but it works for CTRL C
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:
But, why do both cases print "Lol\n" 2 times (in case I don't enter 'A')?
I'm not familiar with your system or compiler.
Adak is offline   Reply With Quote
Old 12-06-2009, 05:33 AM   #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   Reply With Quote
Old 12-06-2009, 05:55 AM   #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   Reply With Quote
Old 12-06-2009, 06:03 AM   #8
Registered User
 
Join Date: Nov 2009
Posts: 14
Omg, you're right!!
Tnx
Junky is offline   Reply With Quote
Old 12-07-2009, 08:20 AM   #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);
}
This is supposed to count lines. But I can't get to the 'printf' part, because, when I enter EOF (which is CTRL C), I get kicked out of the program. Does anyone know why?
(When I was testing it, I entered system("pause"), so that's not the problem).
Junky is offline   Reply With Quote
Old 12-07-2009, 08:28 AM   #10
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 803
Quote:
Originally Posted by Junky View Post
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);
}
This is supposed to count lines. But I can't get to the 'printf' part, because, when I enter EOF (which is CTRL C), I get kicked out of the program. Does anyone know why?
(When I was testing it, I entered system("pause"), so that's not the problem).
EOF is not CTRL-C, its CTRL-Z for Windows and CTRL-D for Linux. CTRL-C is some kinda interrput(which I'm not sure).
__________________
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   Reply With Quote
Old 12-07-2009, 08:33 AM   #11
DESTINY
 
BEN10's Avatar
 
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   Reply With Quote
Old 12-07-2009, 08:45 AM   #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   Reply With Quote
Old 07-28-2010, 01:30 AM   #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   Reply With Quote
Old 07-28-2010, 01:39 AM   #14
Registered User
 
Join Date: Jul 2010
Posts: 3
and I had to press 'Enter' after Ctrl+v AND Ctrl+z
dareck is offline   Reply With Quote
Old 07-28-2010, 01:43 AM   #15
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 12,460
Quote:
Originally Posted by dareck
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?
Interesting, but that does not appear to work for me on Windows 7. I can confirm that CTRL+Z is the way to trigger EOF when writing to standard input at a command prompt for Windows. What I do is enter it on a new line.
__________________
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22