"Bill Cunningham" <nospam@[EMAIL PROTECTED]
> wrote in message
news:%7sUj.3801$Ve.393@[EMAIL PROTECTED]
> I have had several complaints by some people who wish to help me and
I
> wish to get the problem straight. I wrote this small utility myself and
> added some indentation and I wonder if it is acceptable. It does make
> source easier to read.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc, char **argv) {
> if (argc!=3) {
> fprintf(stderr,"usage error\n");
> return -1;
> }
> double x,y;
> x=strtod(argv[1],NULL);
> y=strtod(argv[2],NULL);
> printf("%.2f\n",y/x);
> return 0;
> }
>
> Is this a good example of a properly indended program?
>
well, this gets into this big hairy area known as "coding style" (most
have
opinions, few can completely agree...).
now, I usually code in notepad, which has inflexible 8-space tabs, so
usually I use this.
if the tab space is adjustable, usually I like 4 space tabs.
2 or 3 spaces is IMO too little.
1 space is just horrid (may as well not indent at all...).
usually, I put opening and closing braces on their own lines, and closing
braces are indended the same as the opening braces.
int main(int argc, char *argv[])
{
FILE *fd;
if(argv<2)
{
printf("usage: %s <filename>\n", argv[0]);
return(-1);
}
fd=fopen(argv[1], "rb");
...
return(0);
}
note that EXIT_SUCCESS and EXIT_FAILURE are considered "more correct" for
main return values, but 0 and -1 are more common/traditional.
IMO, both forms:
if(...)
{
and:
if(...) {
are fairly common and acceptable, but most people put the brace on its own
line for functions, and rarely for structs or unions.
it is common for commas to be followed by a space ("f(x, y);" but not
"f(x,y);").
some people precede/follow parens and/or operators with spaces.
if certain single-letter variable names are used (especially,
i,j,k,s,t,...)
it is almost obligatory that they be certain types (i,j,k are int, s,t are
'char *', ...).
return is often/usually written as if it were a function call (common in
C,
rare in C++).
and so on...
> Bill
>
>


|