LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2003, 12:14 PM   #1
xailer
Member
 
Registered: Nov 2003
Posts: 77

Rep: Reputation: 15
Question about EOF (C)


hi
I'm learning C using red hat 9

EOF marks the end of file.But what value does EOF represent?First I thought it was either 1 or 0,but none of them works.So I tried this

main()
{
#define EOF 3

int a;

a=getchar();
while(a!=EOF)
{
putchar(a);
getchar(c),
}
}

But loop just didn't want to end,no matter what I pressed.

So what value does EOF hold and why it didn't work even with #define
statement?

thank you

Last edited by xailer; 12-06-2003 at 12:15 PM.
 
Old 12-06-2003, 12:21 PM   #2
ter_roshak
Member
 
Registered: May 2001
Location: Everett, WA
Distribution: Gentoo, RedHat
Posts: 102

Rep: Reputation: 15
I believe the value returned is -1, and I just tested it with the following code:

void main()
{
printf("%i\n", EOF) ;
}

...and I received a -1 response.

Josh
 
Old 12-06-2003, 12:35 PM   #3
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
I get -1 also,but why is -1 ignored in while loop?

bye
 
Old 12-06-2003, 12:46 PM   #4
teval
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720

Rep: Reputation: 30
EOF is generated with ^D (ctrl + D)

I can tell you why it didn't work too
Your program is definitely not doing what you want it to do.
Code:
int main()
	{
//		#define EOF 3
// Redefining things like EOF is bad, don't do it.
// Some libs may use EOF and if you redefine it, it might break a lot of things

	int a;

	a = getchar(); //Note how it's in [ code] and [/ code] tags, it's indented and spaced out
	// It not only looks better, but it's simpler to read

	while( a != EOF ) // You had = instead of ==, what that would do is assing a the value of EOF
        // != is more logical than a! == EOF
        // You are not flipping the value of a and then comparing, you are comparing and seeing if they are differnet
	// That assignment should always return true, because unless you've used all your computer's memory it won't fail
	{
		putchar(a);
		getchar(a); //Note the fact that it's a all the time, you had c, but I think that was a typo.
	}

	retrun 0; //It's nice when main returns a value, since when you string programs togeather with && it'll cause the entire chain to fail if it's non 0
	//Also a nice way of testing of programs failed or worked
}
I suggest scrapping the code, and doing something else like..
making it stop at a newline (\n)
 
Old 12-06-2003, 01:11 PM   #5
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
hi
Yes,c was a typo.
I copyed this exaple from book.


Quote:
Originally posted by teval


while( a != EOF )
// != is more logical than a! == EOF
// You are not flipping the value of a and then comparing, you are comparing and seeing if they are differnet
// That assignment should always return true, because unless you've used all your computer's memory it won't fail

Are you reffering to what would happened if I had
while (a! == EOF) ;

statement?

Is -1 not recognised as EOF because the value that I type in is essentially
a char,and EOF is integer?

thank you for helping
 
Old 12-06-2003, 08:13 PM   #6
teval
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720

Rep: Reputation: 30
I'm saying, EOF is not the same as -1. EOF is ^D
Comes up as -1 because of the fact ^D doesn't map as a visible character
 
Old 12-07-2003, 11:15 AM   #7
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
thanx for your help
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
EOF finding Goblin_C_Noob Programming 7 09-10-2005 09:58 AM
finding the eof nodger Programming 12 11-26-2004 02:17 AM
Premature EOF? miknight Programming 1 04-04-2004 12:19 AM
eof ... c ... linux xviddivxoggmp3 Programming 6 03-31-2004 12:56 PM
<< Eof ? sikandar Linux - Software 5 09-18-2003 11:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration