一些源代码需要帮助 [英] Help wanted on some source codes

查看:48
本文介绍了一些源代码需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。=========================================== <无线电通信/>
/ * ac * /

int x;

int y;


void main()< br $>
{

f();

printf("%x%x \ n",x,y);

}


/ * bc * /

double x;


void f()

{

x = -0.0;

}

2. ============ ===============================

/ * ac * /

int x = 1;

int y;


void main()

{

f();

printf("%x%x \ n",x,y);

}


/ * bc * /

双倍x;


void f()

{

x = -0.0;

}


3。======================= ====================

/ * ac * /

int x;

int y = 1;


void main()

{

f();

printf("%x%x \ n",x,y);

}


/ * bc * /

double x;


void f()

{

x = -0.0;

}


4。=================================== ========

/ * ac * /

int x = 1;

int y = 1;


void main()

{

f();

printf("%x%x \\ \\ n",x,y);

}


/ * bc * /

double x;


void f()

{

x = -0.0;

}


5. ===========================================

/ * ac * /

int x = 1;

int y = 1;


void main()

{

f();

printf("%x%x \ n",x,y);

}


/ * bc * /

double x;


无效f()

{

x = -0;

}

备注======== ===============================

上述程序从1到5都构成

两个单独的文件,ac& bc,可以在gcc下编译


例如

/ * Linux * /

$ gcc - o测试ac bc

$ ./test

/ * Windows * /

C:\ gcc -o test.exe ac bc

C:\ test

问题============================= ==========

我想知道程序产生的奇怪结果

。请帮我。我想了解

为什么结果是这样的。

顺便说一句,上面的源代码是旧的K& R风格

并且将由gcc警告,你可以将它们更改为

新的ISO / ANSI样式:-)


在此先感谢&最好的问候!


Becker

30-Nov-2005

1.===========================================
/* a.c */
int x;
int y;

void main()
{
f();
printf("%x %x\n", x, y);
}

/* b.c */
double x;

void f()
{
x = -0.0;
}
2.===========================================
/* a.c */
int x = 1;
int y;

void main()
{
f();
printf("%x %x\n", x, y);
}

/* b.c */
double x;

void f()
{
x = -0.0;
}

3.===========================================
/* a.c */
int x;
int y = 1;

void main()
{
f();
printf("%x %x\n", x, y);
}

/* b.c */
double x;

void f()
{
x = -0.0;
}

4.===========================================
/* a.c */
int x = 1;
int y = 1;

void main()
{
f();
printf("%x %x\n", x, y);
}

/* b.c */
double x;

void f()
{
x = -0.0;
}

5.===========================================
/* a.c */
int x = 1;
int y = 1;

void main()
{
f();
printf("%x %x\n", x, y);
}

/* b.c */
double x;

void f()
{
x = -0;
}
Remark=======================================
The above programs from 1 to 5 are all constituted
of two separate files, a.c & b.c, and can be
compiled under gcc.
e.g.
/* Linux */
$ gcc -o test a.c b.c
$ ./test
/* Windows */
C:\gcc -o test.exe a.c b.c
C:\test
Question=======================================
I''m wondering about the strange results generated
by the programs. Please help me. I want to understand
why the results are like that.
By the way, the source codes above are in old K&R style
and will be warned by the gcc, you can change them to
new ISO/ANSI style :-)

Thanks in advance & best regards!

Becker
30-Nov-2005

推荐答案

gcc -o test ac bc
gcc -o test a.c b.c


./ test

/ * Windows * /

C :\ gcc -o test.exe ac bc

C:\ test

问题================= ======================

我想知道产生的奇怪结果

by the程式。请帮我。我想了解

为什么结果是这样的。

顺便说一句,上面的源代码是旧的K& R风格

并且将由gcc警告,你可以将它们更改为

新的ISO / ANSI样式:-)


在此先感谢&最好的问候!


Becker

30-Nov-2005

./test
/* Windows */
C:\gcc -o test.exe a.c b.c
C:\test
Question=======================================
I''m wondering about the strange results generated
by the programs. Please help me. I want to understand
why the results are like that.
By the way, the source codes above are in old K&R style
and will be warned by the gcc, you can change them to
new ISO/ANSI style :-)

Thanks in advance & best regards!

Becker
30-Nov-2005


文章< 11 ********************** @ z14g2000cwz.googlegroups .com> ;,

Becker< we ***** **@devhr.com>写道:
In article <11**********************@z14g2000cwz.googlegroups .com>,
Becker <we*******@devhr.com> wrote:
1. ====================================== =====
/ * ac * /
int x;
int y;

void main()
{
f ();
printf("%x%x \ n",x,y);
}


%x需要一个unsigned int,而不是一个int。

/ * bc * /
双x;

void f()
{
x = -0.0;
}


你永远不会初始化y,并且你将x初始化为错误的

类型。 double可能与int的大小相同,也可能不同;它不是很长,但这并不确定。如果它更长

那么你 - 最终可能会覆盖y,但x和
$ b $的相对顺序不是由标准确定的,而double可能大于两个

一起注入所以你可能会在随机的内存部分涂鸦。


如果您碰巧使用IEEE754浮点数,则-0.0是

被认为不同于0.0。即使在不是的系统上,

浮点表示为0并不一定都是二进制的

0'(尽管 - 浮点数标准要求所有二进制0'的
必须被视为浮点0.)


您的程序2到4只是完全相同主题的变体

各种不同的值初始化内存可能或者可能不会被潦草地写下来。


5。==== =======================================
void f()
{
x = -0;
}
1.===========================================
/* a.c */
int x;
int y;

void main()
{
f();
printf("%x %x\n", x, y);
}
%x expects an unsigned int, not an int.
/* b.c */
double x;

void f()
{
x = -0.0;
}
You never initialize y at all, and you initialize x to the wrong
type. double might or might not be the same size as int; it is not
uncommon for it to be longer, but that is not certain. If it is longer
then you -might- end up overwritting y, but the relative order of x and
y are not fixed by the standard, and double might be larger than two
ints together so you might be scribbling on a random portion of memory.

If you happen to be using IEEE754 floating point, then -0.0 is
considered to be different than 0.0 . Even on systems that it is not,
the floating point representation of 0 is not certain to be all binary
0''s (though -some- floating point standards require that all-binary 0''s
must be treated as floating point 0.)

Your programs 2 thru 4 are just variations on exactly the same themes
with various different values initializing the memory that might or might
not be scribbled over.

5.=========================================== void f()
{
x = -0;
}




-0是一个整数常量,它与除了

相当罕见的有符号幅度的机器(

标准允许这样做。) - 0作为整数因此与0作为整数相同。 />
然后将该整数强制转换为您声明为x的双精度值

在此文件中。在一些不同于浮动的系统上

指向-0.0。

-

重要的是要记住,当它到来时对于法律,电脑

从不复制,只有人类复制。计算机给出了

命令,而非许可。只有人才能获得许可。

- Brad Templeton



-0 is an integer constant, and it is the same as 0 on all but the
rather-uncommon signed-magnitude machines (which are allowed for by
the standard.) -0 as an integer is thus the same as 0 as an integer.
That integer is then cast to the double that you have declared x to
be in this file. On some systems that is different than the floating
point -0.0 .
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton


这篇关于一些源代码需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆