gpt4 book ai didi

c - 如何使用宏连接两个正整数?

转载 作者:太空宇宙 更新时间:2023-11-04 04:12:08 24 4
gpt4 key购买 nike

我需要编写一个程序,使用宏将两个正整数连接起来形成另一个正整数。例如,如果 x=12y=35,答案应该是 1235。我应该怎么做?

最佳答案

你只需要使用##操作符

It is often useful to merge two tokens into one while expanding macros. This is called token pasting or token concatenation. The ‘##’ preprocessing operator performs token pasting. When a macro is expanded, the two tokens on either side of each ‘##’ operator are combined into a single token, which then replaces the ‘##’ and the two original tokens in the macro expansion. Usually both will be identifiers, or one will be an identifier and the other a preprocessing number. When pasted, they make a longer identifier. This isn’t the only valid case. It is also possible to concatenate two numbers (or a number and a name, such as 1.5 and e3) into a number. Also, multi-character operators such as += can be formed by token pasting.

来自 https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html

因此,在您的情况下,#define CONCAT_NUMBER(FIRST, SECOND) (FIRST ## SECOND) 应该有效

关于c - 如何使用宏连接两个正整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55988829/

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