NO

Author Topic: prototype warning on a declaration, 4.0 beta  (Read 4638 times)

kenr

  • Guest
prototype warning on a declaration, 4.0 beta
« on: August 12, 2005, 05:32:04 AM »
4.0 beta is warning on no arguments in a function declaration.  A function prototype requires the word "void,"  but isn't an empty list still ok for a declaration?
 
int foo(void);   // prototype ok, no warning

int foo()   // declaration ok too, but get warning #2117
  {
  return 1;
  }

int main()   // no warning here
  {
  return foo();
  }

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
prototype warning on a declaration, 4.0 beta
« Reply #1 on: August 12, 2005, 08:03:15 PM »
I moved this warning from level 2 to level 1 (in 4.00) because there seem to be some confusion about this:

in C++ func() means a function taking no arguments, but in C it means a function taking any number of arguments. The preferred way in C is to always use prototypes, maybe using ... for a variable number of arguments - so I think the warning is OK.

Pelle
/Pelle

kenr

  • Guest
prototype warning on a declaration, 4.0 beta
« Reply #2 on: August 13, 2005, 02:09:52 AM »
The word "void" is required in a function prototype to indicate the function has no parameters.  However, an empty list in a function declarator which is part of a function declaration specifies that the function has no parameters.  Style standards might suggest that "void" be used in both cases, but the language standard doesn't require it.

Offline Robert

  • Member
  • *
  • Posts: 247
Empty function prototypes are a deprecated feature in C99
« Reply #3 on: August 13, 2005, 03:47:53 AM »
Quote from: "kenr"
The word "void" is required in a function prototype to indicate the function has no parameters.  However, an empty list in a function declarator which is part of a function declaration specifies that the function has no parameters.


Now I'm confused. I thought that in C a function declared with an empty parameter list is an unprototyped function taking an unspecified number of arguments.

Quote from: "kenr"

Style standards might suggest that "void" be used in both cases, but the language standard doesn't require it.


In C a function declared with a parameter list consisting of only void
is a prototyped function taking no arguments.

See

http://david.tribble.com/text/cdiffs.htm

which in part states,

Empty parameter lists
C distinguishes between a function declared with an empty parameter list and a function declared with a parameter list consisting of only void. The former is an unprototyped function taking an unspecified number of arguments, while the latter is a prototyped function taking no arguments.

    // C code

    extern int  foo();          // Unspecified parameters
    extern int  bar(void);      // No parameters

    void baz()
    {
        foo(0);         // Valid C, invalid C++
        foo(1, 2);      // Valid C, invalid C++

        bar();          // Okay in both C and C++
        bar(1);         // Error in both C and C++
    }
C++, on the other hand, makes no distinction between the two declarations and considers them both to mean a function taking no arguments.

    // C++ code

    extern int  xyz();

    extern int  xyz(void);  // Same as 'xyz()' in C++,
                            // Different and invalid in C
For code that is intended to be compiled as either C or C++, the best solution to this problem is to always declare functions taking no parameters with an explicit void prototype. For example:

    // Compiles as both C and C++
    int bosho(void)
    {
        ...
    }
Empty function prototypes are a deprecated feature in C99 (as they were in C89).

[C99: §6.7.5.3]
[C++98: §8.3.5, C.1.6.8.3.5]

Robert Wishlaw

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
prototype warning on a declaration, 4.0 beta
« Reply #4 on: August 13, 2005, 06:47:05 PM »
Quote from: "kenr"
The word "void" is required in a function prototype to indicate the function has no parameters.  However, an empty list in a function declarator which is part of a function declaration specifies that the function has no parameters.  Style standards might suggest that "void" be used in both cases, but the language standard doesn't require it.

Yes. It also says in "Future language directions" that "The use of function declarators with empty parentheses (not prototype-format parameter
type declarators) is an obsolescent feature." - but a level one warning is perhaps a bit strong here. I will revert back to a level 2 warning - will be in the next beta version.

Robert:
Code: [Select]

... func(void);  /* 1) "prototype" */

...

...func() {  /* 2) "definition" */
    ...
}

Pelles C 4.0 will give a strong warning for 2) even with 1) present - which is perhaps not entirely correct.

Pelle
/Pelle