• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: c question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: c question


  • Subject: Re: c question
  • From: William Bumgarner <email@hidden>
  • Date: Wed, 29 Mar 2006 19:31:30 -0800

On Mar 29, 2006, at 4:05 PM, Michael wrote:
In my quest to start at the basics, could someone tell me why this code, in C, will not give the number of characters in the run log. tks

>>>>>>>
#include <stdio.h>
/* counts characters from key board */
int main () {
double i;
for ( i=0; getchar() != EOF; ++i)
;
printf("Number of characters = %.0f\n" , i);
    return 0;
}
<<<<<<<<<<<

The code is working correctly, but it will loop in the for() loop forever until the EOF -- end of file -- is hit. That won't happen until the input stream is closed. But there isn't a way to close the input stream.


Well, not quite. The Cocoa text entry system is very emacsen in nature. As such, certain multi-key sequences work just fine. In this case, you can hit control-q followed by control-d to close standard input to the inferior process. control-q means 'input whatever the heck the user types next as a raw character'. control-d is a shell shortcut for close stdin. Oddly, you do have to hit return to have the ctrl-d actually trigger an EOF. No, the return character is not the EOF, you can hit <ret><ret><ctrl-q><ctrl-d><ret> and see a count of 2.

And some other random points that seemed to have been confused:

- buffered vs. unbuffered I/O is totally irrelevant. getchar() is going to get one character at a time, regardless of buffering semantics. The for() loop is written such that it will only terminate on EOF.

- whether i is a double or an int is irrelevant, too. Some integer type would make more sense, but double works, too.

b.bum

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >c question (From: Michael <email@hidden>)

  • Prev by Date: Re: fnfErr for resource files
  • Next by Date: Re: std::log10() problem
  • Previous by thread: Re: c question
  • Next by thread: fnfErr for resource files
  • Index(es):
    • Date
    • Thread