Re: Why oh why does this NOT give a compile error?



Paul Melis wrote:
Can someone explain to me why the following code compiles without
errors on gcc 4.0.2?

void f()
{
}

void t()
{
f(1,2,3);
f("1");
}

I would expect at least some warning, but not even that. Is this a
feature of the newest C dialect or something, that you can provide
arbitrary arguments to a function having no arguments?

No, it's a feature of the C standard that this code has undefined behavior, but is neither a syntax error nor a constraint violation, and therefore does not require a diagnostic message. A good compiler might provide one. The more fundamental problem is that you should be using function prototypes, in which case the corresponding code would require a diagnostic message.

What compiler options are you using? With the right compiler options, gcc can be a good compiler, though what it produces is a warning about the more fundamental problem, rather than the one you're actually asking about:


cc -std=c99 -pedantic -Wall -Wpointer-arith -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -c -o ft.o ft.c
ft.c:2: warning: function declaration isn’t a prototype
ft.c:6: warning: function declaration isn’t a prototype
.



Relevant Pages

  • Re: [PATCH] security_sk_free void return fixup
    ... Apparently it is valid C99, ... It may work in gcc, but it is invalid according to ISO C99. ... function whose return type is void. ... return.c:5: warning: `return' with a value, ...
    (Linux-Kernel)
  • Re: casting away constness
    ... What's wrong with the "brute force" solution? ... may run into a warning about a violation of the aliasing rules. ... compiler options I normally use, ... It's just in a way that gcc doesn't warn about. ...
    (comp.lang.c)
  • Re: Why oh why does this NOT give a compile error?
    ... void f ... An empty parameter list in a function declaration tells the compiler ... Also, I would have expected GCC to be smart enough to realize that fand fwere incompatible signatures, since fwasn't declared as taking varargs, and give a warning on the second call. ...
    (comp.lang.c)
  • gcc not compiling
    ... I'm just getting started in programming C. ... void main { ... hello.c:3: warning: incompatible implicit declaration of built-in ... in the gcc manual. ...
    (Fedora)
  • Re: [PATCH]: warrning fix: unsigned->signed
    ... The patch seems OK, but I'm curious to know why you're seeing this warning ... Are you using a compiler other than gcc? ... Did you add any new compiler options? ...
    (Linux-Kernel)