使用C预处理器生成一个随机数 [英] Generate a random number using the C Preprocessor

查看:131
本文介绍了使用C预处理器生成一个随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 C 预处理器生成一个随机数或字符串... um ...我什至不知道这样做是否可行,但我正在尝试在飞行(字符串在这里会有所帮助)并为其分配值(整数).因此,我想做一些事情,但基本问题仍然存在-我可以使用预处理器创建随机字符串或数字吗?

I would like to generate a random number or string using the C Preprocessor ... um ... I don't even know if this is possible, but I am trying to create variables on the fly (strings would be helpful here) and assign them values (integers). So there are a few things I am trying to do but the basic question remains - can I create a random string or number using the preprocessor.

推荐答案

我想问您一个问题,您想要一种通过预处理器创建唯一标识符令牌的方法.

I take your question that you want to have a way of creating unique identifier tokens through the preprocessor.

gcc具有一个名为__COUNTER__的扩展名,它可以实现您期望的名称.您可以将其与宏串联##结合使用以获得唯一标识符.

gcc has an extension that is called __COUNTER__ and does what you expect from its name. You can combine this with macro concatenation ## to obtain unique identifiers.

如果您使用的是C99编译器,则可以使用 P99 .它具有称为P99_LINEIDP99_FILEID的宏.它们可以用作

If you have a C99 compiler you can use P99. It has macros called P99_LINEID and P99_FILEID. They can be used as

#include "p99_id.h"

P99_LINEID(some, other, tokens, to, make, it, unique, on, the, line)

,并且与P99_FILEID类似.

第一个选项会根据您的令牌和行号以及一个哈希值(该哈希值取决于文件"p99_id.h"的包含次数)来处理.第二个宏仅使用该哈希而不是行号,以便在同一编译单元内的多个位置重现名称.

The first mangles a name from your tokens and the line number and a hash that depends on the number of times the file "p99_id.h" had been included. The second macro just uses that hash and not the line number such that a name is reproducible at several places inside the same compilation unit.

这两个宏还具有对应的P99_LINENOP99_FILENO,它们只产生大量数字而不是标识符令牌.

These two macros also have counterparts P99_LINENO and P99_FILENO that just produce large numbers instead of identifier tokens.

这篇关于使用C预处理器生成一个随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆