我正在使用visual studio c ++。我希望能够在双长和长双之间切换。 如何在以下程序中使用#ifdef?我想使用更简单的解决方案来处理多个printf的情况。
//#define TYPE_SWITCH
#ifdef TYPE_SWITCH
typedef double myType;
#else
typedef long long myType;
#end
.
.
.
int main()
{
myType a;
#ifdef TYPE_SWITCH
printf ("my value is %lf",a); // I have many printf or scanf and I want to use a simple macro here
#else
printf ("your value is %l",a/10); // I have many printf or scanf and I want to use a simple macro here
#endif
}
答案 0 :(得分:6)
你可以使用类似的东西:
//#define TYPE_SWITCH
#ifdef TYPE_SWITCH
typedef double myType;
#def PATTERN_MY_TYPE "%lf"
#def MODIFICATOR(a) (a)
#else
typedef long long myType;
#def PATTERN_MY_TYPE "%l"
#def MODIFICATOR(a) (a/10)
#end
#def PATTERN_INT "%d"
//...
int main() {
myType a;
printf(PATTERN_MY_TYPE ", " PATTERN_INT, MODIFICATOR(a), 42);
}
竞争者可以在竞争时连接字符串,这样您就可以使用"foo" "bar"
为定义创建字符串"foobar"
。 ("foo" PATTERN_INT "bar"
会产生"foo%dbar"
。