Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

C program does not terminate

Iam using turbo Cv3.0 .It does not recognise the constant EOF.For example consider following example.
#include <stdio.h>
void main()
{
int c ;
int count ;

while ( ( c = getchar() ) != EOF )
   count ++ ;

printf( "%d characters\n" , count ) ;
}

When I compile the program and run it generates the required output ,but the program does not terminate.
0
purtutu
Asked:
purtutu
2 Solutions
 
gj62Commented:
You said it generates the required output - that would be the printf?  If so, your loop has exited and EOF was recognized...  What do you mean it hasn't terminated?
0
 
dimitryCommented:
First you need to initialize your counter.
int count = 0;
Second, if you try to get EOF character from keyboard then it is CTRL-Z.
0
 
irisexCommented:
Typically, control-d or ASCII EOT, generates an EOF, from a terminal. On receiving EOF, a read immediately passes any bytes of data it holds to the process without waiting for a new-line, and discards the EOF. If EOF occurred at the beginning of a line, a read holds no bytes of data, and returns a byte count of zero, the standard end-of-file indication.

Substitute EOF by 4. If it does not work, by 0.

And don't forget to initialize your counter (int count=0;)
0
 
purtutuAuthor Commented:
THanks a bunch for the fast response.
0
 
FaithRavenCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Split points to dimitry & irisex
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

FaithRaven
EE Cleanup Volunteer
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now