Neither ISO C nor POSIX offer functionality to determine the underlying OS during runtime. From a theoretical point of view, it doesn't matter since C offers wrappers for the most common system calls, and from a nit-picking point of view, there doesn't even have to be an underlying OS.
However, in many real-world scenarios, it has proven helpful to know more about the host environment than C is willing to share, e.g. in order to find out where to store config files or how to call select()
, so:
Is there an idiomatic way for an application written in C to determine the underlying OS during runtime?
At least, can I easily decide between Linux, Windows, BSD and MacOS?
My current guess is to check for the existence of certain files/directories, such as C:\
or /
, but this approach seems unreliable. Maybe querying a series of such sources may help to establish the notion of "OS fingerprints", thus increasing reliability. Anyway, I'm looking forward to your suggestions.
select()
call correctly? – Saliva#ifdef
. – Tinytinya#ifdef
is essential here, you must learn to get along with it even if you cannot bring yourself to like it – Ilanwine
even tells you by its name: Wine Is Not an Emulator – Tinytinya#ifdef
, but be perfectly content to useif
. Any insight on that? – Selfimmolating