Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the following C macro do?

Tags:

c

#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?

like image 220
lilzz Avatar asked May 28 '13 01:05

lilzz


People also ask

What does a macro do in C?

Macro in C programming is known as the piece of code defined with the help of the #define directive. Macros in C are very useful at multiple places to replace the piece of code with a single value of the macro. Macros have multiple types and there are some predefined macros as well.

What does ## mean in C macro?

The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token, and therefore, can't be the first or last token in the macro definition.

What is function-like macro in C?

and as part of C++0x. Function-like macro definition: An identifier followed by a parameter list in parentheses and the replacement tokens. The parameters are imbedded in the replacement code. White space cannot separate the identifier (which is the name of the macro) and the left parenthesis of the parameter list.

What does #define do in C?

The #define creates a macro, which is the association of an identifier or parameterized identifier with a token string. After the macro is defined, the compiler can substitute the token string for each occurrence of the identifier in the source file.


2 Answers

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".

like image 74
fusha Avatar answered Nov 15 '22 23:11

fusha


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.

like image 42
Sergey Kalinichenko Avatar answered Nov 15 '22 23:11

Sergey Kalinichenko