Bill Cunningham wrote:
>
> 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?
Close. The main problems are too little indentation (I suggest 3
spaces) and the lack of spaces in the source. Also the variable
declaration should not occur after executable code. Compare the
following:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
double x,y;
if (argc != 3) {
fprintf(stderr, "usage error\n");
return -1;
}
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
printf("%.2f\n", y/x);
return 0;
} /* main */
I like to mark the closing brace of a function with the function
name.
There is a program around called indent (GNU version 2.2.9 here)
which does all this for you. It is configurable. I use the
following configuration for it (really just one long line in
indent.pro):
-kr -l66 -i3 -bad -di16 -lc66 -nce -ncs -cbi0 -bbo -pmt -psl -ts1
-cdw -ppi 3
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com
**


|