Are these behaviors defined?

Discussion in 'C Programming' started by William L. Bahn, Aug 7, 2004.

  1. I'm working on some lessons and want to be sure I get some stuff correct. If
    any of this is covered in the FAQ, please feel free to point me to the
    section - I couldn't find it.

    ----------------------------------------------------------------
    Overflow/Wraparound behavior of integer values
    ----------------------------------------------------------------

    If an arithmetic operation such as (+, -, *) yields a result that exceeds
    the bounds, is the behavior in any way defined? I know that, for instance,
    integers will generally wrap around - but is this the defined behavior?

    ----------------------------------------------------------------
    Logical Operators on floating point values
    ----------------------------------------------------------------

    I know that the bitwise operators are not defined for floating point
    representations (though I have no idea why unless it might be to avoid
    imposing behavior that might be more difficult to implement if the values
    involved are in a floating point unit - any thoughts?). My question is the
    logical operators (!, &&, and ||). Are the behaviors of these defined at
    all?

    ----------------------------------------------------------------
    Standard I/O Functions when given NULL stream pointers
    ----------------------------------------------------------------

    Are the behaviors of functions like free(), fprintf(), fclose(), getc() and
    others defined if they are passed a NULL stream pointer?

    ----------------------------------------------------------------
    Value of bits shifted in by >> and <<
    ----------------------------------------------------------------

    Are the values that are shifted into the msb's of an integer when it is
    shifted to the left (and the lsb's when shifted to the right) defined in any
    way?

    ----------------------------------------------------------------
    Are float and double representations fixed?
    ----------------------------------------------------------------

    By this, I mean is a float guaranteed to be a four-byte IEEE-754 single
    precision representation and a double an eight-byte double precision
    representation, or are these only minimum requirements.
     
    William L. Bahn, Aug 7, 2004
    #1
    1. Advertising

  2. William L. Bahn <> scribbled the following:
    > I'm working on some lessons and want to be sure I get some stuff correct. If
    > any of this is covered in the FAQ, please feel free to point me to the
    > section - I couldn't find it.


    > ----------------------------------------------------------------
    > Overflow/Wraparound behavior of integer values
    > ----------------------------------------------------------------


    > If an arithmetic operation such as (+, -, *) yields a result that exceeds
    > the bounds, is the behavior in any way defined? I know that, for instance,
    > integers will generally wrap around - but is this the defined behavior?


    Unsigned integer types will wrap around, this is defined behaviour. The
    behaviour for signed integer types is implementation-defined IIRC.

    > ----------------------------------------------------------------
    > Standard I/O Functions when given NULL stream pointers
    > ----------------------------------------------------------------


    > Are the behaviors of functions like free(), fprintf(), fclose(), getc() and
    > others defined if they are passed a NULL stream pointer?


    free(NULL) is a safe no-op (however passing a stream pointer to free
    causes undefined behaviour). fflush(NULL) flushes all currently open
    output streams. I don't think it is defined for any other function.

    > ----------------------------------------------------------------
    > Are float and double representations fixed?
    > ----------------------------------------------------------------


    > By this, I mean is a float guaranteed to be a four-byte IEEE-754 single
    > precision representation and a double an eight-byte double precision
    > representation, or are these only minimum requirements.


    I don't think it's required to be IEEE-754 at all, but I could be
    mistaken.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "To err is human. To really louse things up takes a computer."
    - Anon
     
    Joona I Palaste, Aug 7, 2004
    #2
    1. Advertising

  3. William L. Bahn

    CBFalconer Guest

    "William L. Bahn" wrote:
    >
    > I'm working on some lessons and want to be sure I get some stuff
    > correct. If any of this is covered in the FAQ, please feel free
    > to point me to the section - I couldn't find it.
    >
    > ----------------------------------------------------------------
    > Overflow/Wraparound behavior of integer values
    > ----------------------------------------------------------------
    >
    > If an arithmetic operation such as (+, -, *) yields a result that
    > exceeds the bounds, is the behavior in any way defined? I know
    > that, for instance, integers will generally wrap around - but is
    > this the defined behavior?


    no, the behavior is specifically undefined. Don't do that.

    Also, please try to keep your line length well below 80, so that
    quotes do not wrap. 65 is a good value to aim for. I have
    reformatted the quotes here.

    >
    > ----------------------------------------------------------------
    > Logical Operators on floating point values
    > ----------------------------------------------------------------
    >
    > I know that the bitwise operators are not defined for floating
    > point representations (though I have no idea why unless it might
    > be to avoid imposing behavior that might be more difficult to
    > implement if the values involved are in a floating point unit
    > - any thoughts?). My question is the logical operators (!, &&,
    > and ||). Are the behaviors of these defined at all?


    No. Because the bit format of floats is not specified.

    >
    > ----------------------------------------------------------------
    > Standard I/O Functions when given NULL stream pointers
    > ----------------------------------------------------------------
    >
    > Are the behaviors of functions like free(), fprintf(), fclose(),
    > getc() and others defined if they are passed a NULL stream
    > pointer?


    free() doesn't receive a stream pointer, and free(NULL) is
    allowable and a no op. The others mentioned, no. In general,
    read the functions specification in the standard. A free version
    is N869 (google for it). Get the text version, which is easily
    searched with grep or an editor to find those definitions.

    >
    > ----------------------------------------------------------------
    > Value of bits shifted in by >> and <<
    > ----------------------------------------------------------------
    >
    > Are the values that are shifted into the msb's of an integer when
    > it is shifted to the left (and the lsb's when shifted to the
    > right) defined in any way?


    for left shift, 0. For right shift, implementation defined.

    >
    > ----------------------------------------------------------------
    > Are float and double representations fixed?
    > ----------------------------------------------------------------
    >
    > By this, I mean is a float guaranteed to be a four-byte IEEE-754
    > single precision representation and a double an eight-byte double
    > precision representation, or are these only minimum requirements.


    Absolutely not. See the standard.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 7, 2004
    #3
  4. William L. Bahn

    Jack Klein Guest

    On Sat, 7 Aug 2004 05:16:04 -0600, "William L. Bahn"
    <> wrote in comp.lang.c:

    > I'm working on some lessons and want to be sure I get some stuff correct. If
    > any of this is covered in the FAQ, please feel free to point me to the
    > section - I couldn't find it.
    >
    > ----------------------------------------------------------------
    > Overflow/Wraparound behavior of integer values
    > ----------------------------------------------------------------
    >
    > If an arithmetic operation such as (+, -, *) yields a result that exceeds
    > the bounds, is the behavior in any way defined? I know that, for instance,
    > integers will generally wrap around - but is this the defined behavior?


    The unsigned integer types cannot overflow. If a calculation produces
    an out-of-range result, it is adjusted by adding or subtracting
    (maximum value of the unsigned type + 1) until the result is within
    range. This is often called 'modulo arithmetic'.

    Overflow of signed integer types produces undefined behavior. Nothing
    at all is guaranteed.

    > ----------------------------------------------------------------
    > Logical Operators on floating point values
    > ----------------------------------------------------------------
    >
    > I know that the bitwise operators are not defined for floating point
    > representations (though I have no idea why unless it might be to avoid
    > imposing behavior that might be more difficult to implement if the values
    > involved are in a floating point unit - any thoughts?). My question is the
    > logical operators (!, &&, and ||). Are the behaviors of these defined at
    > all?


    The logical operators are defined for all legal expressions, other
    than those of type void.

    if(float_var) yields the value 0 if float_var is exactly 0.0 and 1
    otherwise.

    > ----------------------------------------------------------------
    > Standard I/O Functions when given NULL stream pointers
    > ----------------------------------------------------------------
    >
    > Are the behaviors of functions like free(), fprintf(), fclose(), getc() and
    > others defined if they are passed a NULL stream pointer?


    As others have said, free() is not a FILE * function. The others you
    have mentioned produce undefined behavior if passed a null pointer.
    In general, the C standard states that all functions receiving pointer
    arguments have undefined behavior if passed an invalid or null
    pointer, unless specifically documented otherwise.

    > ----------------------------------------------------------------
    > Value of bits shifted in by >> and <<
    > ----------------------------------------------------------------
    >
    > Are the values that are shifted into the msb's of an integer when it is
    > shifted to the left (and the lsb's when shifted to the right) defined in any
    > way?


    For left shifts and right shifts of unsigned integer types and signed
    integer types with positive values, zero bits are shifted in. For
    right shifts of signed integer types with negative values, it is
    implementation-defined whether one or zero bits are shifted in. The
    compiler must document which behavior it uses.


    > ----------------------------------------------------------------
    > Are float and double representations fixed?
    > ----------------------------------------------------------------


    No, they are completely unspecified by the language. It is entirely
    up to the compiler and the underlying hardware platform. IEEE
    representation is common on newer platforms, but not required in any
    way.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 7, 2004
    #4
  5. Thanks to all who have responded. To summarize (please correct any errors):


    ----------------------------------------------------------------
    Overflow/Wraparound behavior of integer values
    ----------------------------------------------------------------

    Defined for integer types with modulo (or 'wraparound') behavior.
    Undefined for signed types.

    ----------------------------------------------------------------
    Logical Operators on floating point values
    ----------------------------------------------------------------

    Got mixed feedback on this one. One said no, because the bit
    pattern is not defined. One said yes, following the same rules as
    other expressions. Since a NULL pointer is regarded as false in
    an expression regardless of its interal representation, either answer
    seems reasonable to me.

    I've downloaded N869 and, by ommision more than anything,
    it appears to support the second claim - that a floating point
    value would evaluate as true just as in the expression (!(0 = = x)).

    Similarly, it looks like the bitwise operators are not undefined for
    floating point values - again, by ommision as it doesn't say that
    they aren't defined.

    But are promotions done on bitwise operands? In other words,
    if I were to create a bitmask designed to maskoff all but the exponent
    of a floating point value (based on knowledge of the implementation
    of a float, of course) and did a bitwise & between it and the float
    in question, would it promote my mask to a floating point representation?

    I can't determine the answer to any of this based on searching N869
    so would really appreciate someone pointing out the sections that
    spell it out.

    ----------------------------------------------------------------
    Standard I/O Functions when given NULL stream pointers
    ----------------------------------------------------------------

    Not defined except for specific functions.

    ----------------------------------------------------------------
    Are float and double representations fixed?
    ----------------------------------------------------------------

    No. Any they are not required to be IEEE-754 at all.

    This answers another question that I had with regards to why the
    FAQ says that setting a block of memory to all zeros is not a safe
    operation if the data in the block is supposed to be either pointers
    or floating point values. The pointers part I understood, but I didn't
    understand the floating point part since all zeros is zero in an
    IEEE 754 representation.
     
    William L. Bahn, Aug 7, 2004
    #5
  6. In article <>,
    "William L. Bahn" <> wrote:

    > Similarly, it looks like the bitwise operators are not undefined for
    > floating point values - again, by ommision as it doesn't say that
    > they aren't defined.


    May I quote: "Some operators (... collectively called the bitwise
    operators) are required to have operands that have integer types.

    Anyway, anything that is ommited is undefined. If it is not defined,
    then it is undefined.
     
    Christian Bau, Aug 7, 2004
    #6
  7. "Christian Bau" <> wrote in message
    news:...
    > In article <>,
    > "William L. Bahn" <> wrote:
    >
    > > Similarly, it looks like the bitwise operators are not undefined for
    > > floating point values - again, by ommision as it doesn't say that
    > > they aren't defined.

    >
    > May I quote: "Some operators (... collectively called the bitwise
    > operators) are required to have operands that have integer types.


    Okay - I found that. Thank you.

    I also see now where each of the operators states a constraint that it
    must be of integer type. Don't know how I missed that before.

    >
    > Anyway, anything that is ommited is undefined. If it is not defined,
    > then it is undefined.


    That's a nice platitude, but it is frequently not very helpful when delving
    through a several hundred page document where so much is defined
    indirectly through several layers of arcane syntax.
     
    William L. Bahn, Aug 8, 2004
    #7
  8. William L. Bahn

    pete Guest

    William L. Bahn wrote:

    > ----------------------------------------------------------------
    > Logical Operators on floating point values
    > ----------------------------------------------------------------
    >
    > Got mixed feedback on this one. One said no, because the bit
    > pattern is not defined.


    His reason suggests that he thought that he was replying to
    "Bitwise Operators on floating point values"

    (!x) is semantically identical to (x == 0).
    If x is a floating type, then those expressions
    are also equivalent to (x == 0.0),
    and (x || 0) is equivalent to (x != 0.0)
     
    pete, Aug 8, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sylvain Munaut
    Replies:
    2
    Views:
    2,167
    Sylvain Munaut
    Dec 22, 2004
  2. VB Programmer
    Replies:
    3
    Views:
    365
    William F. Robertson, Jr.
    Jul 1, 2003
  3. Scupper

    DHTML Behaviors and ASP.NET

    Scupper, May 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    465
    Vidar Petursson
    May 28, 2004
  4. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  5. Mark Kremers

    Behaviors in Behaviors

    Mark Kremers, Jul 31, 2003, in forum: Javascript
    Replies:
    0
    Views:
    119
    Mark Kremers
    Jul 31, 2003
Loading...

Share This Page