Better C Bracing Style
utzoo!decvax!yale-com!wagar
utzoo!decvax!yale-com!wagar
Wed Oct 27 14:21:43 AEST 1982
I have not seen my favorite C bracing style appear here yet. I believe
it is the most clear and consistent, and I will justify that and provide a
filter for this style below. In my style, all blocks are simply placed
on the same line, without unnecessary spacing. A conditional is thus:
if(conditional){statement;statement;statement;}
The advantages to this style, and some fine points, are:
1. Left braces appear to the LEFT of right braces. This simple concept
is not obeyed by any of the other bracing styles.
2. Comments are not permitted. This is the only wholly consistent
commenting policy I have seen so far. While the following
conditional may seem clear to some people, others may be confused:
i/*f and only i*/f (a =/*equivalence*/= b)
/*of*/ co/*urse, here we co*/ntinue;
Comments are thus considered harmful.
3. Code will compile faster, and take less disk space, since unnecessary
white space and comments are omitted.
4. This style is ideal for the following self-printing C program:
main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);}
I dare anyone out there in net.land to do this with one of the
preferred styles!
Below is my filter to converts any C program to this style. Note that
the Berkeley cb program has 358 lines, whereas mine has only 4.
#include <stdio.h>
#include <ctype.h>
#define ii(c)(isalpha(c)||isdigit(c)||c=='$'||c=='_')
main(){char w[512];register char*wp;register int c,lc;int as,ns,pl,pn;lc='\n';pn=1;while(1){tp:while(isspace(c=getchar())){lc=c;if(pl&&c=='\n'){putchar('\n');pn=1;as=pl=0;}}if(c=='#'&&lc=='\n'){if(!pn)putchar('\n');pl=1;while(isspace(c=getchar()));ungetc(c,stdin);c='#';}if(c==EOF)exit(0);wp=w;do{if((as&&(ii(c)||c=='\''||c=='"'))||(ns&&(c=='-'||c=='*'||c=='&'||c=='+')))if(pl!=1&&pl!=2)putchar(' ');as=ns=0;if(c=='"'){pn=0;putchar(c);while((c=mg())!='"'){putchar(c);if(c=='\\')putchar(c=mg());}putchar(c);goto tp;}if(c=='\''){pn=0;putchar('\'');putchar(c=mg());if(c=='\\')putchar(mg());putchar(c=mg());if(c=='\'')goto tp;if(c=='\\')putchar(mg());putchar(mg());goto tp;}if(c=='/'){if((c=mg())=='*'){sk();if(wp==w)goto tp;goto cn;}else putchar(*wp++='/');}putchar(*wp++=c);cn:;}while(!isspace(c=getchar())&&c!=EOF);lc=c;if(pn=(pl&&c=='\n')){putchar('\n');pl= -1;}else if(pl&&pl++==1)putchar(' ');if(wp==w||c==EOF)exit(0);as=ns=0;if(wp[-1]=='='){if(wp-1==w||(wp[-2]!='='&&wp[-2]!='<'&&wp[-2]!='>'&&wp[-2]!='!'))ns=1;}if(ii(wp[-1])&&pl>=0)as=1;if(pl==-1)pl=0;}}sk(){register char c;while(1){c=mg();g:if(c=='*')if((c=mg())=='/')return;else goto g;}}mg(){register char c;if((c=getchar())==EOF)exit();return(c);}
-Steve Wagar
decvax!yale-comix!wagar
More information about the Comp.lang.c
mailing list