C to use defined macro in current macro block [duplicate] - c

This question already has answers here:
How can I concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"?
(3 answers)
Stringification - how does it work?
(2 answers)
Closed 7 years ago.
I am implementing a generic Hash Table in C using macros.
As one example, I defined key, value types as
#define h_key_type int
#define h_val_type int
Then I defined put function as:
#define H_PUT(key_type, val_type) \
void key_type##_put(...)
H_PUT(h_key_type, h_val_type);
However, after preprocessing the processed source codes become
void h_key_type_put(...)
Even I changed the function declaration as:
#define H_PUT() \
void h_key_type##_put(...)
it's still replaced as:
void h_key_type_put(...)
So I have to use
#define H_PUT(key_type, val_type) \
void key_type##_put(...)
H_PUT(int, int)
to make it work.
But it's not convenient since I either have to introduce a gigantic define block, or I have to type key, value types for each function, which is not elegant.
Any ideas?

Related

Alternative to function overloading in C? [duplicate]

This question already has answers here:
How to achieve function overloading in C?
(14 answers)
Alternatives to function overloading in C
(2 answers)
Closed 4 years ago.
Since function overloading isn't allowed directly in C, how can it be done for these functions?
void MotorDriver_Create(float speedAddress,float frequencyAddress, float db_input);
void MotorDriver_Create(float speedAddress, float frequencyAddress, float minDB, float maxDB);
There is no way to achieve function overloading akin to what is available in C++ since, unlike in C++, function signatures are not mangled in a way that permits overloading. What you can do is use the _Generic keyword to map a macro into whichever function call best suits the argument type. For example:
#include <math.h>
#include <float.h>
#define sqrt(X) _Generic((X), \
long double: sqrtl, \
default: sqrt, \
float: sqrtf \
)(X)

Stringinize and concat macro parameters correctly [duplicate]

This question already has answers here:
Treating __func__ as a string literal instead of a predefined identifier
(4 answers)
Closed 4 years ago.
Basically, i have the following macro definition :
#include <stdio.h>
#define altErrMsg(x,y,z) x":"#y":"z
#define errMSG(x,y,z) altErrMsg(x,y,z)
#define __failure() errMSG(__FILE__,__LINE__,__FUNCTION__)
int
main (int argc, char **argv)
{
puts (__failure ());
return 0;
}
The macro __failure() is supposed to print some debugging information in the form "filename:line:function". For this purpose i used the GCC's predefined macros __LINE__, __FILE__ and __FUNCTION__. I used an indirection so that the predefined macros will be expanded before they are concatenated. The expansion of __LINE__ must be stringized (using the # before the parameter's name).
AFAIK, __failure() will be expanded to somthing like : "test.c"":""20"":""main" which will be quoted into a one single string constant "test.c:20:main". But that's not happening, instead, I'm getting errors :
test.c:5:46: error: expected ‘)’ before ‘__FUNCTION__’
#define __failure() errMSG(__FILE__,__LINE__,__FUNCTION__)
^
test.c:3:35: note: in definition of macro ‘altErrMsg’
#define altErrMsg(x,y,z) x":"#y":"z
^
test.c:5:21: note: in expansion of macro ‘errMSG’
#define __failure() errMSG(__FILE__,__LINE__,__FUNCTION__)
^~~~~~
test.c:10:11: note: in expansion of macro ‘__failure’
puts (__failure ());
Compiling with gcc -E shows that the __FUNCTION__ is never expanded and the final string looks like this : "test.c"":""22"":"__FUNCTION__ which is a wrong syntax but i have no idea why this happens !
Is there an explanation for this behavior ? and any correction to the issue ?
If you ask why then from Predefined macros
C99 introduced __func__, and GCC has provided __FUNCTION__ for a long time. Both of these are strings containing the name of the current function (there are slight semantic differences; see the GCC manual). Neither of them is a macro; the preprocessor does not know the name of the current function.
Not a macro - that's why not macro expanded. If this was your intention it won't work. (As you have seen).
Solution is to use a function where you will pass these things and print them accordingly. That would work.
void my_log( const char * filename, int linenumber, const char * funcname){
fprintf(sdtout,"%s[%d]%s\n",filename, linenumber, funcname);
}
And call like my_log(__FILE__,__LINE__,__FUNCTION__);.

What is the meaning of such definition "#define __arch_swab32 __arch_swab32"? [duplicate]

This question already has answers here:
Why define a macro with the same name and content in C? [duplicate]
(2 answers)
Closed 5 years ago.
In /usr/include/asm/swab.h I found following code:
static __inline__ __u32 __arch_swab32(__u32 val)
{
__asm__("bswapl %0" : "=r" (val) : "0" (val));
return val;
}
#define __arch_swab32 __arch_swab32
What is the meaning of the last line, defining a name as itself?
This is called self-referentitial macro:
One common, useful use of self-reference is to create a macro which expands to itself. If you write
#define EPERM EPERM
then the macro EPERM expands to EPERM. Effectively, it is left alone by the preprocessor whenever it's used in running text. You can tell that it's a macro with #ifdef. You might do this if you want to define numeric constants with an enum, but have #ifdef be true for each constant.

How to detect a type is pointer in preprocessor of C? [duplicate]

This question already has answers here:
Check if a macro argument is a pointer or not
(6 answers)
Closed 5 years ago.
Is there a way to detect whether a type is pointer in preprocessor of C?
Suppose its name is IS_POINTER. What the final result I want may looks like:
#define DATA_STRUCTURE(KEY_T)
#if IS_POINTER(KEY_T)
/* do something */
#endif
Thanks!
The preprocessor has no notion of types, you cannot write such a macro that can be used in a #if directive.
Conversely, you can use some non-portable built-in functions to write an expression that does check if a given object is a pointer or something else.
Here is a macro to perform a static assertion that a is an array:
#define assert_array(a) \
(sizeof(char[1 - 2 * __builtin_types_compatible_p(typeof(a), typeof(&(a)[0]))]) - 1)
It can be used with gcc and clang. I use it to make the countof() macro safer:
#define countof(a) ((ssize_t)(sizeof(a) / sizeof(*(a)) + assert_array(a)))
You could try using typeof(expr), which may help you in your task. It doesn't exactly tell you something is a pointer, but perhaps you could use it in comparisons:
https://gcc.gnu.org/onlinedocs/gcc/Typeof.html

Universal macro which can have zero or one parameter [duplicate]

This question already has answers here:
How to define macro function which support no input parameter and support also input parametr in the same time
(4 answers)
Overload C macros
(2 answers)
Closed 8 years ago.
Is there any way to define macro which can have one or zero parameters?
I need something to be used as in this example:
#define MY_RETURN(ret) return ret;
void foo(){
MY_RETURN();
}
int foo_integer(){
MY_RETURN(1);
}
I am not able to find solution for this.
In C99 and later, it is possible to use variadic macros.
C11 (n1570), § 6.10 Preprocessing directives
# define identifier lparen identifier-list , ... ) replacement-list new-line
Your macro may look something like:
#define MY_RETURN(...) return __VA_ARGS__
If you need to count arguments, you may check here for instance.
Define two MY_RETURN macros, one with parameter and one without! A kind of overloading, but for macros.
Based on the answer of How to define macro function which support no input parameter and support also input parametr in the same time
Consider something like this:
#define MY_RETURN(ret) { \
int args[] = {ret}; \
if(sizeof(args) > 0) \
return ret; \
}

Resources