Results 1 to 9 of 9
  1. #1
    Cernunnos's Avatar
    Join Date
    May 2013
    Gender
    male
    Posts
    38
    Reputation
    10
    Thanks
    4

    Question 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 ?

  2. #2
    somewhatpro's Avatar
    Join Date
    May 2014
    Gender
    male
    Posts
    317
    Reputation
    38
    Thanks
    29
    My Mood
    Daring
    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;
    }

  3. #3
    Cernunnos's Avatar
    Join Date
    May 2013
    Gender
    male
    Posts
    38
    Reputation
    10
    Thanks
    4
    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.
    Last edited by Cernunnos; 07-15-2014 at 02:26 PM.

  4. #4
    somewhatpro's Avatar
    Join Date
    May 2014
    Gender
    male
    Posts
    317
    Reputation
    38
    Thanks
    29
    My Mood
    Daring
    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.
    Last edited by somewhatpro; 07-15-2014 at 11:00 PM.

  5. #5
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Why do you stick to C?
    Ah we-a blaze the fyah, make it bun dem!

  6. #6
    somewhatpro's Avatar
    Join Date
    May 2014
    Gender
    male
    Posts
    317
    Reputation
    38
    Thanks
    29
    My Mood
    Daring
    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) ._.
    Last edited by somewhatpro; 07-21-2014 at 04:09 PM.

  7. #7
    Cernunnos's Avatar
    Join Date
    May 2013
    Gender
    male
    Posts
    38
    Reputation
    10
    Thanks
    4
    Gonna try find a way eventually, and oh, can't use typeof/sizeof (before someone suggest) :3
    Last edited by Cernunnos; 07-21-2014 at 06:00 PM.

  8. #8
    Cernunnos's Avatar
    Join Date
    May 2013
    Gender
    male
    Posts
    38
    Reputation
    10
    Thanks
    4
    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 ?

  9. #9
    somewhatpro's Avatar
    Join Date
    May 2014
    Gender
    male
    Posts
    317
    Reputation
    38
    Thanks
    29
    My Mood
    Daring
    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?

Similar Threads

  1. [Release] TeknoMW3 : Print XUID/Name of All Clients
    By mwxplayer in forum Call of Duty Modern Warfare 3 Private Server Hacks
    Replies: 45
    Last Post: 01-07-2014, 01:39 AM
  2. Leet Names :)
    By EleMentX in forum Spammers Corner
    Replies: 18
    Last Post: 09-20-2010, 10:34 PM
  3. No-name game
    By SadisticGrin in forum Spammers Corner
    Replies: 12
    Last Post: 01-28-2010, 10:58 AM
  4. Post your game name here
    By styx23 in forum WarRock - International Hacks
    Replies: 49
    Last Post: 07-17-2007, 08:47 PM
  5. Real Names
    By JTLOBAMHSK in forum General
    Replies: 54
    Last Post: 06-12-2006, 04:10 AM