Unix, C, and C++
Function Reference
System Dependencies and Predefined Constants
The C and C++ standards leave a lot of things undefined or incompletely
defined, and different operating systems and compilers will will make
different decisions. Sometimes it is essential to know what system a
program is running under.
Detecting the System Type
Every commonly used C or C++ compiler pre-defines some special constants
that the program can check. These are pre-processor definitions (#define),
not proper C++ constants, and they should only be used in conjunction with
the preprocessor directives #ifdef and #ifndef.
I recommend putting the following sequence of definitions at the top of
any program that needs to know what kind of system it is running on.
No #includes are needed to make this work:
const int sys_unix=1, sys_windows=2, sys_unknown=0;
#if defined(unix)
const int system = sys_unix;
#elif defined (_WIN32)
const int system = sys_windows;
#else
const int system = sys_unknown;
#endif
With those definitions in place, proper C and C++ statements can make use
of the information:
void main(void)
{ if (system==sys_unix)
printf("Unix\n");
if (system==sys_windows)
printf("windows32\n");
if (system==sys_unknown)
printf("unknown\n"); }
Very Long (64 bit) Integers
Under the gcc compilers for unix, 64 bit integers are called "long long int";
under the microsoft compilers they are called "__int64" (that's two underlines
at the beginning).
This sequence provides uniform system-independent type names of "int64", and
"unit64" for the unsigned version, that can be used in all declarations.
#if defined(_WIN32)
#define int64 __int64
#define uint64 __int64
#elif defined(unix)
#define int64 long long int
#define uint64 unsigned long long int
#endif