Discover the operating system of the machine in C

3

I need to do a function to clear the screen in C that runs on win, mac and linux. I thought about doing a function that:

If the OS were windows: system("cls");

If the OS was linux or mac: system("clear");

The problem is: How do I find out the computer's operating system and how would these if and else be?

    
asked by anonymous 24.08.2017 / 16:52

2 answers

2

To know if the system is linux or windows you can do as follows:

#ifdef __unix__         
    #include <unistd.h>
    #include <stdlib.h>

#elif defined(_WIN32) || defined(WIN32) 

   #define OS_Windows

   #include <windows.h>

#endif

int main(int argc, char *argv[]) 
{
#ifdef OS_Windows
 /* Codigo Windows */
    system("cls");
#else
 /* Codigo GNU/Linux */
    system("clear");
#endif    
}
    
24.08.2017 / 16:56
2

In this answer , I used this:

#if defined(__MINGW32__) || defined(_MSC_VER)
#define limpar_input() fflush(stdin)
#define limpar_tela() system("cls")
#else
#define limpar_input() __fpurge(stdin)
#define limpar_tela() system("clear")
#endif
    
24.08.2017 / 17:04