Language C
(standard version)
Date: | 04/20/05 |
Author: | Bill Wein |
URL: | n/a |
Comments: | 4 |
Info: | n/a |
Score: | (2.81 in 124 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
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
actually produces correct lyrics :P | Dustshine | 08/20/05 | 0 | |
Linux kernel module | Stefan Scheler | 08/02/05 | 15 | |
Correct ANSI C containing no semicolons | Steve Checkoway | 01/15/09 | 0 | |
multithreaded version | Stefan Scheler | 05/11/05 | 4 | |
poor Style | Matteo Casati | 09/01/05 | 6 |
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!
Comments
marcus said on 03/09/06 22:23:26
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"
}
}
ItsMe said on 09/20/08 14:13:11
void main()
{
for(int beers=99; beers>0
{
char *s = (beers != 1) ? "s" : "";
printf("%d bottle%s of beer on the wall,\n", beers, s);
printf("%d bottle%s of beer.\n", beers, s);
printf("Take one down, pass it around,\n"
s = (--beers != 1) ? "s" : "";
if(beers>0) printf("%d bottle%s of beer on the wall.\n",beers,s);
else printf("No more bottles of beer on the wall.\n"
}
printf("Go to the store and buy some more\n"
}
aName said on 11/29/08 03:15:41
This isn't standard at all. See for example the declaration of main(), or the declaration of the variable 'beers'. It uses the "implicit int" feature which was removed in the current version of C.
anon said on 04/12/09 19:12:53
You also need:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Of course