如何比较 C 条件预处理器指令中的字符串 [英] How to compare strings in C conditional preprocessor-directives
本文介绍了如何比较 C 条件预处理器指令中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在 C 中做这样的事情.它只有在我使用 char 时才有效,但我需要一个字符串.我该怎么做?
I have to do something like this in C. It works only if I use a char, but I need a string. How can I do this?
#define USER "jack" // jack or queen
#if USER == "jack"
#define USER_VS "queen"
#elif USER == "queen"
#define USER_VS "jack"
#endif
推荐答案
我认为没有办法完全在预处理器指令中进行可变长度字符串比较.不过,您也许可以执行以下操作:
I don't think there is a way to do variable length string comparisons completely in preprocessor directives. You could perhaps do the following though:
#define USER_JACK 1
#define USER_QUEEN 2
#define USER USER_JACK
#if USER == USER_JACK
#define USER_VS USER_QUEEN
#elif USER == USER_QUEEN
#define USER_VS USER_JACK
#endif
或者您可以稍微重构代码并改用 C 代码.
Or you could refactor the code a little and use C code instead.
这篇关于如何比较 C 条件预处理器指令中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文