Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #14103

Re: Type interchange

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Type interchange
Date 2011-11-30 17:29 -0500
Organization A noiseless patient Spider
Message-ID <4ED6AE65.8050901@verizon.net> (permalink)
References <jb68ju$icb$1@speranza.aioe.org>

Show all headers | View raw


On 11/30/2011 04:53 PM, Edward Rutherford wrote:
> Hello:
> 
> Can the following program generate an undefined behavior?
> 
> Thank you.
> 
> #include<limits.h>
> int main()
> {
> #if INT_MAX == LONG_MAX
>   long getchar();
> #else
>   int getchar();
> #endif
>   (void) getchar();
>   return 0;
> }

Programs do not generate undefined behavior. "undefined behavior" does
not mean a particular kind of behavior. It means that the behavior is
NOT defined - it can be anything. Code with undefined behavior is
perfectly capable of behaving in precisely the fashion you incorrectly
thought it was required to behave.

Section 7.1.4 says "Provided that a library function can be declared
without reference to any type defined in a header, it is also
permissible to declare the function and use it without including its
associated header.", so the declaration using 'int' is permissible.
However, 6.5.2.2p9 says " If the function is defined with a type that is
not compatible with the type (of the expression) pointed to by the
expression that denotes the called function, the behavior is
undefined."

A function declaration that is not a function prototype, and is used in
a function call expression with no arguments, can be compatible with a
function defined as taking no parameters - but only if the return types
are compatible. Nothing in the standard allows you to assume that 'long'
and 'int' are compatible types, not even if INT_MAX == LONG_MAX. 'int'
could be a one's complement type, while 'long' is a 2's complement type.
'int' could be little-endian, while 'long' could be big-endian. When
used as function arguments, 'int' and 'long' values could be passed in
different registers.

None of those possibilities is very likely. In all probability, your
code will work fine, particularly since it discards the return value,
unused. However, the fact that it will probably work is NOT guaranteed
by the standard, the correct wording to describe that absence of a
guarantee is "the behavior of your program is undefined".

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Type interchange Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-11-30 21:53 +0000
  Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-11-30 17:29 -0500
    Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-11-30 15:22 -0800
      Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-11-30 19:10 -0500
        Re: Type interchange Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-01 03:09 +0000
          Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-01 07:13 -0500
            Re: Type interchange Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-01 15:55 +0000
              Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-01 11:28 -0500
                Re: Type interchange Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-01 16:45 +0000
                Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-01 12:00 -0500
                Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-12-01 14:31 -0800
                Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-01 20:18 -0500
                Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-12-01 17:35 -0800
                Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-02 12:03 -0500
                Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-12-02 13:49 -0800
                Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-02 22:12 -0500
                Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-12-03 02:01 -0800
                Re: Type interchange Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 12:47 -0800
            Re: Type interchange pete <pfiland@mindspring.com> - 2011-12-01 11:10 -0500
              Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-01 11:34 -0500
              Re: Type interchange Kaz Kylheku <kaz@kylheku.com> - 2011-12-01 17:48 +0000
              Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-12-01 14:17 -0800
                Re: Type interchange pete <pfiland@mindspring.com> - 2011-12-01 17:43 -0500
                Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-12-01 15:00 -0800
                Re: Type interchange pete <pfiland@mindspring.com> - 2011-12-01 19:29 -0500
                Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-12-01 16:59 -0800
                Re: Type interchange pete <pfiland@mindspring.com> - 2011-12-02 08:35 -0500
                Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-02 08:46 -0500
                Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-12-02 13:32 -0800
                Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-12-02 13:30 -0800
                Re: Type interchange pete <pfiland@mindspring.com> - 2011-12-02 18:45 -0500
        Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-11-30 19:17 -0800
          Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-01 06:50 -0500
            Re: Type interchange Keith Thompson <kst-u@mib.org> - 2011-12-01 07:15 -0800
              Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-01 10:36 -0500
                Re: Type interchange pete <pfiland@mindspring.com> - 2011-12-01 11:04 -0500
                Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-01 11:37 -0500
                Re: Type interchange Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 13:02 -0800
            Re: Type interchange Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 13:01 -0800
    Re: Type interchange Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-12-02 22:15 +0000
      Re: Type interchange James Kuyper <jameskuyper@verizon.net> - 2011-12-03 11:39 -0500

csiph-web