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/C++ Preprocessor

(Direct output (general / GNU)

Date:12/30/05
Author:Gerry JJ
URL:n/a
Comments:2
Info:n/a
Score: (2.93 in 15 votes)
/*
**  99 bottles of beer for the C/C++ preprocessor, by Gerry JJ.
**
**  Tested with GNU CPP.  It should work with others as well,
**  provided the include nesting limit is high enough.
**
**  To use, run:  cpp -P 99bottlesofbeer.h
**  (The -P switch to CPP turns off line marker comments).
*/

#ifndef BOTTLES_OF_BEER_SONG
#define BOTTLES_OF_BEER_SONG
#define DIG1         9
#define DIG2         9
#define CAT2(a,b)    a ## b
#define CAT(a,b)     CAT2(a,b)
#define NUM          CAT(DIG1,DIG2)
#define BOTTLES      bottles
#define BEER(n)      n BOTTLES of beer
#define ONTHEWALL(n) BEER(n) on the wall
#define TAKEDOWN     Take one down and pass it around,
#endif

| ONTHEWALL(NUM),
| BEER(NUM)!
| TAKEDOWN

#if DIG2 == 9
#  undef  DIG2
#  define DIG2 8
#elif DIG2 == 8
#  undef  DIG2
#  define DIG2 7
#elif DIG2 == 7
#  undef  DIG2
#  define DIG2 6
#elif DIG2 == 6
#  undef  DIG2
#  define DIG2 5
#elif DIG2 == 5
#  undef  DIG2
#  define DIG2 4
#elif DIG2 == 4
#  undef  DIG2
#  define DIG2 3
#elif DIG2 == 3
#  undef  DIG2
#  define DIG2 2
#elif DIG2 == 2
#  undef  DIG2
#  define DIG2 1
#  if CAT(DIG1,DIG2) == DIG2
#    undef  BOTTLES
#    define BOTTLES bottle
#  endif
#elif DIG2 == 1
#  undef  DIG2
#  define DIG2 0
#  undef  BOTTLES
#  define BOTTLES bottles
#elif   DIG2 == 0
#  undef  DIG2
#  define DIG2 9
#  if DIG1 == 9
#    undef  DIG1
#    define DIG1 8
#  elif DIG1 == 8
#    undef  DIG1
#    define DIG1 7
#  elif DIG1 == 7
#    undef  DIG1
#    define DIG1 6
#  elif DIG1 == 6
#    undef  DIG1
#    define DIG1 5
#  elif DIG1 == 5
#    undef  DIG1
#    define DIG1 4
#  elif DIG1 == 4
#    undef  DIG1
#    define DIG1 3
#  elif DIG1 == 3
#    undef  DIG1
#    define DIG1 2
#  elif DIG1 == 2
#    undef  DIG1
#    define DIG1 1
#  elif DIG1 == 1
#    undef  DIG1
#    define DIG1
#  else
#    undef  DIG2
#    define DIG2 0
#  endif
#endif

#if NUM > 0
| ONTHEWALL(NUM)!
| 
#include "99bottlesofbeer.h"
#else
| ONTHEWALL(No more)!
#endif

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
1Wim Rijnders04/20/057
with Order interpreterVesa Karvonen12/30/051

Comments

>>  Paul said on 05/29/07 20:56:03

Paul This doesn't work at all for me.

First I get this:
>g++ -P 99bottlesofbeer.h
g++: compilation of header file requested

>g++ --version
g++ (GCC) 3.2.3 (mingw special 20030504-1)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Then making a copy in .cpp and one for .h

>g++ -P 99bottlesofbeer.cpp
99bottlesofbeer.cpp:24: parse error before `|' token

What's up with that? Anyone else get it to work?

>>  Anon said on 06/04/07 03:50:51

Anon Paul, it fails because g++ is not cpp. You're trying to compile it as c++ code, while the preprocessor output is just plain text and not valid c++. Run it with cpp in stead (the c/c++ preprocessor). It should be part of your gcc installation.

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: