以下代码如何工作? [英] how the following code works?
问题描述
main(){
浮动a = 5.375;
char * p;
int i;
p =(char *)& a;
for(i = 0; i< = 3; i ++)
printf("%02x",(unsigned char) )p);
}
上面的代码给出了一个二进制表示形式。
它是如何工作的? />
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?
推荐答案
在文章< 11 ********************* @ e9g2000prf.googlegroups.c om>,
Ravi< ra ********* @ gmail.comwrote:
In article <11*********************@e9g2000prf.googlegroups.c om>,
Ravi <ra*********@gmail.comwrote:
> main(){
浮动a = 5.375;
char * p;
int i;
p =(char *)& a;
for( i = 0; i< = 3; i ++)
printf("%02x",(unsigned char)p);
}
上面的代码给出了二进制表示一个。
它是如何工作的?
>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?
它没有。
dave
(它似乎是试图检查字节代表一个对象
的浮点类型,但至少包含一个明显的错误,一个
微妙的错误,以及一个无偿的假设。)
>
-
Dave Vandervies dj******@csclub.uwaterloo .ca
PJ Plauger有正当的理由[...]虽然我会说这与他的客户关系不太合理。
- 在comp.lang.c中删除Gordon
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
Ravi写道:
Ravi wrote:
>
main(){
浮动a = 5.375;
char * p;
int i;
p =(char *)& a;
for(i = 0; i< = 3; i ++)
printf( "%02x",(unsigned char)p);
}
上面的代码给出了二进制表示一个。
它是如何工作的?
>
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?
它根本不起作用。
/ * BEGIN new.c * /
#include< stdio.h>
int main(无效)
{
浮动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'');
返回0;
}
/ * END new.c * /
-
pete
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
" Ravi" < ra ********* @ gmail.comwrote in message
news:11 ********************* @ e9g2000prf.googlegrou ps.com ...
"Ravi" <ra*********@gmail.comwrote in message
news:11*********************@e9g2000prf.googlegrou ps.com...
main(){
浮动a = 5.375;
char * p;
int i;
p =(char *)& a;
for(i = 0; i< = 3; i ++)
printf("%02x",(unsigned char)p);
}
以上代码给出了一个二进制表示。
它是如何工作的?
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?
让我们稍微重写一下。
#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(无效)
{
浮动a = 123.567;
转储(& ; a,sizeof(float));
返回0;
}
现在您应该看看该程序的工作原理。另外,你有一个方便的小
例程,你可以随时剪切和粘贴检查一个对象的'
二进制表示。
-
免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm
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
这篇关于以下代码如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!