473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Are these behaviors defined?

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.


Nov 14 '05 #1
7 1643
William L. Bahn <wi*****@toomuchspam.net> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To err is human. To really louse things up takes a computer."
- Anon
Nov 14 '05 #2
"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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3
On Sat, 7 Aug 2004 05:16:04 -0600, "William L. Bahn"
<wi*****@toomuchspam.net> 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/~a...FAQ-acllc.html
Nov 14 '05 #4
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.



Nov 14 '05 #5
In article <10*************@corp.supernews.com>,
"William L. Bahn" <wi*****@toomuchspam.net> 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.
Nov 14 '05 #6

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <10*************@corp.supernews.com>,
"William L. Bahn" <wi*****@toomuchspam.net> 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.


Nov 14 '05 #7
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)
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Catherine Lynn Wood | last post by:
I have a page that I just developed using a combination of stylesheets and div layers. It uses a 'tab' style system placing four div layers in the same space with visibility 'hidden' and position...
1
by: Harlan Messinger | last post by:
I went to the W3C site to see if behaviors were discussed--something like CSS but for event handlers (onclick, etc.) rather than attributes. They are, at ...
0
by: Luigi | last post by:
I would know what "criteria" is used in box positioning with the position property. Here my tries (using IE and Opera): I have a box with an absolute positioning (properties used: position,...
7
by: sonic | last post by:
Hello, I am cloning a table row which contains images that have behaviors attached to them as well as onclick events. The problem is that the cloned row seems to be executing the...
1
by: Scupper | last post by:
I have been experimenting with adding some of the IE filters to an ASPX page, and have encountered a problem. Strangely, though the page will render in VS's browse window, it will not load in IE...
4
by: David Tilman | last post by:
I've created a web application using ASP .NET that creates tables similar to Gantt charts. There are 5 tables will 180 cells each, so there are about 900 cells on the web page. I had javascript in...
1
by: Mark Denardo | last post by:
I recently upgraded to VS2005 and converted some projects from 1.1 to 2.0 and am seeing two weird behaviors I can't seem to resolve, and am wondering if they are bugs or something I'm forgetting to...
2
by: Matt Nordhoff | last post by:
Sebastjan Trepca wrote: Python doesn't like when you read a variable that exists in an outer scope, then try to assign to it in this scope. (When you do "a = b", "b" is processed first. In...
8
by: Bo Yang | last post by:
Hi, Today, I make some test on the C++ STL iterators of set containers with GNU g++ compiler. The code is: #include <set> #include <iostream> using namespace std; int main(int argc, char...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.