Thread: Printing a variable's name.

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Printing a variable's name.

    Hello everyone. I'm new to C and I'm having trouble printing a variable's name.
    Such as if I have int a =2, is there a way to print "a".

    I would like my print statement to look like:

    printf("variable's name %d \n",a);

    and "a 2" to be printed to the screen.

    I've been trying to find an answer on google but haven't had any luck.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Such as if I have int a =2, is there a way to print "a".
    No.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    We only have variable names so humans don't get confused (like it really helps).
    After your program gets turned into assembly and eventually machine code, the computer doesn't give a damn what you name your variables.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Unhappy

    Quote Originally Posted by NeonBlack View Post
    We only have variable names so humans don't get confused (like it really helps).
    After your program gets turned into assembly and eventually machine code, the computer doesn't give a damn what you name your variables.
    That makes sense. Too bad it doesn't help me. Thanks anyway.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    #include <stdio.h>
    
    #define print_int(x) printf("Variable " #x "= &#37;d\n", x)  
    #define print_char(x) printf("Variable " #x "= %c\n", x)  
    #define print_string(x) printf("Variable " #x "= %s\n", x)  
    
    int main (int argc, const char * argv[]) {
    	int a = 2 ; 
    	char letter = 'Z' ; 
    	char phrase[] = "Look, I can do this!!" ; 
    	
    	print_int(a) ;
    	print_char(letter) ; 
    	print_string(phrase) ; 
    	return 0;
    }
    Output is
    Code:
    [Session started at 2008-03-11 23:01:17 -0500.]
    Variable a= 2
    Variable letter= Z
    Variable phrase= Look, I can do this!!
    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Code:
    #include<stdio.h>
    #define display(n) printf(#n);
    int main()
    {
        int n;
        display(n);
        return 0;
    }
    The preprocessor directive # when used with the variable in the macro outputs the name of the variable that was used.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Awesome, I really appreciate the help. Thanks.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Ah yes, the good old "stringizing" operator. Also useful is the token pasting operator '##'. Don't ask what I've used it for, it's wrong. Very, very wrong.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    One more question if you don't mind.

    I'm using the #define print_int(x) printf("Variable " #x "= %d\n", x) method; but mine looks like this:
    #define print_int(x) printf(#x "\t%d\t %d %% \n", x-1, ((x-1)*100)/total)

    Is there a way to right justify the #x? I've tried putting %5c in quotes in front of it with a space as the char and a bunch of other stuff like this but no luck.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #define print_int(x) printf("%5s\t%d\t %d %% \n", #x, x-1, ((x-1)*100)/total)
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  3. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  4. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  5. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM