Thread: Defining printf...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    Defining printf...

    Im trying to define printf("%d",var) to something more easy,but i cant figure out how.

    I want it something like printvar(var),but i cant figure out a code to define it like that =\

    Thanks

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    #define printvar(var) printf("%d",var)
    I'm pretty sure that's how macro functions work.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But when var is no longer an int, what are you going to do then?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    yes,but that means i would have to name the variable "var".

    i want something where i can go,
    Code:
    printvar(var);
    printvar(var2);
    printvar(anothervar);
    The code you gave me only defines "var".

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SC__Programmer View Post
    yes,but that means i would have to name the variable "var".

    i want something where i can go,
    Code:
    printvar(var);
    printvar(var2);
    printvar(anothervar);
    The code you gave me only defines "var".
    I think you need to think more deeply about how functions (and function-like macros) work. Does this mean I can never call printf with different things inside it?

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by SC__Programmer View Post
    yes,but that means i would have to name the variable "var".

    i want something where i can go,
    Code:
    printvar(var);
    printvar(var2);
    printvar(anothervar);
    The code you gave me only defines "var".
    No, you can use any variable name you want. You can use #define printvar(derp) printf("%d",derp) if you want to, it really doesn't matter.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Salem View Post
    But when var is no longer an int, what are you going to do then?
    Is there any way to do that?
    I thought preprocessor macros had absolutely no idea about types.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Is there any way to do that?
    I thought preprocessor macros had absolutely no idea about types.
    Unless you put something type specific in them that is.... like printf("%d", var) ... the macro doesn't care but printf() does.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by CommonTater View Post
    Unless you put something type specific in them that is.... like printf("%d", var) ... the macro doesn't care but printf() does.
    So, if I understand it correctly..It would, by definition, be impossible to have the same macro (as here) to printf() different types of variables. Correct?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    So, if I understand it correctly..It would, by definition, be impossible to have the same macro (as here) to printf() different types of variables. Correct?
    Correct... You would need to create a macro for each type...
    PRINTINT() PRINTULONG() PRINTFLOAT() etc.

    Frankly the gain from this over just using printf() directly is almost zero. All you're really doing is obfuscating the behavior of your code.

  11. #11
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Incidentally, it would be good to get into the habit of enclosing macro arguments in parentheses to force the correct order evaluation. It makes no difference in this case, but consider the following:

    Code:
    #define square(x) x*x
    If you then use it in your code with, say, square(a+5) you will get an unexpected result because it will expand in-line to a+5*a+5 rather than (a+5)*(a+5). This is correct:
    Code:
    #define square(x) (x)*(x)
    Code:
    while(!asleep) {
       sheep++;
    }

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by TheBigH View Post
    Incidentally, it would be good to get into the habit of enclosing macro arguments in parentheses to force the correct order evaluation. It makes no difference in this case, but consider the following:

    Code:
    #define square(x) x*x
    If you then use it in your code with, say, square(a+5) you will get an unexpected result because it will expand in-line to a+5*a+5 rather than (a+5)*(a+5). This is correct:
    Code:
    #define square(x) (x)*(x)
    No it isn't. Try "~square(x)". Not only do you need to put each argument in parentheses, the entire expression needs to be in parentheses too.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Yes, but who would want to do something like that?
    Code:
    while(!asleep) {
       sheep++;
    }

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TheBigH View Post
    Yes, but who would want to do something like that?
    We're about 10 posts past where that question was needed.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by TheBigH View Post
    Yes, but who would want to do something like that?
    square is a toy example that nobody would actually use, but generally applying a high precedence operator to the result of a function-like macro is not unheard of.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #Defining
    By pc_doctor in forum C Programming
    Replies: 2
    Last Post: 05-25-2010, 08:04 AM
  2. Defining IDs
    By jmd15 in forum Windows Programming
    Replies: 3
    Last Post: 03-12-2005, 01:56 PM
  3. make printf using printf?
    By germaneater in forum C Programming
    Replies: 9
    Last Post: 11-10-2004, 10:58 PM
  4. Defining
    By Tigerworks in forum C Programming
    Replies: 7
    Last Post: 03-09-2003, 05:34 AM
  5. defining codes
    By kwesley in forum C Programming
    Replies: 1
    Last Post: 10-01-2002, 02:58 PM