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)
Related
This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 5 years ago.
I was going through the make program's source code and I came across the following function declaration:
struct dep *
read_all_makefiles (makefiles)
char **makefiles;
{ ... followed by function code ...
How do we decipher this declaration?
It's the old K&R style of function parameter declaration, prior to the ANSI/ISO standard C. This style is outdated now but can still be found in some very old codes. Although it's still in standard, it's recommended not to write like this anymore.
To decipher, simply move the parameter declaration list back to the function prototype, one-by-one, with the identifiers matching.
Quoting draft N1570, §6.9.1/13:
EXAMPLE 1
extern int max(int a, int b)
{
return a > b ? a : b;
}
EXAMPLE 2
extern int max(a, b)
int a, b;
{
return a > b ? a : b;
}
See Alternative (K&R) C syntax for function declaration versus prototypes
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
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?
This question already has answers here:
"static const" vs "#define" vs "enum"
(17 answers)
Why would someone use #define to define constants?
(9 answers)
Closed 8 years ago.
I would like to understand the difference between defining a constant variable as follows:
const int ONE = 1;
and using a preprocessor directive:
#define ONE (1)
I know that in the second case "1" gets in some sense hardcoded and the compiler does not even see the variable ONE, but I am not sure about the first case. The fact of declaring the variable as constant just prevents from accidentally change its value, or does the compiler catch the opportunity to do some optimization? Is there any significant benefit of one approach over the other?
In C, const int c; means that c can't be modified during the run of program. However, c is not a constant during compile time and can not be used in constant expressions. So for example the program:
const int MAX = 10;
int a[MAX];
does not compile, while:
#define MAX 10
int a[MAX];
does.
In C++, const variables are true compile-time constants, so there may be less reasons to use #define for it. An example where #define is necessary is when you need to use the constant in an #if directive.
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; \
}