Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is this program legal C89?

Reply
Thread Tools

Is this program legal C89?

 
 
Army1987
Guest
Posts: n/a
 
      07-24-2007
Is this program legal C89?

/* no headers included */
int main(void)
{
if (sizeof (exit(0), 0),
((void (* )(int))&exit)(
(puts((const char *)"hello, world"), 0)
),
0) {
}
}

The the first operand of the first comma serves to implicitly
declare exit() as returning an int, but it is never called as
such. Does that wrong implicit declaration cause UB by itself,
even if the function is eventually called with the right type?

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-24-2007
Army1987 wrote On 07/24/07 07:01,:
> Is this program legal C89?


Legal. Repellent, but legal.

> /* no headers included */
> int main(void)
> {
> if (sizeof (exit(0), 0),
> ((void (* )(int))&exit)(
> (puts((const char *)"hello, world"), 0)
> ),
> 0) {
> }
> }
>
> The the first operand of the first comma serves to implicitly
> declare exit() as returning an int, but it is never called as
> such. Does that wrong implicit declaration cause UB by itself,
> even if the function is eventually called with the right type?


The type of the function pointer used at the call
site matches the type of the called function: it's legal.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a
 
      07-24-2007
Army1987 wrote:
> Is this program legal C89?
>
> /* no headers included */
> int main(void)
> {
> if (sizeof (exit(0), 0),
> ((void (* )(int))&exit)(
> (puts((const char *)"hello, world"), 0)
> ),
> 0) {
> }
> }
>
> The the first operand of the first comma serves to implicitly
> declare exit() as returning an int, but it is never called as
> such. Does that wrong implicit declaration cause UB by itself,
> even if the function is eventually called with the right type?


The only relevant paragraph in a C89 draft I'm looking at is
3.1.2.6 Compatible type and composite type
"All declarations that refer to the same object or function shall have
compatible type; otherwise the behavior is undefined."
Considering the implementation of the standard library contains a definition
(which is a declaration) of exit(), and your program's implicit declaration
is not compatible with it, I believe the behaviour is undefined, but
whether 3.1.2.6 is meant to apply to standard library functions is not
entirely clear. C99 clears it up with an explicit prohibition against
incorrect declarations of standard library functions:
7.1.3p2:
"No other identifiers are reserved. If the program declares or defines an
identifier in a context in which it is reserved (other than as allowed by
7.1.4), or defines a reserved identifier as a macro name, the behavior is
undefined."
so if you make the implicit declarations explicit, it's still definitely
invalid C99, but I have been told this text is not present in C89.
 
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
Research: File-sharers big legal download spenders. Silverstrand Front Page News 0 07-27-2005 03:00 PM
State machine transition on internal signals - is it legal? Divyang M VHDL 9 05-18-2005 03:58 PM
State machine transition on internal signals -- is it legal? Divyang M VHDL 1 05-15-2005 09:36 AM
Is this legal? Valentin Tihomirov VHDL 20 10-29-2003 10:31 AM



Advertisments