English 中文(简体)
处理内部函数中的常数
原标题:Dealing with constants inside functions

我想定义一个常数, 如果某事是真实的, 并在“ 系统( ) ” 中使用它的价值 ;

例如:

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


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

我知道在 conio.h/conio2.h 中有 < code> clerscr () ; in concio.h/conio2.h, 但这只是一个例子。 当我试图发射它时, 它说 < code> clcs 没有被宣布, 或者 CLRSCR 不是内部命令( bash) 。

谢谢 谢谢

最佳回答

常数是一个 < em> 识别符 ,而不是一个 < em> 字符串字性 (字符串字性周围有双引号; 标识符没有)。

另一方面,常数值是一个字符串字典,而不是标识符。您需要像这样切换它 :

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


int main(){
    system(CLRSCR); //use its value here.
}
问题回答

您需要这个:

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


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




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签