Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with constants inside functions

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

For example:

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


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

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

Thanks

like image 899
ghaschel Avatar asked May 24 '12 12:05

ghaschel


2 Answers

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

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

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


int main(){
    system(CLRSCR); //use its value here.
}
like image 77
Sergey Kalinichenko Avatar answered Oct 24 '22 03:10

Sergey Kalinichenko


You need this:

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


system(CLRSCR); //use its value here.
like image 32
Agnius Vasiliauskas Avatar answered Oct 24 '22 04:10

Agnius Vasiliauskas