【问题标题】:Dealing with constants inside functions处理函数内部的常量
【发布时间】:2012-05-31 01:20:44
【问题描述】:

如果某事为真,我想定义一个常量,并在 "system(""); 中使用它的值;

例如:

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


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

我知道 conio.h/conio2.h 中有 clrscr(); 但这只是一个例子。当我尝试启动它时,它说 cls 没有声明,或者 CLRSCR 不是内部命令 (bash)

谢谢

【问题讨论】:

    标签: c system constants c-preprocessor


    【解决方案1】:

    Constant 是一个标识符,而不是一个字符串文字(字符串文字有双引号;标识符没有)。

    另一方面,常量值是字符串文字,而不是标识符。你需要像这样切换它:

    #ifdef __unix__
    #   define CLRSCR "clear"
    #elif defined _WIN32
    #   define CLRSCR "cls"
    #endif
    
    
    int main(){
        system(CLRSCR); //use its value here.
    }
    

    【讨论】:

    • 解决了我的问题。谢谢!
    【解决方案2】:

    你需要这个:

    #ifdef __unix__
       #define CLRSCR "clear"
    #elif defined _WIN32
       #define CLRSCR "cls"
    #endif
    
    
    system(CLRSCR); //use its value here.
    

    【讨论】:

    • 谢谢!解决了我的问题。
    猜你喜欢
    • 2021-03-31
    • 2015-11-13
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    相关资源
    最近更新 更多