2012-05-24 29 views
5

我要定義一個常量,如果事情是真實的,並使用其值「系統(」「)內;與常量處理函數內

例如:

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


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

我知道有clrscr();在CONIO.H/conio2.h但是這僅僅是一個例子。當我嘗試啓動它,它說cls未聲明,或者說CLRSCR不是內部命令(bash)的

感謝

回答

6

常量是一個標識,而不是一個字符串字面(字符串字面量在其周圍雙引號;標識符不)。

恆定值時,在另一方面,是一個字符串,而不是一個標識符。你需要像這樣切換它:

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


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

解決了我的問題。謝謝! – ghaschel

4

你需要這樣的:

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


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

謝謝!解決了我的問題。 – ghaschel