Language C
(standard version)
| Date: | 04/20/05 |
| Author: | Bill Wein |
| URL: | n/a |
| Comments: | 4 |
| Info: | n/a |
| Score: |
/*
* 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 |
|---|---|---|---|---|
| Linux kernel module | Stefan Scheler | 08/02/05 | 15 | |
| actually produces correct lyrics :P | Dustshine | 08/20/05 | 0 | |
| 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
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"
}
}
{
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"
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Of course