Bill Cunningham said:
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?
Better, but not yet good.
A single space per indentation level is IMHO insufficient.
It's still very hard to see what's indented. I like 4-column
indentation myself; I consider 2 to be the bare minimum. (Some
advocate 8-column indentation, which certainly encourages Any
decent text editor should do most of the work for you automatically
(":se ai" or ":set autoindent" in vi, for example).
There are several common styles for indentation and brace placement.
Your program exhibits none of them. That's not necessarily A Bad
Thing, but it's a bit jarring, and there's rarely any good reason
not to follow one of the existing styles.
Two of the most common styles are:
1. K&R style. An opening brace '{' goes at the end of a line,
preceded by a space (except possibly for the opening brace of a
function definition, for historical reasons). Enclosed lines are
indented by one level (e.g., by 4 columns). The closing brace is
aligned directly under the beginning of the line containing the
opening brace (this is where you go astray).
2. Opening and closing braces appear on lines by themselves.
Enclosed lines are indented by one level.
Rarer, but still valid, styles are:
3. Like 2, but the braces are aligned with the enclosed lines.
4. Like 2, but the braces are half-indented (e.g., by 2 columns).
I think this is the GNU-recommended style.
Personally, I prefer #1, and I've listed the others in order of my own
preference. Note that in all these styles, the closing brace is
aligned either with the opening brace or with the line on which the
opening brace appears.
At the bottom of this message, I've added a copy of your code in
each of the 4 styles (omitting the #include directives).
Some additional notes on your code:
Returning -1 from main() is non-portable. Use 0, EXIT_SUCCESS,
or EXIT_FAILURE.
Mixing declarations and statements is a C99-specific feature;
not all compilers support it. It's not wrong, but it may limit
the portability of your code.
(I'm tempted to mention the lack of error checking in your strtod()
calls, but this is obviously a toy program so I won't.)
Re-formatted code follows. Note that I've also inserted some
whitespace to make the code easier to read. Rules of thumb: use
spaces around operators, and always use a space after a comma.
Compare:
x=strtod(argv[1],NULL);
x = strtod(argv[1], NULL);
ThinkabouthowdifficultreadingEnglishwouldbewithoutwhitespace.
1.
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;
}
1a.
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;
}
2.
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;
}
3.
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;
}
4.
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;
}