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:6
Info:n/a
Score: (2.85 in 20 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
actually produces correct lyrics :PDustshine08/20/050
Linux kernel moduleStefan Scheler08/02/0515
Correct ANSI C containing no semicolonsSteve Checkoway01/15/090
multithreaded versionStefan Scheler05/11/054
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() :-)

>>  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.

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: