C语言 #if #endif 预处理指令,PortAudio



我了解 C 中 #if #endif 预处理器指令的基础知识,因为根据哪个表达式的计算结果为 true,将编译 #if 中的后续代码,但是我目前正在学习 portaudio(我正在为学校制作一个 VOIP 应用程序(,我正在查看他们的一些示例,我对一小部分感到困惑

/* Select sample format. */
#if 1
#define PA_SAMPLE_TYPE  paFloat32
typedef float SAMPLE;
#define SAMPLE_SILENCE  (0.0f)
#define PRINTF_S_FORMAT "%.8f"
#elif 1
#define PA_SAMPLE_TYPE  paInt16
typedef short SAMPLE;
#define SAMPLE_SILENCE  (0)
#define PRINTF_S_FORMAT "%d"
#elif 0
#define PA_SAMPLE_TYPE  paInt8
typedef char SAMPLE;
#define SAMPLE_SILENCE  (0)
#define PRINTF_S_FORMAT "%d"
#else
#define PA_SAMPLE_TYPE  paUInt8
typedef unsigned char SAMPLE;
#define SAMPLE_SILENCE  (128)
#define PRINTF_S_FORMAT "%d"
#endif

我想到的第一个问题是

#if 1
#define PA_SAMPLE_TYPE  paFloat32
typedef float SAMPLE;
#define SAMPLE_SILENCE  (0.0f)
#define PRINTF_S_FORMAT "%.8f"
#elif 1
#define PA_SAMPLE_TYPE  paInt16
typedef short SAMPLE;
#define SAMPLE_SILENCE  (0)
#define PRINTF_S_FORMAT "%d"

#elif 1 不会总是被跳过,因为如果以某种方式 #if 1(#if true(计算为假,#elif 1 是否也会计算为假?

问题21 不是计算为真,0 计算为假吗?那么 #elif 0 不会总是计算为假吗?也就是说这并不重要?

问题3我将通过套接字发送这些示例,跳过此预处理器指令,只使用代码

#define PA_SAMPLE_TYPE  paInt8
typedef char SAMPLE;
#define SAMPLE_SILENCE  (0)
#define PRINTF_S_FORMAT "%d"

#define PA_SAMPLE_TYPE  paUInt8
typedef unsigned char SAMPLE;
#define SAMPLE_SILENCE  (128)
#define PRINTF_S_FORMAT "%d"
#endif
这样

我的SAMPLE_TYPE/SAMPLE可以被视为字符数组/无符号字符(不必将浮点数转换为字符然后再次转换(以便从套接字写入/读取,这是否更合适?

您需要

了解的是,在 #if/#elif/#else 序列之间,只会选择一个条件:

#if在此处选择:

#if 1
// only this one will be selected
#elif 1
#else
#endif

#elif在此处选择:

#if 0
#elif 1
// only this one will be selected
#else
#endif

#else在此处选择:

#if 0
#elif 0
#else
// only this one will be selected
#endif

最新更新