# another C programming question

**URL:** <https://boards.straightdope.com/t/another-c-programming-question/153648>\
**Category:** Factual Questions\
**Created:** [February 8, 2003, 3:30am UTC](https://boards.straightdope.com/t/another-c-programming-question/153648 "2003-02-08T03:30:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cw](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@cw](https://boards.straightdope.com/u/cw)\
**Post date:** [February 8, 2003, 3:30am UTC](https://boards.straightdope.com/t/another-c-programming-question/153648/1 "2003-02-08T03:30:33Z")

</div>

Writing a program that requires ctrl-d to be pressed to exit the while loop. It is being run on a UNIX server. How can I do this? Im a C newbie, my best guess so far is to use #define EOF (^d) in the preprocessor directives and then put a statement in the while loop, will this work? If not how?

---

<div class="post-metadata">

**Author:** ![TeleTronOne](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@TeleTronOne](https://boards.straightdope.com/u/TeleTronOne)\
**Post date:** [February 8, 2003, 3:50am UTC](https://boards.straightdope.com/t/another-c-programming-question/153648/2 "2003-02-08T03:50:33Z")

</div>

EOF is already a predefined constant in any C implementation I’ve ever seen (defined in stdio.h).

something like:

char c;  
while(c=getch() && (c != EOF)) {  
/\* Do things with c \*/  
}

or

char buf[256];  
while(fgets(buf, sizeof(buf), stdin)) {  
/\* do things with buf  
fgets() returns NULL on EOF \*/  
}

should work for you

---

<div class="post-metadata">

**Author:** ![TeleTronOne](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@TeleTronOne](https://boards.straightdope.com/u/TeleTronOne)\
**Post date:** [February 8, 2003, 3:53am UTC](https://boards.straightdope.com/t/another-c-programming-question/153648/3 "2003-02-08T03:53:14Z")

</div>

Oh, and incidentally,

#define EOF (^d)

wouldn’t work anyway. What I think you meant was

#define EOF “^d”

which wouldn’t work anyway 🙂 “^d” is just what the console displays (since you can ‘make’ an EOF with CTRL-D), EOF itself is actually -1

---

<div class="post-metadata">

**Author:** ![cw](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@cw](https://boards.straightdope.com/u/cw)\
**Post date:** [February 8, 2003, 4:11am UTC](https://boards.straightdope.com/t/another-c-programming-question/153648/4 "2003-02-08T04:11:37Z")

</div>

Thanks Teletron, your always helpful on my comp nerd questions. I don’t know what I was thinking, as I said I am a C newbie, my book just looked like I could use EOF for this and change -1 to something I wanted to exit the program (I’m all confused).

---

<div class="post-metadata">

**Author:** ![John\_T.Conklin](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/john_t.conklin/32/3307_2.png) [@John\_T.Conklin](https://boards.straightdope.com/u/John_T.Conklin)\
**Post date:** [February 8, 2003, 6:13am UTC](https://boards.straightdope.com/t/another-c-programming-question/153648/5 "2003-02-08T06:13:36Z")

</div>

> [@](#):
>
> \*Originally posted by TeleTronOne \*  
> \*\*EOF is already a predefined constant in any C implementation I’ve ever seen (defined in stdio.h).
> 
> something like:
> 
> char c;  
> while(c=getch() && (c != EOF)) {  
> /\* Do things with c \*/  
> }  
> \*\*

This won’t actually work.

EOF is defined to be -1 (Actually, I don’t have my copy of the C standard close at hand and can’t remember whether it is defined to be -1 or just a negative integral constant, but for the purpose of this message it doesn’t really matter) , which is out of range of a char. The EOF would be truncated to fit in a char, and then may (depending on whether chars are signed or unsigned (which is implementation defined)) not match EOF due to sign extension. To avoid this problem, use an int instead.

The following example copies standard in to standard out.

```auto

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

```

For **cw** , the ^D is somewhat of a red herring. On a UNIX system, a ^d on a tty generally means end of file (the actual character can be changed with the stty command), On DOS it’s ^Z. In both cases, the ^D or ^Z are never read by your program.

---

<div class="post-metadata">

**Author:** ![TeleTronOne](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@TeleTronOne](https://boards.straightdope.com/u/TeleTronOne)\
**Post date:** [February 8, 2003, 6:26pm UTC](https://boards.straightdope.com/t/another-c-programming-question/153648/6 "2003-02-08T18:26:09Z")

</div>

> [@](#):
>
> \*Originally posted by John T. Conklin \*  
> \*\*This won’t actually work.
> 
> EOF is defined to be -1 (Actually, I don’t have my copy of the C standard close at hand and can’t remember whether it is defined to be -1 or just a negative integral constant, but for the purpose of this message it doesn’t really matter) , which is out of range of a char. The EOF would be truncated to fit in a char, and then may (depending on whether chars are signed or unsigned (which is implementation defined)) not match EOF due to sign extension. To avoid this problem, use an int instead.
> 
> The following example copies standard in to standard out.
> 
> ```auto
> 
> int c;
> while ((c = getchar()) != EOF) {
> putchar(c);
> }
> 
> ```
> 
> For **cw** , the ^D is somewhat of a red herring. On a UNIX system, a ^d on a tty generally means end of file (the actual character can be changed with the stty command), On DOS it’s ^Z. In both cases, the ^D or ^Z are never read by your program. \*\*

This is what I get for posting late after having a few beers and not checking my work 🙂 I can’t even remember the last time I wrote something that used something like this, I stand corrected, sir. :smack:

---

<div class="post-metadata">

**Author:** ![TeleTronOne](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@TeleTronOne](https://boards.straightdope.com/u/TeleTronOne)\
**Post date:** [February 8, 2003, 6:27pm UTC](https://boards.straightdope.com/t/another-c-programming-question/153648/7 "2003-02-08T18:27:31Z")

</div>

> [@](#):
>
> \*Originally posted by cw \*  
> \*\*Thanks Teletron, your always helpful on my comp nerd questions. I don’t know what I was thinking, as I said I am a C newbie, my book just looked like I could use EOF for this and change -1 to something I wanted to exit the program (I’m all confused). \*\*

Well, as John pointed out, I can be wrong 🙂 But if I may, you need to get a copy of K&R’s “The C Programming Language”, if you don’t have one already. No C programmer should be without it. It’s not very thick, and it’s been out for ages, so I imagine you could get a pretty cheap copy.
