public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* implicit void arguments not checked?
@ 2006-07-13 12:06 Florin Iucha
  2006-07-13 15:48 ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Florin Iucha @ 2006-07-13 12:06 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1745 bytes --]

Hello,

I have the following three files:

greet.h

   ---cut-here---cut-here---cut-here
   #ifndef __GREET_H__
   #define __GREET_H__

   // greet is not declared to take any arguments
   void greet();

   #endif // __GREET_H__
   ---cut-here---cut-here---cut-here

greet.c

   ---cut-here---cut-here---cut-here
   #include <stdio.h>

   void greet()
   {
         printf("Hello, Anybody!\n");
   }
   ---cut-here---cut-here---cut-here

hello.c

   ---cut-here---cut-here---cut-here
   #include "greet.h"

   int main()
   {
         // call greet with an argument
         greet("World!\n");
         return 0;
   }
   ---cut-here---cut-here---cut-here

Using gcc-4.1.1 on Debian testing/unstable:

   $ gcc greet.c hello.c
   # no error or warning

   $ gcc -Wall greet.c hello.c
   # no error or warning

   $ gcc -Wall -pedantic greet.c hello.c
   In file included from hello.c:1:
   greet.h:6:8: warning: C++ style comments are not allowed in ISO C90
   greet.h:6:8: warning: (this will be reported only once per input file)

   $ gcc -Wall -pedantic -ansi greet.c hello.c
   In file included from hello.c:1:
   greet.h:6:8: warning: extra tokens at end of #endif directive

If I change the greet declaration to "void greet(void)" I get a 
compilation error error. But it is puzzling I do not get one by
default.

1. Shouldn't gcc at least warn about greet being called with and argument
that is not present in the declaration?

2. What -W option should I use to get the warning (if it is not by
default in Wall)?

florin

-- 
If we wish to count lines of code, we should not regard them as lines
produced but as lines spent.                       -- Edsger Dijkstra

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: implicit void arguments not checked?
  2006-07-13 12:06 implicit void arguments not checked? Florin Iucha
@ 2006-07-13 15:48 ` Ian Lance Taylor
  2006-07-13 17:45   ` Florin Iucha
  0 siblings, 1 reply; 4+ messages in thread
From: Ian Lance Taylor @ 2006-07-13 15:48 UTC (permalink / raw)
  To: Florin Iucha; +Cc: gcc-help

florin@iucha.net (Florin Iucha) writes:

> 1. Shouldn't gcc at least warn about greet being called with and argument
> that is not present in the declaration?

No.  In C, "void fn()" means that you are not saying anything about
the arguments accepted by the function.  As you mentioned, the way to
say that a function takes no arguments is "void fn(void)".

> 2. What -W option should I use to get the warning (if it is not by
> default in Wall)?

-Wstrict-prototypes

Ian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: implicit void arguments not checked?
  2006-07-13 15:48 ` Ian Lance Taylor
@ 2006-07-13 17:45   ` Florin Iucha
  2006-07-13 17:53     ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Florin Iucha @ 2006-07-13 17:45 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1282 bytes --]

On Thu, Jul 13, 2006 at 08:48:23AM -0700, Ian Lance Taylor wrote:
> florin@iucha.net (Florin Iucha) writes:
> 
> > 1. Shouldn't gcc at least warn about greet being called with and argument
> > that is not present in the declaration?
> 
> No.  In C, "void fn()" means that you are not saying anything about
> the arguments accepted by the function.  As you mentioned, the way to
> say that a function takes no arguments is "void fn(void)".

I knew that from K&R, but I was expecting ANSI C90 and C99 to tighten
that up a bit. Otherwise, what would be the point of having function 
declaration at all? Only for the return type?

Do you have a hard reference (to the standard)?

> > 2. What -W option should I use to get the warning (if it is not by
> > default in Wall)?
> 
> -Wstrict-prototypes

   $ gcc -Wstrict-prototypes *.c
   greet.c:4: warning: function declaration isn’t a prototype
   In file included from hello.c:1:
   greet.h:4: warning: function declaration isn’t a prototype
   hello.c:4: warning: function declaration isn’t a prototype

It is complaining about everything ;(

florin

-- 
If we wish to count lines of code, we should not regard them as lines
produced but as lines spent.                       -- Edsger Dijkstra

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: implicit void arguments not checked?
  2006-07-13 17:45   ` Florin Iucha
@ 2006-07-13 17:53     ` Ian Lance Taylor
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2006-07-13 17:53 UTC (permalink / raw)
  To: Florin Iucha; +Cc: gcc-help

florin@iucha.net (Florin Iucha) writes:

> > No.  In C, "void fn()" means that you are not saying anything about
> > the arguments accepted by the function.  As you mentioned, the way to
> > say that a function takes no arguments is "void fn(void)".
> 
> I knew that from K&R, but I was expecting ANSI C90 and C99 to tighten
> that up a bit. Otherwise, what would be the point of having function 
> declaration at all? Only for the return type?

C90 introduced function prototypes, but they had to be backward
compatible.  So they decided that fn() says nothing about argument
types.  Otherwise they would have broken all existing K&R C code.

> Do you have a hard reference (to the standard)?

C99 6.7.5.3 paragraph 14:

    An identifier list declares only the identifiers of the parameters
    of the function.  An empty list in a function declarator that is
    part of a definition of that function specifies that the function
    has no parameters.  The empty list in a function declarator that
    is not part of a definition of that function specifies that no
    information about the number or types of the parameters is
    supplied.(124)

Footnote 124:

    See ``future language directions'' (6.11.6).

6.11.6:

    The use of function declarators with empty parentheses (not
    prototype-format parameter type declarators) is an obsolescent
    feature.

Ian

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-07-13 17:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-13 12:06 implicit void arguments not checked? Florin Iucha
2006-07-13 15:48 ` Ian Lance Taylor
2006-07-13 17:45   ` Florin Iucha
2006-07-13 17:53     ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).