Working with constants inside functions

I want to define a constant, if something is true, and use its value inside "system (" ");

For instance:

#ifdef __unix__
#   define CLRSCR clear
#elif defined _WIN32
#   define CLRSCR cls
#endif


int main(){
    system("CLRSCR"); //use its value here.
}

I know what is clrscr();in conio.h / conio2.h, but this is just an example. And when I try to run it, it says it is clsnot declared or CLRSCR is not an internal command (bash)

thank

+5
source share
2 answers

A constant is an identifier, not a string literal (string literals have double quotes around them, identifiers do not).

A constant value, on the other hand, is a string literal, not an identifier. You need to switch it like this:

#ifdef __unix__
#   define CLRSCR "clear"
#elif defined _WIN32
#   define CLRSCR "cls"
#endif


int main(){
    system(CLRSCR); //use its value here.
}
+6
source

You need the following:

#ifdef __unix__
   #define CLRSCR "clear"
#elif defined _WIN32
   #define CLRSCR "cls"
#endif


system(CLRSCR); //use its value here.
+4
source

All Articles