Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why oh why does this NOT give a compile error?

Reply
Thread Tools

Why oh why does this NOT give a compile error?

 
 
Paul Melis
Guest
Posts: n/a
 
      11-05-2007
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?

Paul

And yes, I'm compiling the right file:

12:58|paul@tabu:/tmp> gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=release --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++,objc,java,f95,ada --enable-java-awt=gtk --with-java-
home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --host=i386-redhat-linux
Thread model: posix
gcc version 4.0.2 20051125 (Red Hat 4.0.2-
12:58|paul@tabu:/tmp> cat t.c
void f()
{
}

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

int
main()
{
t();

return 0;
}
12:58|paul@tabu:/tmp> gcc -o t -W -Wall t.c
12:58|paul@tabu:/tmp> ls -l t
-rwxrwxr-x 1 paul paul 4659 Nov 5 12:58 t

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-05-2007
On Monday 05 Nov 2007 5:32 pm Paul Melis <(E-Mail Removed)> wrote in
article <(E-Mail Removed). com>:

> 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?


It's a feature of "K&R" C, i.e., a pre-Standard dialect of C. For
backwards compatibility it is still legal though not recommended.

An empty parameter list in a function declaration tells the compiler
that the function takes an unspecified number and type of arguments and
turns of parameter cross-checking of invocations against the
declaration.

To specify that a function accepts no arguments put the void keyword
between the parameter list.

void f(void) { /* ... */ }

<snip>

 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      11-05-2007
"santosh" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fgn135$vud$(E-Mail Removed)...
> On Monday 05 Nov 2007 5:32 pm Paul Melis <(E-Mail Removed)> wrote in
> article <(E-Mail Removed). com>:
>
>> 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?

>
> It's a feature of "K&R" C, i.e., a pre-Standard dialect of C. For
> backwards compatibility it is still legal though not recommended.

And therefore I'd expect at least a warning. And on my compiler I get one

warning(229): too many arguments in function call

Bye, Jojo


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-05-2007
On Monday 05 Nov 2007 5:45 pm Joachim Schmitz <(E-Mail Removed)>
wrote in article <fgn1h1$ng3$(E-Mail Removed)>:

> "santosh" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:fgn135$vud$(E-Mail Removed)...
>> On Monday 05 Nov 2007 5:32 pm Paul Melis <(E-Mail Removed)> wrote
>> in article <(E-Mail Removed). com>:
>>
>>> 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?

>>
>> It's a feature of "K&R" C, i.e., a pre-Standard dialect of C. For
>> backwards compatibility it is still legal though not recommended.

> And therefore I'd expect at least a warning. And on my compiler I get
> one
>
> warning(229): too many arguments in function call


gcc needs either '-Wmissing-prototypes' or '-Wold-style-definition'
switches to produce a diagnostic. These are not included even
in '-Wall' and '-W'.

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-05-2007
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
 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      11-05-2007

"santosh" <(E-Mail Removed)> wrote in message
news:fgn135$vud$(E-Mail Removed)...
> On Monday 05 Nov 2007 5:32 pm Paul Melis <(E-Mail Removed)> wrote in
> article <(E-Mail Removed). com>:
>
>> 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?

>
> It's a feature of "K&R" C, i.e., a pre-Standard dialect of C. For
> backwards compatibility it is still legal though not recommended.
>
> An empty parameter list in a function declaration tells the compiler
> that the function takes an unspecified number and type of arguments and
> turns of parameter cross-checking of invocations against the
> declaration.
>


I guess a lot depends on if the compiler will still accept K&R style code
(mine will not, nor will it accept some aspects of C90 syntax, such as
'implicit int', being tolerant of missing prototypes, ...).

of course, I am not sure whether one can view newer C dialects as infact
proper supersets of the old dialects (say, C99 also implies full support of
C90), or if the view is more that C in itself is actually a moving target
(would seem at least implied), in truth several different languages that
happen to be closly enough related that as a general rule code works between
them without problem.

or such...


> To specify that a function accepts no arguments put the void keyword
> between the parameter list.
>
> void f(void) { /* ... */ }
>
> <snip>
>



 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-05-2007
cr88192 wrote:
> "santosh" <(E-Mail Removed)> wrote in message
> news:fgn135$vud$(E-Mail Removed)...
>> On Monday 05 Nov 2007 5:32 pm Paul Melis <(E-Mail Removed)> wrote in
>> article <(E-Mail Removed). com>:
>>
>>> 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?

>> It's a feature of "K&R" C, i.e., a pre-Standard dialect of C. For
>> backwards compatibility it is still legal though not recommended.
>>
>> An empty parameter list in a function declaration tells the compiler
>> that the function takes an unspecified number and type of arguments and
>> turns of parameter cross-checking of invocations against the
>> declaration.
>>

>
> I guess a lot depends on if the compiler will still accept K&R style code
> (mine will not, nor will it accept some aspects of C90 syntax, such as
> 'implicit int', being tolerant of missing prototypes, ...).


A conforming implementation of C99 is required to accept functions
without prototypes. But then, you've made it clear that conformance to
any particular standard is not a priority for you.

> of course, I am not sure whether one can view newer C dialects as infact
> proper supersets of the old dialects (say, C99 also implies full support of
> C90), or if the view is more that C in itself is actually a moving target
> (would seem at least implied), in truth several different languages that
> happen to be closly enough related that as a general rule code works between
> them without problem.


C99 is almost completely backwards compatible with C90, and I don't
think this program is one of the exceptions to that rule (I can't be
certain, I don't have a copy of the C90 standard).
 
Reply With Quote
 
ups_genius@gmx.net
Guest
Posts: n/a
 
      11-05-2007
On 5 Nov., 13:38, James Kuyper <(E-Mail Removed)> wrote:
> 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- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -



> 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.


Exactly!
Things that are "correct" in C might still not always be the best
solution.

Think about the following:

(array[i] == i[array]) will return (!0)
array[i] is the same as i[array], and it is perfectly correct in C.
But you still would not want to use it.

Why?
array[i] == *(array+i) == *(i+array) == i[array]

Christian

 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      11-05-2007
"santosh" <(E-Mail Removed)> wrote in message
news:fgn135$vud$(E-Mail Removed)...
> On Monday 05 Nov 2007 5:32 pm Paul Melis <(E-Mail Removed)> wrote in
> article <(E-Mail Removed). com>:
>
>> 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?

>
> It's a feature of "K&R" C, i.e., a pre-Standard dialect of C. For
> backwards compatibility it is still legal though not recommended.
>
> An empty parameter list in a function declaration tells the compiler
> that the function takes an unspecified number and type of
> arguments and turns of parameter cross-checking of invocations
> against the declaration.


That is correct, but he provided a function _definition_, not just a
declaration.

> To specify that a function accepts no arguments put the void keyword
> between the parameter list.
>
> void f(void) { /* ... */ }


I thought that the "void" in the _definition_ was unnecessary; it's only
necessary in a pure _declaration_. (Note: answer differs from C++)

I would have expected the above behavior if he'd just provided:

void f();

Also, I would have expected GCC to be smart enough to realize that f(1,2,3)
and f("1") were incompatible signatures, since f() wasn't declared as taking
varargs, and give a warning on the second call.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-05-2007
Stephen Sprunk wrote, On 05/11/07 15:19:
> "santosh" <(E-Mail Removed)> wrote in message


<snip>

>> To specify that a function accepts no arguments put the void keyword
>> between the parameter list.
>>
>> void f(void) { /* ... */ }

>
> I thought that the "void" in the _definition_ was unnecessary; it's only
> necessary in a pure _declaration_. (Note: answer differs from C++)


This has been argued to death both here and in comp.lang.c. Although
"void(f() {}" defined a function that takes no parameters it does not
provide a prototype so the compiler is not required to diagnose
parameters being passed to it. It is, of course, *allowed* to diagnose it.

> I would have expected the above behavior if he'd just provided:
>
> void f();
>
> Also, I would have expected GCC to be smart enough to realize that
> f(1,2,3) and f("1") were incompatible signatures, since f() wasn't
> declared as taking varargs, and give a warning on the second call.


I would have said it was better for a compiler to diagnose it as well,
but it is not a bug as such as it is not required to.
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why wouldn't this line of give give the address of the pointer? grocery_stocker C Programming 10 05-25-2005 03:05 AM
Give us 3 minutes; we give you the whole library lib Computer Support 1 02-04-2005 03:16 AM
Give us 3 minutes; we give you the whole library lib Computer Support 0 01-27-2005 07:52 AM



Advertisments