Thread: f() or f(void)

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    f() or f(void)

    Should i use f() or f(void) to indicate that the function doesn't take any argument. I read in an C++ FAQ that it considered to be a bad programming style in C++ but what with C ? I also know that if f() is used it is assumed to take any number of int arguments.
    Actually the FAQ confused me, although i prefer to use f(void).
    Thanks
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would say that f() is by far the most common, including in (eg) C library sources.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    I would say that f() is by far the most common, including in (eg) C library sources.
    It does make a difference though. If you declare:

    Code:
    void f();
    And then somebody calls:

    Code:
    f( 1, 2, 3, "foo" );
    The compiler will not complain. The only time I use such "pure variadic" functions is in the context of a function pointer. I don't ever declare real functions that way. If it really takes no arguments, you should say "void."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    maybe this might help

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by brewbuck View Post
    The only time I use such "pure variadic" functions is in the context of a function pointer.
    However, "old style" function calls (void f(); f();) are subject to the default argument promotions, which can make a mismatch when used with prototyped functions (void f(void); f();) if the promoted argument type is different from the non-promoted argument type, invoking undefined behavior.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well, this was good execise since i never checked this before. It seems that the compile is confused whether to call the function f with the an argument even tho the function takes no argumrnts. It is much to specify the void atleast in C to say that the function takes no arguments. If not its an undefined behaviour.

    Code:
    #include <stdio.h>
    
    void f()
    {
         printf("helloe");
    }
    
    int main()
    {
        f(10);
        
        getchar();
        return 0;
    }
    /* my output
    helo
    */
    I am above code, i was expected an error. But the compiler dint complain and it was happy to execute. But when places a void int he f function. It did warn me!

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Thanks brewbuck. Your example was nice. As ssharish said my compiler too didn't throw any error but it did warn me too.
    Also thanks itCbitC for the faq. That FAQ explained it nicely.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssharish2005 View Post
    Code:
    #include <stdio.h>
    
    void f()
    {
         printf("helloe");
    }
    
    int main()
    {
        f(10);
        
        getchar();
        return 0;
    }
    /* my output
    helo
    */
    Care to rethink that?


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

  9. #9
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by BEN10
    Should i use f() or f(void)
    for emty list it is not much matter, but if you have function which defined as int func(float a); with old style declaration as int func(); argument from the call func(2.2f); will be casted to double, then this double-float difference will cause an error
    with new style of declaration compiler will say you about mistake if for function int func(void); you use call func(1);

Popular pages Recent additions subscribe to a feed