473,397 Members | 2,084 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,397 software developers and data experts.

integer and floating-point bit pattern

Would the following determine if the bit pattern for
floating point 0.0 and integer 0 are the same?

#include <stdio.h>
int main(void)
<%
if(0 == 0.0f)
puts("Bit pattern is the same.");

return 0;
%>
Also is it undefined behavior for failure
to include the proper header file for any
function or just functions that accept a
variable number of arguments?
Nov 13 '05 #1
5 2990
"Mantorok Redgormor" <ne*****@tokyo.com> wrote in message
news:41**************************@posting.google.c om...
Would the following determine if the bit pattern for
floating point 0.0 and integer 0 are the same?
No.
#include <stdio.h>
int main(void)
<%
if(0 == 0.0f)
puts("Bit pattern is the same.");
puts ("VALUE is the same.");

return 0;
%>
Use a pointer to unsigned char ('unsigned char*')
to inspect individual bytes of other types.

#include <stdio.h>
#include <string.h>

int main(void)
{
int i = 0;
float f = 0;
unsigned char *pi = (unsigned char*)&i;
unsigned char *pf = (unsigned char*)&f;
int eq_size = sizeof i == sizeof f;

if(eq_size && !memcmp(pi, pf, sizeof i))
puts("match");
else
puts("no match");

return 0;
}
Also is it undefined behavior for failure
to include the proper header file for any
function or just functions that accept a
variable number of arguments?


For C99 I believe it's a constraint violation.
I don't think it's UB in either case.
Just #include the necessary header, and forget it.

-Mike
Nov 13 '05 #2
On 1 Oct 2003 13:56:10 -0700, ne*****@tokyo.com (Mantorok Redgormor)
wrote in comp.lang.c:
Would the following determine if the bit pattern for
floating point 0.0 and integer 0 are the same?

#include <stdio.h>
int main(void)
<%
if(0 == 0.0f)
puts("Bit pattern is the same.");

return 0;
%>
Also is it undefined behavior for failure
to include the proper header file for any
function or just functions that accept a
variable number of arguments?


Mike already answered your first question, he was a little unsure
about the second.

It is not and has never been a requirement in C that you include
header files, except in a few cases where data types with
implementation-defined types and members (such as time_t, FILE, etc.).

Prior to the current (1999) standard you did not need a prototype or a
declaration to call a function that returned int and accepted a fixed
number and type of arguments, if all the argument types were those
provided by default promotions.

Under the current standard, there must be at least a declaration in
scope for any function you call, specifying the return type. Implicit
int is illegal, including the return type of functions. The
declaration does not need to be a prototype if the function accepts a
fixed number and type of arguments if the types are the default
promotions.

Since the very first 1989 ANSI standard, it produces undefined
behavior to call a variadic function without a full prototype in
scope.

It is perfectly valid under all versions of the C standard to provide
your own prototype for a standard function so long as you get it
correct.

If you write a prototype equivalent to:

char *strchr(const char *s, int c);

....then you can call this function without including <string.h>.

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #3
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:ds********************************@4ax.com...
Also is it undefined behavior for failure
to include the proper header file for any
function or just functions that accept a
variable number of arguments?


Mike already answered your first question, he was a little unsure
about the second.


And now I'm not. :-)

[snip good explanation]

Thanks, Jack.

-Mike
Nov 13 '05 #4
Jack Klein <ja*******@spamcop.net> wrote in message news:<ds********************************@4ax.com>. ..
On 1 Oct 2003 13:56:10 -0700, ne*****@tokyo.com (Mantorok Redgormor)
wrote in comp.lang.c:
Would the following determine if the bit pattern for
floating point 0.0 and integer 0 are the same?

#include <stdio.h>
int main(void)
<%
if(0 == 0.0f)
puts("Bit pattern is the same.");

return 0;
%>
Also is it undefined behavior for failure
to include the proper header file for any
function or just functions that accept a
variable number of arguments?


Mike already answered your first question, he was a little unsure
about the second.

It is not and has never been a requirement in C that you include
header files, except in a few cases where data types with
implementation-defined types and members (such as time_t, FILE, etc.).

Prior to the current (1999) standard you did not need a prototype or a
declaration to call a function that returned int and accepted a fixed
number and type of arguments, if all the argument types were those
provided by default promotions.

Under the current standard, there must be at least a declaration in
scope for any function you call, specifying the return type. Implicit
int is illegal, including the return type of functions. The
declaration does not need to be a prototype if the function accepts a
fixed number and type of arguments if the types are the default
promotions.

Since the very first 1989 ANSI standard, it produces undefined
behavior to call a variadic function without a full prototype in
scope.

It is perfectly valid under all versions of the C standard to provide
your own prototype for a standard function so long as you get it
correct.

If you write a prototype equivalent to:

char *strchr(const char *s, int c);

...then you can call this function without including <string.h>.


Thanks for the response.

I'm puzzled about one thing though. Why is unsigned char *
the preferred type for handling underlying representations
of types?
Nov 13 '05 #5

"Mantorok Redgormor" <ne*****@tokyo.com> wrote in message
news:41**************************@posting.google.c om...
I'm puzzled about one thing though. Why is unsigned char *
the preferred type for handling underlying representations
of types?


In simplest terms:

Because the language specification guarantees that it will work.
No such guarantee is made for any other type.

-Mike
Nov 13 '05 #6

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

Similar topics

1
by: George Hester | last post by:
At the time this suggestion was made I didn't have the wherewithall to even attempt it. But over time I learned enough to make a stab at it. Let just say the foating DIV had to provide the same...
8
by: SAN CAZIANO | last post by:
i have to do in the onkeypress or in onchange the float (real) field validation I try something: function ValidaCampo(nomeCampo,TotInteri,TotDecimali) {...
1
by: Joe | last post by:
Hi all, I have a linux c source code that stores a float value into a type int on many occasions. Is this possible in linux or does compiler round the float value into integral type? The strange...
19
by: shanx__=|;- | last post by:
hi i need some help regarding use of very very long integer datatype in 'c'.. i need it to store result of large number's factorial.. if someone can healp it would be a delight..
2
by: Qiang | last post by:
Those who have used Google notebook may notice that google notebook displays the notes in a small floating window of the browser. I have tried to create a similar floating window, but with no luck....
9
by: PengYu.UT | last post by:
Hi, The usually integer division will round the result to the biggest integet smaller than the float version division.For example, 10/3 = 3. I'm wondering if there is any easy way to round it...
7
by: Jo Deni | last post by:
Folks, I'm a newbie here (and with C++). While executing a C++ program I'm getting the following message error: integer constant is too large for "long" type and here is the offending line...
0
by: gdrenfrew | last post by:
I'd like to know if it is possible to stop the parent application of a floating form becoming active when the controls on the floating form are clicked? I've got a large application, with multiple...
6
by: Jeremy | last post by:
I've got a floating div which becomes visible when a link is clicked. I want the div to be hidden when the user clicks anywhere on the page except for whithin the div. What is the best way to do...
5
by: Rex Mottram | last post by:
Can anyone explain this? % cat t.c #include <stdio.h> #define FIRST "first" #define SECOND "second" int main(int argc, char *argv)
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.