My thinking is to use somehow the if
operator lots of times and check for every type:
if (the variable has type int){
return 0;
}
if (the variable has type double){
return 1;
}
...
My thinking is to use somehow the if
operator lots of times and check for every type:
if (the variable has type int){
return 0;
}
if (the variable has type double){
return 1;
}
...
Your question has no much meaning in C11 (read carefully n1570 for details): the type of a given variable (or value) is known at compile time and forgotten at runtime, because you declare that (global, static, or automatic) variable with some explicit type. Be also aware that calling conventions and ABIs define how data of different types might be handled differently (e.g. passed in various and different processor registers or on the call stack).
Likewise, a function has a fixed and well-defined signature (you should give it when declaring the function, e.g. in some header file).
If you pass the address of some data as a void*
formal argument (or some data as variadic argument), you need some convention to get its type (often, you'll pass another formal argument describing the type; printf
or scanf
is a typical example). For variadic functions, consider stdarg.h
facilities.
Maybe you want to consider generic selection with _Generic
used in a macro. This is a feature that I very rarely use.
Maybe you want to customize your GCC compiler thru some GCC plugin. That could take months of work (and is dependent of your particular version of GCC).
Maybe you want some tagged union or sum type. You'll typically implement that abstract data type with a struct
containing an enumerator (choosing the type) and a union
. For inspiration look into Glib's GVariant, and, since Glib is free software, study its source code.
Perhaps you might want to look into libffi.
If you (specifically) use GCC, consider some of its C language extensions (outside of the C11 standard). You might want typeof
or some builtins like __builtin_types_compatible_p
.
At last, you might generate some C file, perhaps using your script, another preprocessor like GPP or m4, etc...
You should describe your actual problem in details. In its initial form, your question is typically some XY problem.