Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language C

(poor Style)

Date:09/01/05
Author:Matteo Casati
URL:n/a
Comments:8
Info:n/a
Score: (2.76 in 17 votes)
/* The 99 Bottles of Beer Program
 *
 * Sept. 1st 2005
 * by Matteo Casati <devaraja87^gmail^com>
 *
 * This is the poor-style C version of
 * the program. It uses only gotos.
 */

int main()
{
    int n_beers=99;
    char pl='s';
    goto a;
  z:
    return 0;
  b:
    n_beers--;
    if(n_beers){
	pl=(n_beers==1?31:'s');
	printf("%d bottle%c of beer on the wall!\n",n_beers,pl);
	goto a;
    }else{
	printf("no more bottles of beer on the wall!\n");
	printf("No more bottles of beer on the wall,"
	       " no more bottles of beer.\nGo to the store"
	       " and buy some more, 99 bottles of beer on the wall!\n");
	goto z;
    }
  a:
    printf("%d bottle%c of beer on the wall, "
	   "%d bottle%c of beer\n",n_beers,pl,n_beers,pl);
    printf("Take one down and pass it around, ");
    goto b;
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Linux kernel moduleStefan Scheler08/02/0515
Correct ANSI C containing no semicolonsSteve Checkoway01/15/090
multithreaded versionStefan Scheler05/11/056
actually produces correct lyrics :PDustshine08/20/050
standard versionBill Wein04/20/054

Comments

>>  Skylark said on 09/05/05 14:01:58

Skylark You have to #include <stdio.h> for that to work, ie printf lolz.

Also:

#include <stdio.h>

int main()
{

>>  Richard Selby said on 08/25/06 17:01:29

Richard Selby Ah lovely! It's so nice to see someone break the rules

>>  Kiyoshi Akima said on 08/28/06 20:04:23

Kiyoshi Akima In the olde days you didn't need to #include a header unless you needed something from it. Since the program doesn't use the return value from printf(), it doesn't need <stdio.h>.

However, the program does use string concatenation. Perhaps poorer style might be something like
<pre>
printf("No more bottles of beer on the wall,\
no more bottles of beer.\nGo to the store\
and buy some more, 99 bottles of beer on the wall!\n";);
</pre>

>>  Kiyoshi Akima said on 08/28/06 20:06:28

Kiyoshi Akima Not sure where that smiley came from. It was just a close parenthesis.

And I guess I now know not to put in formatting tags...

>>  G. De Rosa said on 09/10/06 19:26:56

G. De Rosa void main() would be even poorer than int main() :-)

>>  Anonymous said on 09/03/07 15:57:59

Anonymous "void main() would be even poorer than int main() :-)"

Ahh, but int main(), and not returning anything tops both ;)

>>  aName said on 11/29/08 03:28:22

aName "Ahh, but int main(), and not returning anything tops both ;)"

No. If control "falls off" the end of main() without an explicit return statement, the return value is zero.

>>  Anonymous said on 06/11/09 22:13:30

Anonymous your code is far too verbose to really be called poor style, "int main()" really? I like your label names, but your vars are way way too readable. Poorly styled code should be concise, reliant on support for old language features, nearly indecipherable and above all else formatted to fit 80 columns.
how about...
//this WILL compile with gcc
#define P printf
main(){
char i='c',*s="s",*z="",*c=" of beer",*w=" on the wall, ",*b=" bottle",
*n="no more",*t="take one down and pass it around, ";
a:P("%d%s%s%s%s%d%s%s%s\n%s",i,b,i>1?s:z,c,w,i,b,i>1?s:z,c,t);
i<2?P(n):P("%d",i-1);P("%s%s%s%s\n\n",b,i-2?s:z,c,w);
if(--i)goto a;goto b;
b:P("%s%ss%s%s%s%ss%s\ngo to the store and buy some more, 99%ss%s%s\n",
n,b,c,w,n,b,c,b,c,w);
}

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: