我有点困惑.在开发基于预定义参数的一个函数期间,根据它们的类型传递给sprintf函数所需的精确参数,我发现了非常奇怪的行为(类似于"这是%f%d示例",typeFloat,typeInt).
请查看以下剥离的工作代码:
struct Param {
enum { typeInt, typeFloat } paramType;
union {
float f;
int i;
};
};
int _tmain(int argc, _TCHAR* argv[])
{
Param p;
p.paramType = Param::typeInt;
p.i = -10;
char chOut[256];
printf( "Number is %d\n", p.paramType == Param::typeInt ? p.i : p.f );
printf( "Number is %f\n", p.paramType == Param::typeInt ? p.i : p.f );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的预期产量将是 printf( "Number is %d\n", p.paramType == Param::typeInt ? p.i : p.f );
Number is -10
Run Code Online (Sandbox Code Playgroud)
但它确实打印出来了
Number …Run Code Online (Sandbox Code Playgroud)