The following program demonstrates your tab width, indentation style, and various other aspects of your code style. Please write it (in verbatim) in your preferred style. Remember to use spaces instead of tabs, as shitchan automatically converts tabs into 3 space characters.
[code]#include "stdio.h"
int main (void)
{ int x;
char c = 'a';
float y = 0;
for (x = 0; x < 10; x++)
{ if (x % 2 == 0)
printf ("%d\n", x);
else
{ printf ("%c\n", c);
c +=1;
}
}
int
main
(
void
)
{
int
x;
char
c
= 'a';
float
y
= 0;
for
(
x
= 0;
x
< 10;
x++
)
{
if (
x
% 2
== 0
)
printf (
"%d\n"
, x
);
else
{
printf (
"%c\n"
,c
);
c
+= 1;
}
}
return 0;
}
Name:
Anonymous2010-01-25 8:03
FROZEN VOID QUALITY
Name:
Anonymous2010-01-25 8:54
/* This is the main function. The main function will do
everything in the program. */
int main (void)
{
/* In this section rest the declarations
for the variables used througout the program. */
int x; /* This will be use for the counter. */
char c;
float y; /* ``float'' is a nice word. */
/* Here we initialize the char (declared above)
and the float. */
c = 'a';
y = 0;
/* This section of the code loops from 0 to 9,
and handles things differently depending on whether
``x'' divides by two. */
for (x = 0; x < 10; x++) {
/* If-else clause. Handles what happens depending
on the value of x. */
/* If... */
if (x % 2 == 0)
/* If x divides, it prints x, and a newline. */
printf ("%d\n", x);
/* Else... */
else {
/* If x does not divide, it prints ``c'', and a newline,
and adds 1 to c. */
for (int x = 0; x != 10; ++x) {
if (x % 2 == 0)
printf("%d\n", x);
else
printf("%c\n", c++);
}
return 0;
}
The one true style. And no, I don't do C89 bullshit block declarations. It's 2010 for fuck's sake.
Name:
Anonymous2010-01-25 20:40
Oh god, so many hideous code styles. The only one remotely passable is >>31, and it has the legacy compatibility limitations of old K&R, such as requiring the open brace of a function on a newline. >>29 actually isn't terrible, except for the fact that his spacebar seems to be broken.
As the author of >>60, I feel I must educate furthur as to the one true style. There's lots of stuff this did not cover:
double braces around assignment in if statements if ((x = 7)) do_some_shit();
related declarations should have equal signs aligned int something = 4;
int something_else = 7;
repeated related function calls should have arguments aligned (with space only after the commas, none trailing; no alignment of commas or closing brace) some_func(an_arg, another_arg);
some_func(a_third_arg, last_arg);
braces can be used around and in ternary operator when needed, and put spaces around ? and : (but never inside braces). usually put braces around arms when needed to clarify, even when the precedence order would allow no braces (mainly because the inner arm of ?: breaks the precedence order) some_func(a ? (b + c) : d, e);
if an else block needs braces, add braces for the if block as well (don't do like OP). they don't add any lines in this case. if (x % 2 == 0) {
printf ("%d\n", x);
} else {
printf("%c\n", c);
c = c + 1;
}
no line length limits; this isn't the stone ages. but be reasonable. repeated related function calls however can break the 'reasonable' limitation, and run well over 200 characters; scrolling horizontally to see a table of well-aligned function calls is much preferable to 30 lines of repeated function calls with no alignment to show you the argument layout.
Uh... No, all of the non-joke coding styles listed in this thread are fine, as long as they're consistent.
Name:
Anonymous2010-01-26 2:31
>>61 related declarations should have equal signs aligned int something = 4; int something_else = 7;
Fair enough. Although I think Guido might object. repeated related function calls should have arguments aligned (with space only after the commas, none trailing; no alignment of commas or closing brace) some_func(an_arg, another_arg); some_func(a_third_arg, last_arg);
Uggggh. IHBT.
When it comes to C-likes, here's what I think of them:
K&R - okay, if consistent, but I find it less readable
Allman - more readable, I tend to find it easier to parse
BSD - okay, but less readable than Allman, however I like how function return type is declared on a separate line
Whitesmith - I don't see any advantage to it.
GNU - It's not terrible, but the braces having their own indent level is just plain weird.
Horstmann - Pretty good. Combines Allman's advantages with K&R's advantages. Probably one of the better ones, but it requires a good editor like Emacs to be used with. Is this what OP used?
For C-likes, I tend to use Allman, but if I'd use a more superior editor like Emacs for C code, I'd write in Horstmann style.
Indentation style is an already fully solved problem for some languages, for example Lisp has its own style, which is used by all sane Lispers (it's actually hard to read code written in non-standard lisp indentation styles, which are usually nonexistent and only used by misguided newbies which don't have a good editor). Emacs (and other lisp editors) provide full (re)indentation support and paren-matching, as well as many other useful features. This is ideal, but it helps having an uniform syntax, which is not what C-likes have.
Name:
Anonymous2010-01-26 6:41
>>70 Allman - more readable, I tend to find it easier to parse
And how is that possible?
>>71
I didn't mean "parse" in the sense of writing a program which parses such source code. I meant it in the mental sense: my brain seems to take less time reading C code indented in Allman than it does with K&R style. It may be a small difference, but it's enough for me to write my own(when working with code written by others, it's best to keep consistent) C code in Allman style.
K&R style
Yes, though I don't like having the brace on a newline after a function definition if I can help it. Long function definitions and constructors that initialize data are my exceptions. 1TBS
No. Allman style
Awful. BSD KNF style
Could be worse. I hate including else statements on the closing brace of an if, though. Whitesmiths style FUCK NO!!! GNU style
As pig-disgusting as Stallman himself. Horstmann style
Maybe if I was a troll. So basically, no.
Name:
Anonymous2010-01-26 7:44
you forgot frozenvoid style. no newlines.
Name:
Anonymous2010-01-26 8:19
The One True Indentation Style for C #include "stdio.h"
int main(void)
{int x;
char c='a';
float y=0;
for (x=0;
x<10;
x++)
{if(x%2==0)
printf("%d\n",x);
else
{printf("%c\n",c);
c +=1;}}
return 0;}