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.
相关问题
- Unity - Get Random Color at Spawning
- How to generate a random number, then display it o
- how to randomly loop over an array (shuffle) in ba
- How to fill an array with random numbers from 0 to
- Upper bound of random number generator
相关文章
- why 48 bit seed in util Random class?
- How can I use gcc's -I command to add recursiv
- Need help generating discrete random numbers from
- Using C Preprocessing to get integer value of a st
- Is it possible to use #define from other cpp file?
- Get random records with Doctrine
- Looking for a fast hash-function
- Oracle random row from table
Don't do this in C. You'll end up confusing people. If you need to create variables on the fly, use
malloc
andrealloc
and maintain an array of their values.To answer your question, no. The preprocessor doesn't include a random number generator. You can generate random numbers at runtime (with
rand()
), but if you really need them at compile time, you'll have to write your own preprocessor and run your code through it. Or you could just use 4, which was randomly determined by a roll of a fair 100 sided die.Based on 1999-01-15 Jeff Stout (thanks to @rlb.usa)
Output:
I take your question that you want to have a way of creating unique identifier tokens through the preprocessor.
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.If you have a C99 compiler you can use P99. It has macros called
P99_LINEID
andP99_FILEID
. They can be used asand similarily for
P99_FILEID
.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.
These two macros also have counterparts
P99_LINENO
andP99_FILENO
that just produce large numbers instead of identifier tokens.