Help❓ Printing var name

Posts 1–9 of 9 · Page 1 of 1
❓ Printing var name
So, I wanna print my var name as well as her value, since generic programming is DATHING I'd love to get a solution to my problem, actually this is what I have:

Code:
 #define PrintVar(varName) printf("%s vale %p\n", #varName, varName)
Will work with int, but will print the address for anything else, any idea ?
Well "%p" is for printing a pointer...
so i think you have to create a class for that maybe..

something like this :
Code:
class Var 
{
 public: 
 void Printfloat(float var);
{
 printf("%0.2f \n" , var);
}
 void PrintInteger(int var)
{
  printf("%d \n", var);
}
// etc...
    void PrintString();
    void Printchar();
};
then
Code:
int main()
{
Var *PrintVar = new Var();
PrintVar->Printfloat(73.37f);
return 0;
}
I used a pointer so I could try playing with the void :3 If I need to if/else then:
Code:
#define PrintVar(varName, type) printf("%s vale ", #varName);\
	if (type == "f") printf("%f\n", varName);\
	else if (type == "d") printf("%d\n", varName);\
	else if (type == "c") printf("%c\n", varName);\
	else if (type == "s") printf("%s\n", varName);\
	else if (type == "l") printf("%llu", varName);
,also forgot to say I'm on pure C, not C++.
On C++ it's a way easier:
Code:
 #define PrintVar(varName) cout << #varName << " vale " << varName << endl;
Thanks for the help anyways.
Still open to suggestions.
Hmmmmmmmmmm, i don't quite understand what your problem is...

EDIT: ah! i see, you wanted to print the actual variable's name. hold on il edit my post later.
Why do you stick to C?
Quote Originally Posted by Hell_Demon View Post
Why do you stick to C?
I think he is coding in Linux...



BTW i forgot to edit my post, i tested your code in VC++ 2013 works perfectly, i retried in Code::Blocks Lubuntu and it worked aswell (C++)
tried building a .C code manually with gedit and and it didn't work , but it works with C++ (gcc) ._.
Gonna try find a way eventually, and oh, can't use typeof/sizeof (before someone suggest) :3
Quote Originally Posted by Hell_Demon View Post
Why do you stick to C?
Curiosity :3
I think he is coding in Linux...

Quote Originally Posted by somewhatpro View Post
I think he is coding in Linux...



BTW i forgot to edit my post, i tested your code in VC++ 2013 works perfectly, i retried in Code::Blocks Lubuntu and it worked aswell (C++)
tried building a .C code manually with gedit and and it didn't work , but it works with C++ (gcc) ._.
You mean g++ with gcc right ?. And what code did you test, C++ or C one ?
Quote Originally Posted by Cernunnos View Post
Curiosity :3
I think he is coding in Linux...


You mean g++ with gcc right ?. And what code did you test, C++ or C one ?
both of them, the code compiled in C++ worked, but the C code didn't work well, must be some compiler bug?
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us