473,664 Members | 3,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how the following code works?

main() {
float a = 5.375;
char *p;
int i;
p = (char*)&a;
for(i=0;i<=3;i+ +)
printf("%02x",( unsigned char)p);
}

the above code gives the binary representation of a.
how does it work?

Jun 16 '07 #1
16 1700
In article <11************ *********@e9g20 00prf.googlegro ups.com>,
Ravi <ra*********@gm ail.comwrote:
>main() {
float a = 5.375;
char *p;
int i;
p = (char*)&a;
for(i=0;i<=3;i ++)
printf("%02x", (unsigned char)p);
}

the above code gives the binary representation of a.
how does it work?
It doesn't.
dave
(it appears to be an attempt to examine the bytes representing an object
of floating-point types, but contains at least one obvious error, one
subtle error, and one gratuitious assumption.)

--
Dave Vandervies dj******@csclub .uwaterloo.ca
P.J. Plauger has a valid reason [...] although I would say that was
more to do with his customers being less than sensible.
--Flash Gordon in comp.lang.c
Jun 16 '07 #2
Ravi wrote:
>
main() {
float a = 5.375;
char *p;
int i;
p = (char*)&a;
for(i=0;i<=3;i+ +)
printf("%02x",( unsigned char)p);
}

the above code gives the binary representation of a.
how does it work?
It doesn't work at all.

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
float a = 5.375;
unsigned char *p;
size_t i;

p = (unsigned char *)&a;
for (i = 0; i != sizeof a; ++i) {
printf("%02x ", p[i]);
}
putchar('\n');
return 0;
}

/* END new.c */
--
pete
Jun 16 '07 #3

"Ravi" <ra*********@gm ail.comwrote in message
news:11******** *************@e 9g2000prf.googl egroups.com...
main() {
float a = 5.375;
char *p;
int i;
p = (char*)&a;
for(i=0;i<=3;i+ +)
printf("%02x",( unsigned char)p);
}

the above code gives the binary representation of a.
how does it work?
Let's rewrite slightly more professionally.

#include <stdio.h>

void dump(void *bytes, int N)
{
int i;
unsigned char *cbytes = bytes;

for(i=0;i<N;i++ )
printf("%02x", cbytes[i]);
printf("\n");
}

int main(void)
{
float a = 123.567;

dump(&a, sizeof(float));
return 0;
}

Now you ought to see how the program works. Plus you have a handy little
routine you can cut and paste any time you need to examine an object's
binary representation.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 16 '07 #4
On Jun 16, 10:38 am, Ravi <ra.ravi....@gm ail.comwrote:
main() {
float a = 5.375;
char *p;
int i;
p = (char*)&a;
for(i=0;i<=3;i+ +)
printf("%02x",( unsigned char)p);

}
oops, you had taken liberty over the size of float!
>printf("%02x", (unsigned char)p);
Never know what you are trying to do by this statement.
>
the above code gives the binary representation of a.
No, it doesn't!
Moreover, even if you correct all the mistakes, I don't think you will
get the binary representation, unless CHAR_BIT==1,in which case your
implementation is non-conforming!

You will get only "byte representation" .
how does it work?
It doesn't work at all.
Jun 16 '07 #5
Ravi said:
main() {
float a = 5.375;
char *p;
int i;
p = (char*)&a;
for(i=0;i<=3;i+ +)
printf("%02x",( unsigned char)p);
}

the above code gives the binary representation of a.
how does it work?
It doesn't.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 16 '07 #6

"pete" <pf*****@mindsp ring.comha scritto nel messaggio
news:46******** **@mindspring.c om...
Ravi wrote:
>>
main() {
float a = 5.375;
char *p;
int i;
p = (char*)&a;
for(i=0;i<=3;i ++)
printf("%02x", (unsigned char)p);
}

the above code gives the binary representation of a.
how does it work?

It doesn't work at all.

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
float a = 5.375;
unsigned char *p;
size_t i;

p = (unsigned char *)&a;
for (i = 0; i != sizeof a; ++i) {
Any reason to use != where the rest of the world uses <?
(Just curious, I know that *here* they do the same.)
printf("%02x ", p[i]);
}
putchar('\n');
return 0;
}

/* END new.c */
--
pete

Jun 16 '07 #7
Army1987 wrote:
"pete" <pf*****@mindsp ring.comha scritto nel messaggio
news:46******** **@mindspring.c om...
> for (i = 0; i != sizeof a; ++i) {
Any reason to use != where the rest of the world uses <?
(Just curious, I know that *here* they do the same.)
In such constructs I usually put != instead of < as well. It doesn't matter
here, and it's the same thing that would be required in just a slightly
different example:
for (p = start; p != NULL; p = p->next)
Jun 16 '07 #8
In article <f5**********@t di.cu.mi.it>, Army1987 <pl********@for .itwrote:
> for (i = 0; i != sizeof a; ++i) {
>Any reason to use != where the rest of the world uses <?
There is a theory that it's better to use "<", because if the variable
somehow gets to be bigger than the terminating value, the loop will
still stop. I believe this is sometimes considered to be "defensive
programming".

There is another theory that this is a really bad idea, because it will
hide bugs in your program (how did the variable get the bogus value?),
and you should instead use "!=" to make the error get noticed sooner.

From the point of view of readability, I think that "<" is more likely to
express the way the programmer is thinking about it - the terminating
value expresses the end of a range, rather than a sentinel value as
is "while(*p++ != '\0')".

So using "!=" seems to me to be in the same class of idioms as writing
"if(1 == a)" rather than "if(a == 1)" - it might sometimes result in
earlier error detection, but at the expense of naturalness and hence
readability.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 16 '07 #9
Harald van D?k wrote:
>
Army1987 wrote:
"pete" <pf*****@mindsp ring.comha scritto nel messaggio
news:46******** **@mindspring.c om...
for (i = 0; i != sizeof a; ++i) {
Any reason to use != where the rest of the world uses <?
(Just curious, I know that *here* they do the same.)

In such constructs I usually put != instead of < as well. It doesn't matter
here, and it's the same thing that would be required in just a slightly
different example:
for (p = start; p != NULL; p = p->next)
'<' is safer. If something glitches the value of i.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 16 '07 #10

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

Similar topics

3
11300
by: Peter Rohleder | last post by:
Hi, I'm using a style-sheet where I make use of the XPATH-"following-sibling"-expression. The part which makes problems looks similar to the following code: --------------------------- <xsl:for-each select="headdata/extension/person">
1
2789
by: Marlon | last post by:
<! -- Web.config--> <authentication mode="Windows" /> <identity impersonate="true" /> <! -- Web.config--> where Configurations.ADsUsersPath is LDAP://domain.com/ou=Users,ou=Corporate,dc=domain,dc=com ds = New DirectoryEntry(Configurations.ADsUsersPath)
1
1145
by: Balaji. M. | last post by:
Hi, The following code not works in netscape 7.2, function WizardMoveNext () { CurrentStep++; WizardPage = "regreqstep" + CurrentStep + ".php"; alert ("Current Wizard Page " + WizardPage); document.getElementById("frSubTab").src = WizardPage;
8
2703
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
0
1230
by: Kathy Burke | last post by:
I'm providing the following syntax in hopes someone could tell me why I get an object reference error on the second one. The first one works, using an xmlDocument, the second one immediately follows and uses the same xmlDocument. It works if the result is not Nothing. I've used similar xpath expressions (but never with the following-sibling reference) and the If Nothing works ok. Spent hours on this, but I need another set of eyes and a...
13
2156
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the code? Thanks PS I added the +1 because as I understand the GetLogicalDriveStrings doc
1
2395
by: Jedufa | last post by:
following of thread: "Adding namespaces to code behind automatically" Hello, I had quite the same problem and got further in the right direction with your suggestions, thanks. Nevertheless, I have a newer problem which seems to come from the fact that the assembly which contains my "SimpleComponentToolboxItem" must be already referenced
4
1120
by: zaperaj | last post by:
Im working with python2.2 on red hat linux. The following program is supposed to print decreasing numbers from an entered number till 1, each decrement being = 1 : #! usr/bin/env/python def f(n=int(raw_input("enter number: "))): print 'n=',n if n>1: return n*f(n-1)
2
2100
by: SRT | last post by:
Hi, I have the following code below, which works if I do the following Call No1 'Call No2 Exit Sub For whatever reason it fails at Me!.SetFocus at the end of Sub No2 with
6
2062
by: dba | last post by:
using the following code with a problem.... echo "<input type='hidden' name='member_id' value=\"{$row}\">{$row}"; echo "<input type='radio' name='member_name' value=\"{$row}\">{$row}<br />"; The post_data.php program posts the following member id is: 0009
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7375
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.