#define in Java

No, because there’s no precompiler. However, in your case you could achieve the same thing as follows: class MyClass { private static final int PROTEINS = 0; … MyArray[] foo = new MyArray[PROTEINS]; } The compiler will notice that PROTEINS can never, ever change and so will inline it, which is more or less what … Read more

Preprocessor output

gcc -E file.c or g++ -E file.cpp will do this for you. The -E switch forces the compiler to stop after the preprocessing phase, spitting all it’s got at the moment to standard output. Note: Surely you must have some #include directives. The included files get preprocessed, too, so you might get lots of output. … Read more

Real-world use of X-Macros

I discovered X-macros a couple of years ago when I started making use of function pointers in my code. I am an embedded programmer and I use state machines frequently. Often I would write code like this: /* declare an enumeration of state codes */ enum{ STATE0, STATE1, STATE2, … , STATEX, NUM_STATES}; /* declare … Read more

This obfuscated C code claims to run without a main(), but what does it really do?

C language define execution environment in two categories: freestanding and hosted. In both execution environment a function is called by the environment for program startup. In a freestanding environment program startup function can be implementation defined while in hosted environment it should be main. No program in C can run without program startup function on … Read more

Is the C99 preprocessor Turing complete?

Well macros don’t directly expand recursively, but there are ways we can work around this. The easiest way of doing recursion in the preprocessor is to use a deferred expression. A deferred expression is an expression that requires more scans to fully expand: #define EMPTY() #define DEFER(id) id EMPTY() #define OBSTRUCT(…) __VA_ARGS__ DEFER(EMPTY)() #define EXPAND(…) … Read more

Is it possible to iterate over arguments in variadic macros?

Here is my homework of the day, it’s based on macro tricks and today I particularly learnt about __VA_NARG__ invented by Laurent Deniau. Anyway, the following sample code works up to 8 fields for the sake of clarity. Just extend the code by duplicating if you need more (this is because the preprocessor doesn’t feature … Read more

What’s the difference between the WIN32 and _WIN32 defines in C++

To elaborate (Neil Butterworth and blue.tuxedo have already given the correct answer): WIN32 is defined by the SDK or the build environment, so it does not use the implementation reserved namespace _WIN32 is defined by the compiler so it uses the underscore to place it in the implementation-reserved namespace You’ll find a similar set of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)