Reputation: 359
I'm trying to create a function that takes in a variable and print out it name
It'd look something like this
#include <stdio.h>
void print_name(int a);
int main()
{
int a_random_name;
print_name(a_random_name);
return 0;
}
The output:
a_random_name
How would i be able to do this in c ?
Upvotes: 0
Views: 238
Reputation: 11386
As discussed in the comments, you cannot use a function to do so, but a macro:
#include <stdio.h>
#define print_name(x) (sizeof (x), printf("%s\n", #x))
void foo() {}
int main()
{
int a_random_name;
print_name(a_random_name);
print_name(foo);
return 0;
}
// Prints:
// a_random_name
// foo
Note that the (sizeof (x),
part of the macro is here to make sure that the variable exists.
More information on macros manipulation:
C11 macros specifications:
Upvotes: 8