简体   繁体   中英

What does the following C macro do?

#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif

What does ## mean in C? Is that a placeholder or function?

It is called the token pasting operator , it concatenates tokens so that 123313 ## LL becomes 123313LL during the preprocessing.

There is also a stringification operator # , which converts #name into "name" .

No, ## is not a placeholder for a function, it is a token pasting operator. It is valid only inside preprocessor macros (with or without parameters). It produces a concatenation of its left and right sides.

For example, if you pass INT64_C a value of 123

INT64_C(123)

the result produced by the preprocessor would be equivalent to writing

123LL

The idea behind these macros is to make signed and unsigned constants stand out in the code a little more: a value that looks like INT64_C(123) may be a little more readable than the equivalent 123LL . It is definitely a big improvement over its other equivalent 123ll , which looks like a completely different number.

## means to concatenate two tokens.

So (c ## LL) will be pre-processed to cLL .

But pay attention, it's done in pre-processing stage so it's not like strcat .

int i = 3;
INT64_C(i);

will generate iLL instead of 3LL .

As others mentioned, ## pastes two tokens together.

#define INT64_C(c) (c ## LL)

So, INT64_C(123) becomes (123LL) after macro expansion.

These macros exist so you can portably use int64_t constants. On most 64-bit systems, the macro will be defined as such:

#define INT64_C(c) (c ## L)

This is because on most 64-bit systems, int64_t is long so the constant should be 123L . On most 32-bit systems and on Windows, int64_t is long long so the constant should be 123LL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM