Randomly Organized Things

A collection of partially-coherent thoughts.

Fun with #define in C — July 25, 2015

Fun with #define in C

I was messing around the other day with C/C++ and began to realize what all you could do with #define. I knew it was just a text replacement, so it could probably be anything. So I began to wonder, what can’t it do? I did some tests, and here are a few things that worked:

/// test #1
#define u using
#define n namespace
#define s std
// so can this work?
u n s;
// yes indeed it can!
/// test #2
#define spit return
// does it work?
int meaningOfLife() {
spit 42;
// meaningOfLife() returns 42
/// test #3 - let's go more in depth.
#define thing int
#define methodName meaningOfLife()
#define answer 42
// let's test it:
thing methodName {
// we defined spit before so I didn't redefine it
spit answer;
// that returns 42!
view raw define.c hosted with ❤ by GitHub

Now, I realize this isn’t exactly the ideal way to do things, but it seems that with a WHOLE lot of #define, you could make your whole program just random words!