Thread: If a function prototype parameter field is left empty does it assume an int?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    If a function prototype parameter field is left empty does it assume an int?

    Take this code as an example
    Code:
    #include <stdio.h>
    
    void function1();
    void function2( void );
    
    int main()
    {
    //	function1( 5 );
    
    	function1();
    //	function2( 5 );
    
    }
    
    /*
    void function1( int x )
    {
    
    	return;
    
    }
    */
    
    void function1()
    {
    
    	return;
    
    }
    
    
    
    /*
    void function2( int x )
    {
    
    	return;
    
    }
    */

    The parameter field for the prototype of function1 is left blank. This lets me create either a function1 that takes an int parameter or a function1 that takes no parameters and nothing else. Could someone explain this to me?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    When you supply no parameters, you're not actually creating a prototype, just a declaration. An empty parameter lists means an unspecified number of arguments. You can pass one int, but you could also pass, say, 5 ints. The compiler does not check whether the call is correct because it can't.

    You should never use an empty parameter list. It's a holdover from the pre-prototype days of C, and prototypes are better, because they allow the compiler to make sure you're calling a function with the proper types.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by yougene
    The parameter field for the prototype of function1 is left blank. This lets me create either a function1 that takes an int parameter or a function1 that takes no parameters and nothing else. Could someone explain this to me?
    The empty parameter list means that the number and types of the parameters are unspecified in that declaration.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    182
    Thanks for all the helpful replies


    @laserlight
    if the types of parameters are unspecified why does this produce an error for me?
    Code:
    #include <stdio.h>
    
    void function1();
    
    int main()
    {
    
    	function1( 'p' );
    
    }
    
    
    void function1( char x )
    {
    
    	return;
    
    }



    Code:
    eugene@eugene-laptop:~/cfiles$ cc -Wall -pedantic testprototypes.c
    testprototypes.c:8:1: warning: C++ style comments are not allowed in ISO C90
    testprototypes.c:8:1: warning: (this will be reported only once per input file)
    testprototypes.c: In function ‘main’:
    testprototypes.c:13: warning: control reaches end of non-void function
    testprototypes.c: At top level:
    testprototypes.c:25: error: conflicting types for ‘function1’
    testprototypes.c:25: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
    testprototypes.c:3: error: previous declaration of ‘function1’ was here
    eugene@eugene-laptop:~/cfiles$
    It didn't work with char types for some reason.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by yougene
    It didn't work with char types for some reason.
    According to the error message, "an argument type that has a default promotion can’t match an empty parameter name list declaration". So the problem is that char would be promoted to int, which would then cause a mismatch with the function definition's char parameter. (That said, I am not entirely sure why the error message was phrased that way since 'p' is of type int, so there should not have been a type promotion, from what I see.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by yougene View Post
    It didn't work with char types for some reason.
    Fascinating. This is something I haven't seen before. The spec has the answer...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    182
    mmmm, C voodoo

    Thanks guys

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by yougene View Post
    mmmm, C voodoo

    Thanks guys
    What compiler are you using to test this?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Looks like gcc's output, and I think that's what it is because of this thread. Never seen this syntax before. Help?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    182
    Yeah, it's GCC

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I think it's interesting, because you first declare a prototype with unspecified arguments, then you define the function with specified arguments -- apparently, gcc allows the definition to override the previously specified prototype.

    The reason that's interesting is because in all other cases it would be an error (well, a different error). If you did this:

    Code:
    int function( int x );
    
    int function( char y )
    {
    }
    You'd get an error because the prototype does not match the definition.

    I'm curious whether this behavior is specific to gcc, or something which is prescribed by the standard. It's almost like the prototype with unspecified arguments is "weak".
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    So is it an implementation-specific symptom or is it enforced by the C standard because the C++ compiler (I have) barfs but it's no problemo for the C compiler.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's probably because in C++, if you leave out a parameter list in a prototype, it's exactly like saying (void). In other words, C++ doesn't have the old K&R provision that () means "unspecified parameters" like C does.

    Anyway, you shouldn't be using K&R prototypes if you can absolutely help it.

    [edit] For what it's worth:
    Code:
    $ cat emptyproto.c
    #include <stdio.h>
    
    void function1();
    
    int main()
    {
    
            function1( 'p' );
    
    }
    
    
    void function1( char x )
    {
    
            return;
    
    }
    $ gcc-4.1 -W -Wall -ansi -pedantic emptyproto.c -o emptyproto
    emptyproto.c: In function ‘main’:
    emptyproto.c:10: warning: control reaches end of non-void function
    emptyproto.c: At top level:
    emptyproto.c:14: error: conflicting types for ‘function1’
    emptyproto.c:14: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
    emptyproto.c:3: error: previous declaration of ‘function1’ was here
    emptyproto.c:13: warning: unused parameter ‘x’
    $ gcc-4.2 -W -Wall -ansi -pedantic emptyproto.c -o emptyproto
    emptyproto.c: In function ‘main’:
    emptyproto.c:10: warning: control reaches end of non-void function
    emptyproto.c: At top level:
    emptyproto.c:14: error: conflicting types for ‘function1’
    emptyproto.c:14: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
    emptyproto.c:3: error: previous declaration of ‘function1’ was here
    emptyproto.c:13: warning: unused parameter ‘x’
    $ gcc-4.3 -W -Wall -ansi -pedantic emptyproto.c -o emptyproto
    emptyproto.c: In function ‘main’:
    emptyproto.c:10: warning: control reaches end of non-void function
    emptyproto.c: At top level:
    emptyproto.c:13: error: conflicting types for ‘function1’
    emptyproto.c:14: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
    emptyproto.c:3: error: previous declaration of ‘function1’ was here
    emptyproto.c:13: warning: unused parameter ‘x’
    $ gcc-4.4 -W -Wall -ansi -pedantic emptyproto.c -o emptyproto
    emptyproto.c:13: error: conflicting types for ‘function1’
    emptyproto.c:14: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
    emptyproto.c:3: note: previous declaration of ‘function1’ was here
    emptyproto.c: In function ‘function1’:
    emptyproto.c:13: warning: unused parameter ‘x’
    $
    This is a 64-bit Debian GNU/Linux system. [/edit]
    Last edited by dwks; 06-12-2009 at 02:01 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by itCbitC View Post
    So is it an implementation-specific symptom or is it enforced by the C standard because the C++ compiler (I have) barfs but it's no problemo for the C compiler.
    The C standard explicitly allows a definition to override a () empty parameter list.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tabstop View Post
    The C standard explicitly allows a definition to override a () empty parameter list.
    Well, that makes sense, since you obviously have to be able to specify the actual arguments when defining the function.

    But why does it change the type promotion rules so that the call with a char is not valid? Weird.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM