I thought my code is compile-time check, how can it be a run-time check?
That code is executed at run-time - not compile time. My code above is compile-time check - as only 1 of the lines 13 or 15 will be compiled.
This is fine if the code is being executed on the machine/compiler options compiled for, but if the exe code is to be used on different environments then you do need a run-time check. In which case consider:
Can I check wether my operation system is 32/64 using the code below
You're checking the size of a pointer. Note that you can run 32-bit applications on 64-bit Windows so it doesn't necessarily tell you about the OS.
I am wondering what made it be in run-time, is it the sizeof operator?
sizeof() will be evaluated at compile time but the switch statement would be executed at runtime.
I never seen a directive for the preprocessor written inside a function rather that in the global namespace.
You can write preprocessor directives anywhere. The preprocessor doesn't care about the rules of the C++ language. It just expands to text and when the preprocessor phase is over the normal C++ compilation starts.
The printing with std::cout will still happen at runtime and need to be inside a function as always. No magic there.
This can be useful to avoid compiling code unnecessarily that will never be used on the target platform, or that cannot even be compiled for whatever reason (imagine you had some 64-bit code that can't be compiled on a 32-bit system without causing compilation errors). One disadvantage with not always compiling all the code is that you need to recompile for different platforms to make sure there are no syntax errors.
Here, s3 will be 4 when compiled in 32-bit mode, and 8 when compiled in 64-bit mode because it is
returning the size of a const char*, which is a pointer
So I thought this can be a hack to know witch system you have whether 32/64.
I am not sure any of these are ensured. If you install and use a 32 bit compiler, the resulting executable may tell you the OS is 32 bit, using code tricks to try to guess at it, and it may even do that if you build a 32 bit compiler on a 64 bit compiler. I don't know for sure what happens in all these cases, but you should just ask the OS. It will flat out tell you, though you need a library call to ask it, its much safer. (actually, you can get it without a library call if you are willing to ask it via a system call type thing and parse the output).
The data model does not depend only on the OS. For example, on 64-bit Windows, the data models of 64 bit programs are different for the microsoft compiler (LLP64) and the GNU compiler (LP64). For programming information, we need to seek the information from the compiler being used.
A 32-bit compiler/IDE can compile code that runs as 64-bit. Visual Studio 2019 and earlier versions, for example. Visual Studio 2022 requires a 64-bit CPU, though it can create 32-bit executables.
AFAIK there is no cross-platform method for determining if the OS a app is running on is 32-bit/64-bit. There are ways for specifically Windows.
One is to check for the existence of a "Program Files (x86)" folder at the root of your boot drive, along with a "Program Files" folder.
Under the Windows folder there can be other folders if the OS is 64-bit: "SysWOW64" is one.
It is possible to determine if an app is running as 32-bit in a 64-bit Windows environment:
Can I check wether my operation system is 32/64 using the code below
That code determines at run-time if you compile the code as 32- or 64-bit. That doesn't determine the bit-ness of the OS that runs the app.
How you determine the bit-ness when an app is running is likely going to require using OS-specific system calls, Windows kinda makes it easier by having special system folders if it is 64-bit that don't exist with 32-bit.
There are lots of web-pages with "simple code" on the internet that allegedly show how to determine what the bit-ness of an OS is with a running app, they are crap with a capital 'C'. The code determines the bit-ness the app was compiled as.
#include <stdio.h>
int main()
{
#ifdef _WIN32 // Includes both 32 bit and 64 bit
#ifdef _WIN64
printf("Windows 64 bit\n");
#else
printf("Windows 32 bit\n");
#endif
#else
printf("Not a Windows OS\n");
#endif
return 0;
}
Looks kinda straight-forward, doesn't it. But it doesn't work as intended.
Using Visual Studio 2022 compiled and run as x64:
Windows 64 bit
Compiled and run as x86 (32-bit):
Windows 32 bit
Same OS, just compiled differently.
Run the two apps on a Win x86 system and the 32-bit app will run. The x64 app will not execute at all.
I can't remember what the Win error message states off the top of my head.