LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-03-2013, 04:29 AM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
explain eof in c programming.


i am clearing my concepts on programming and figured out this concept of eof using getchar()
This program never prints the number of characters inputted in getchar .

please help
where n how it gets eof
and why is it not printing tota number of counts


Code:
#include<stdio.h>
main(){
int c ,count;
while ((c=getchar())!=EOF)
count++;
printf("%d characters /n",count);
}
 
Old 02-03-2013, 10:03 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
1) it should be int main().
2) you should initialize count before you use it.
3)
Code:
printf("%d characters /n",count);
maybe you meant
Code:
printf("%d characters \n",count);
Anyway, the program should work.

Quote:
Originally Posted by batman4
where n how it gets eof
EOF is a special value that indicates that the end of the file has been reached. If the input is read from the terminal, you can indicate the end of input by pressing Ctrl+D.
 
1 members found this post helpful.
Old 02-06-2013, 07:58 PM   #3
drunkenbrawler
LQ Newbie
 
Registered: Sep 2009
Location: FL, United States
Distribution: Ubuntu Fedora
Posts: 10

Rep: Reputation: 0
http://www.cplusplus.com/reference/cstdio/EOF/

A good site for checking out definitions in C.
 
Old 02-07-2013, 02:26 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by millgates View Post
...
Anyway, the program should work.
...

I don't think so - 'count' is uninitialized.
 
Old 02-07-2013, 07:33 AM   #5
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Rep: Reputation: Disabled
I guess this would work!!!

Code:
#include<stdio.h>
int main(){
int c ,count=0;
while ((c=getchar())!=EOF)
count++;
printf("%d characters /n",count);
return 0;
}

EOF is the end charater specifying the end of charaters(readable) in a FILE
 
Old 02-07-2013, 08:33 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,890

Rep: Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522
Note: you're allowed to use indentation:
Code:
/* somenamehere.c */

#include <stdio.h>

int main (void)
{
    int c, count=0;

    while ((c=getchar())!=EOF)
        count++;
    printf ("%d characters\n", count);
    return 0;
}
 
Old 02-07-2013, 10:16 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Note that getchar() reads stdin. If the stdin stream is not redirected at runtime, then the program can never see EOF because EOF would signify closure of the console and that would also normally terminate the program. Redirecting stdin from a file will cause EOF to be seen when the file is exhausted.

The exercise might be more meaningful if used on a file or device.

--- rod.
 
Old 02-07-2013, 10:20 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,890

Rep: Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522
Or if you press Ctrl+Z (WinDos) or Ctrl+D (unix).
 
  


Reply

Tags
programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to print EOF in C programming?? batman4 Programming 3 11-29-2012 11:49 AM
Could someone please explain the concept of associative arrays in AWK programming? AJAY E Linux - Newbie 6 05-27-2012 08:01 PM
Can someone explain to me this code from K&R's 'The C Programming Language' book? frankie_DJ Programming 10 11-25-2006 02:35 PM
Eof tostay2003 Programming 4 07-05-2006 11:11 AM
<< Eof ? sikandar Linux - Software 5 09-18-2003 12:39 PM


All times are GMT -5. The time now is 06:35 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration