EOF keyboard control

Hello all,

I am learning C in one of my classes and we are working on some basic programs like counting lines and characters of input and such. The problem i am running into is that when i press Ctrl + D to generate an EOF character, it does not work. For the program below, when i press Ctrl + D i get a '^D' printed in the terminal, but hte program does not end. I have to manually stop it with Ctrl+c.

======================
#include <stdio.h>

main() {
int c, n1;

n1 = 0;
while ((c == getchar()) != EOF) {
if (c == '\n') {
++n1;
}
}
printf("%d\n", n1);
}
======================

This program works in linux, but i'm not quite sure why it doesn't work on my mac. Is there a different key combination for an EOF?

Thanks,

--whit

MacBook Pro, Mac OS X (10.4.10)

Posted on Feb 29, 2008 7:13 PM

Reply
4 replies

Feb 29, 2008 11:17 PM in response to hockeyboi16

hockeyboi16 wrote:
This program works in linux, but i'm not quite sure why it doesn't work on my mac. Is there a different key combination for an EOF?


No, it is Ctrl + D.
Can you please do a test? Type cat and then press Ctrl + D. Does it work?

It should be *c = getchar()* not with double ==.
Also, your main() function is not defined according to the ANSI standard.
Last, compile your program with -Wall for additional warnings.

Mihalis.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

EOF keyboard control

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.