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

(standard version)

Date:04/20/05
Author:Bill Wein
URL:n/a
Comments:1
Info:n/a
Score: (2.80 in 112 votes)
/*
 * 99 bottles of beer in ansi c
 *
 * by Bill Wein: bearheart@bearnet.com
 *
 */

#define MAXBEER (99)

void chug(int beers);

main()
{
register beers;

for(beers = MAXBEER; beers; chug(beers--))
  puts("");

puts("\nTime to buy more beer!\n");

exit(0);
}

void chug(register beers)
{
char howmany[8], *s;

s = beers != 1 ? "s" : "";
printf("%d bottle%s of beer on the wall,\n", beers, s);
printf("%d bottle%s of beeeeer . . . ,\n", beers, s);
printf("Take one down, pass it around,\n");

if(--beers) sprintf(howmany, "%d", beers); else strcpy(howmany, "No more");
s = beers != 1 ? "s" : "";
printf("%s bottle%s of beer on the wall.\n", howmany, s);
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Linux kernel moduleStefan Scheler08/02/059
multithreaded versionStefan Scheler05/11/055
actually produces correct lyrics :PDustshine08/20/050
poor StyleMatteo Casati09/01/056

Comments

>>  marcus said on 03/09/06 22:23:26

marcus Dang, you guys are verbose. This plays out just like the song does, forever:

int main(void)
{
int x;
char xs[4];
char *n = "o more";
char *b = " bottle";
char *o = " of beer";
char *w = " on the wall";

while(1)
{
x = 99;
while(x)
{
printf("%d%s%s%s%s, ", x, b, x == 1 ? "" : "s", o, w);
printf("%d%s%s%s\n", x, b, x == 1 ? "" : "s", o);
printf("Take %s down and pass it around, ", x-- == 1 ? "it" : "one" );
sprintf(xs, "%d", x);
printf("%s%s%s%s%s\n\n", x > 0 ? xs : "No", b, x != 1 ? "s" : "", o, w );
}
printf("N%s%ss%s%s, n%s%ss%s\n\7", n, b, o, w, n, b, o);
printf("Go to the store and buy some more\n";);
}
}

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: