tired of typing same line over and over again....

This is a discussion on tired of typing same line over and over again.... within the C Programming forums, part of the General Programming Boards category; Originally Posted by matsp [And of course, calling printf instead of a simple function may actually create MORE code - ...

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by matsp View Post
    [And of course, calling printf instead of a simple function may actually create MORE code - so that's an argument for NOT using macros].
    I'm not following you on this one. The only thing you gain from not using a macro is a smaller executable. You will always have more "runtime overhead" (for lack of a better term) when adding function call than not adding more:
    Code:
    void foo( void )
    {
        printf(" ... ");
        system( "pause" );
    }
    
    ...
    
    stuff...
    
    foo( ); /* 1 */
        /* 2, the printf */
        /* 3, the system */
    VS
    Code:
    stuff...
    
    printf... /* 1 */
    system... /* 2 */
    You don't end up with less overhead by adding function calls. The only thing you "lose" is ending up with a larger executable.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    I really don't think any of the pros and cons verses macros and functions deal with the OP's needs. S/he just wants to simplify things a bit cause of the typing.

    Also macros don't guarentee code replacement. It could become just like a function call.

  3. #18
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by strickyc View Post
    I really don't think any of the pros and cons verses macros and functions deal with the OP's needs. S/he just wants to simplify things a bit cause of the typing.

    Also macros don't guarentee code replacement. It could become just like a function call.
    Ultimately the preprocessor decides on how the macro is expanded, but IMO it'll never become a function call.
    On the other hand, functions maybe inlined especially if they are compiler built-ins.

  4. #19
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    IMO, the use of a macro requires extensive justification over the use of a function, while using a function requires zero justification.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    IMO, the use of a macro requires extensive justification over the use of a function, while using a function requires zero justification.
    I agree! I also agree that we are probably just confusing the original poster...

    Just to clarify: If we OFTEN call printf("\n\n"), then a small function which does printf("\n\n") will generate SMALLER code. It will be (a tiny amount in the whole scheme of calling printf) slower because of the extra call, but since it's a call to a function with NO arguments (no pushing on the stack, and no stack cleanup coming back - that is at the very least two instructions, about 7 bytes worth of code-space PER call), it will be shorter than a call to printf. Likewise for system("pause") and system("cls"). If we are really unlucky, the compiler will also generate a new string for EVERY ONE of these calls, but most compilers will only form one of each constant string per compile unit.

    --
    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.

  6. #21
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by matsp View Post
    it will be shorter than a call to printf.
    But that function will be invoking printf, so when invoking the function, it does have to push a string constant onto the stack, invoke printf, and then cleanup stack.

    I might not be following you here.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by matsp View Post
    Just to clarify: If we OFTEN call printf("\n\n"), then a small function which does printf("\n\n") will generate SMALLER code.
    That's the dumbest thing I've heard today. If you make a function which calls printf, it's going to generate more code than if you had just alled printf in the first place.
    Quote Originally Posted by matsp View Post
    It will be (a tiny amount in the whole scheme of calling printf) slower because of the extra call, but since it's a call to a function with NO arguments (no pushing on the stack, and no stack cleanup coming back - that is at the very least two instructions, about 7 bytes worth of code-space PER call), it will be shorter than a call to printf.
    I beg to differ. How on earth is a function wich calls another function going to generate smaller code than you calling that function directly? That doesn't even remotely being to sound correct.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I think matsp is implying that you write a function that does what printf does without actually using printf. IE puts("\n"); or putchar('\n');putchar('\n');

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21