TO C OR NOT TO *C

John Hascall hascall at atanasoff.cs.iastate.edu
Sun Oct 22 01:12:07 AEST 1989


In article <1294> cpcahil at virtech.UUCP (Conor P. Cahill) writes:
}In article <991>, dhesi%cirrusl at oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
}> Playing Devil's advocate, we bravely yet blithely break Henry Spencer's
}> every rule:

 Rules?!  We don't need to steenk'n rules...

}>      char c;
}>      c = getc(stdin);           /* Oops! forgot to test for EOF! */
}>      do {
}>         if feof(stdin)
}>            break;               /* WHEW! */
}>         .. do stuff with c ..
}>         c = getc(stdin);
}>      } while (1);
 
}And the same code could have been done without the break, without the 
}overhead of the feof() function call for every iteration of the loop
}and (in my humble opinion) more readable as follows:
 
}      int c;
}      while( (c=getc(stdin)) != EOF);
}      {
}         .. do stuff with c ..
}      }

 Or you could use:

       char c;
       while (c = getc(stdin), !feof(stdin)) {
	  .. stuff with c ..
       }

 which lets you use "char c", doesn't have the break and (IMHO) is more
 readable than the above two.  The whole purpose of this was that there is
 some great advantage to be had by using "char c" over "int c", right? :-)

 I suppose one might want to do:

      char buf[BUFMAX];
      while (buf[i] = getc(stdin), !feof(stdin) && (++i < BUFMAX)) ...

              *** that's the beauty of C ... it makes it easy ***
	      *** to do the same thing umpteen different ways ***


John Hascall                                The opinions are mine, you say...
ISU Comp Center                             but if you'ld like them everyday...
Ames, IA  50011                             see misc.jobs.resumes today!
hascall at atanasoff.cs.iastate.edu            BURMA SHAVE



More information about the Comp.lang.c mailing list