gpt4 book ai didi

c++ - 串行流和定义的变量

转载 作者:行者123 更新时间:2023-11-28 07:13:16 25 4
gpt4 key购买 nike

希望新手提出一个简单的问题...

如果我有一个已定义的变量:

#define LOCK_OUT   0xA00A

我在这样的串行流中使用它:

iprintf("%s Sent to the system. \r\n", LOCK_OUT);

在我的串行终端上,我会看到 LOCK_OUT 的文本表示还是数字表示?例如:

“LOCK_OUT 已发送到系统。”

特别是,我有一个检查已定义宏数组的循环,我想在输出串行流中使用选定的宏(一旦找到)。所以这就是循环:

int UDPDATA;
for (int i = 0, i < UDP_Size; i++)
{
if (MACRO_ARRAY[i] == UDPDATA iprintf("%s Sent to system \r\n", MACRO_ARRAY[i]);
}

我想要发送到串行流的宏名称,而不是它所代表的值。我希望我能正确解释这个......对比

“0xA00A 发送到系统。”

我想做第一个,不确定是否可能...谢谢!

最佳答案

因为宏是替换,你必须这样做:

#include <stdio.h>

#define STRINGIFY(x) #x
#define LOCK_OUT 0xA00A

int main() {
// %#04x prints 4 hexadecimal places
printf("%s %#04x Sent to the system. \r\n", STRINGIFY(LOCK_OUT), LOCK_OUT);
return 0;
}

关于c++ - 串行流和定义的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20640372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com