Date: Tue, 31 Mar 2009 07:48:00 -0400 From: William Gordon Rutherdale <will.rutherdale@utoronto.ca> To: Polytropon <freebsd@edvax.de> Cc: Gary Kline <kline@thought.org>, FreeBSD Mailing List <freebsd-questions@freebsd.org> Subject: Re: Why?? (prog question) Message-ID: <49D202F0.9010104@utoronto.ca> In-Reply-To: <20090331112122.ae329221.freebsd@edvax.de> References: <20090331025726.GA10888@thought.org> <20090331112122.ae329221.freebsd@edvax.de>
next in thread | previous in thread | raw e-mail | index | archive | help
Polytropon wrote: > I don't want to start a "style debate", but forgive me the > following annotations: > > 1. Use the tab character for indentation. You can set its > length with your favourite editor (e. g. mcedit: F9, > Options, General; joe: ^TD). Don't waste with spaces. > > 2. The main() function should be declared as > int main(int argc, char *argv[]) > or > int main(int argc, char **argv) > Note that it's returning (int). Use this functionality. > > 3. In case of errors (e. g. incorrect number of parameters) > use fprintf() to stderr, or perror() with the builtin > error handling (e. g. for "file not found" by fopen()). > > 4. Use the predefined return codes, don't hardcode them. > FreeBSD has EXiT_SUCCESS and EXIT_FAILURE, they're for > maximum compatibility (such as with Linux). There are > more exit codes for differentiation, but they're specific > to FreeBSD, as far as I know. > > 5. This is highly debatable: Use a good style for { and }. > > 6. Use delimiters around operators, e. g. buf[strlen(buf) - 1] > instead of buf[strlen(buf)-1]; increases readability. > > Here is the program again, with some stylistic modifications > and the "correct" (read: recommended, usual) exit code handling: > > > > > /* > * simple prog to join all | very nearly all lines of a text file > * that make up one paragraph into one LONG line. > * > * paragraphs are delimiated by a single \n break. > */ > > #include <stdio.h> > #include <string.h> > #include <stdlib.h> > > int main(int argc, char *argv[]) > { > char buf[65536]; > > > if(argc == 1) { > fprintf(stderr, "Usage: %s < file > newfile\n", argv[0]); > exit(EXIT_FAILURE); > } > > while (fgets(buf, sizeof buf, stdin)) { > if(*buf == '\n') { > fprintf(stdout, "\n\n"); > } else { > buf[strlen(buf) - 1] = ' '; > fputs(buf, stdout); > } > } > > return EXIT_SUCCESS; > } > > > > > > Note that compiling with -Wall (always a good option) doesn't > show any warning. > > > > I read my advices again... makes me sound sooooo old! :-) > > > > Tabbing is the worst form of indentation. It is *much* better to use spaces consistently. -Will
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?49D202F0.9010104>