代码的行为 [英] Behavior of the code

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

问题描述

大家好,


我正在练习一个C教程。

因为他们给了一个小代码并且询问了输出。


#include< stdio.h>

int main(无效)

{

int x = 0xFFFFFFF0;

signed char y;

y = x;

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

返回0;

}

程序的输出是


fffffff0


他们给出的解释如下:


由于%x用于打印,printf语句将推广

y到整数,因此将打印fffffff0。


我对这个解释有疑问。


我的理解是

1)如果我们指定一个超过其最大尺寸的签名变量,它将溢出所以行为是未定义的。

2)在说明中它说%x将y提升为整数,但是我认为它是骗局的将其参数转换为十六进制。

我的理解错了吗?


请帮助。


问候,

Somenath

Hi All,

I was going through one of the exercise of one C tutorial .
In that they have given one small code and asked about the output.

#include <stdio.h>
int main(void)
{
int x = 0xFFFFFFF0;
signed char y;
y = x;
printf("%x\n", y);
return 0;
}
Output of the program is

fffffff0

The explanation they have given as bellow

Since %x is being used for printing, the printf statement will promote
y to an integer and hence fffffff0 will be printed.

I have doubt about the explanation.

My understanding is
1) if we assign a signed variable more than its max size it will
overflow so the behavior is undefined .
2) And in the explanation it says %x promote y to an integer but I
think it converts its argument to hexadecimal.
Is my understanding wrong?

Please help.

Regards,
Somenath

推荐答案

12月24日晚上8:57,somenath< somenath ... @ gmail.comwrote:
On Dec 24, 8:57 pm, somenath <somenath...@gmail.comwrote:

我正在练习一个C教程。

因为他们给了一个小代码并询问了输出。


#include< stdio.h>

int main(无效)

{

int x = 0xFFFFFFF0;

signed char y;

y = x;

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

返回0;}


程序输出是


fffffff0


他们给出的解释如下:


由于%x用于打印,printf语句会将
$ b $提升为整数,因此将打印fffffff0 />

我对这个解释有疑问。
I was going through one of the exercise of one C tutorial .
In that they have given one small code and asked about the output.

#include <stdio.h>
int main(void)
{
int x = 0xFFFFFFF0;
signed char y;
y = x;
printf("%x\n", y);
return 0;}

Output of the program is

fffffff0

The explanation they have given as bellow

Since %x is being used for printing, the printf statement will promote
y to an integer and hence fffffff0 will be printed.

I have doubt about the explanation.



好​​。解释是错误的。

Good. The explanation is wrong.


我的理解是

1)如果我们分配的签名变量大于其最大大小它将

溢出,因此行为未定义。
My understanding is
1) if we assign a signed variable more than its max size it will
overflow so the behavior is undefined .



这并不是那么糟糕。如果0xfffffff0被解释为int

并且ints有一个32位的二进制补码表示,那么x是-16,

这是在signed char的范围内。对于其他实现

选项,例如64位整数,可能存在未定义的行为

正是你建议的原因。

这里有一些实现依赖:显然大小为

int,

但是(如果我正确地解释标准),是否

0xfffffff0

被解释为int或unsigned int常量。如果它是
解释

为unsigned int那么我认为你确实有未定义的行为

It''s not quite as bad as that. If 0xfffffff0 is interpreted as an int
and ints have a 32-bit two''s complement representation then x is -16,
which is within the range of signed char. For other implementation
choices, such as 64-bit ints, there may be undefined behaviour for
exactly the reason you suggest.

There is some implementation-dependence here: obviously the size of
int,
but also (if I am interpreting the standard correctly), whether
0xfffffff0
is interpreted as an int or unsigned int constant. If it is
interpreted
as unsigned int then I think you do have undefined behaviour


2)在解释中它说%x将y提升为整数,但是我认为它将其参数转换为十六进制。
2) And in the explanation it says %x promote y to an integer but I
think it converts its argument to hexadecimal.



是的。你是对的,解释是错误的。我们可以查看一个

简化版本的程序,它没有从int转换为令人困惑的



#include< stdio.h>

int main(无效)

{

signed char y = -16;

printf (%x \ n,y);

返回0;

}

实际输出是实现指定的,但是在我的电脑上

这个

还打印

fffffff0


在这个程序中,因为y是可变长度参数的一部分

printf()列表它将被默认参数

促销提升为int [1]。 printf()接收int值为-16,

%x格式以十六进制格式请求。 %x格式对于促销y到int没有任何结果。

-thomas


[1] I ''我不确定签名字符是否有可能具有不适合int的值

。我确定名单上有人。

Yes. You are right, the explanation is wrong. We can look at a
simplified version of the program that doesn''t have the confusing
conversion from int.
#include <stdio.h>
int main(void)
{
signed char y = -16;
printf("%x\n", y);
return 0;
}
The actual output is implementation-specified, but on my computer
this
also prints
fffffff0

In this program, since y is part of the variable-length argument
list of printf() it will be promoted[1] by the default argument
promotions to int. printf() receives an int value of -16, and the
%x format asks for this in hexadecimal. The %x format has nothing to
do with the promotion of y to int.
-thomas

[1] I''m not sure whether it is possible for signed char to have values
that don''t fit in an int. I''m sure that someone on the list is.


Thomas Lumley写道:
Thomas Lumley wrote:

12月24日,8:57 pm,somenath< somenath ... @ gmail.comwrote:
On Dec 24, 8:57 pm, somenath <somenath...@gmail.comwrote:

>我正在进行一个C教程的练习。
因为他们给出了一个小代码并询问了输出。

#include< stdio.h>
int main(无效)
{
int x = 0xFFFFFFF0;
签名char y;
y = x;
printf("%x \ n",y);
返回0;}

该程序的输出是

fffffff0

他们给出的解释如下:

因为%x被用于打印时,printf语句会将y提升为整数,因此会打印出fffffff0。

我对这个解释有疑问。
>I was going through one of the exercise of one C tutorial .
In that they have given one small code and asked about the output.

#include <stdio.h>
int main(void)
{
int x = 0xFFFFFFF0;
signed char y;
y = x;
printf("%x\n", y);
return 0;}

Output of the program is

fffffff0

The explanation they have given as bellow

Since %x is being used for printing, the printf statement will
promote y to an integer and hence fffffff0 will be printed.

I have doubt about the explanation.



好​​。解释是错误的。


Good. The explanation is wrong.


>我的理解是
1)如果我们分配的签名变量大于其最大大小它
将溢出,因此行为未定义。
>My understanding is
1) if we assign a signed variable more than its max size it
will overflow so the behavior is undefined .



这并不是那么糟糕。如果0xfffffff0被解释为int

并且int有32位二进制补码表示[...]


It''s not quite as bad as that. If 0xfffffff0 is interpreted as an int
and ints have a 32-bit two''s complement representation [ ... ]



C标准明确地为这种限制辩解。 int既不需要

为32位也不以二进制形式表示。它需要

保持在-32767到32767范围内的值。它可以是二进制补码,

补码或符号和大小等等。它没有明确说明,但我们可能假定int至少有16个值位,

因为C需要二进制表示。

The C standard explicitly excuses such restrictions. int neither needs
to be 32 bits nor be represented in twos-complement form. It needs to
hold values in the range -32767 to 32767. It could be twos-complement,
ones-complement or sign-and-magnitude or something else. It is not
explicitly stated but we may presume at least 16 value bits for an int,
as C needs binary representation.


12月25日,10:57 * am,santosh< santosh .... @ gmail.comwrote:
On Dec 25, 10:57*am, santosh <santosh....@gmail.comwrote:

Thomas Lumley写道:
Thomas Lumley wrote:

12月24日晚上8:57,somenath< somenath ... @ gmail.comwrote:
On Dec 24, 8:57 pm, somenath <somenath...@gmail.comwrote:

我正在进行一个C教程的练习。

因为他们给了一个小代码并询问了输出。
I was going through one of the exercise of one C tutorial .
In that they have given one small code and asked about the output.


#include< stdio.h>

int main (无效)

{

* * * * int x = 0xFFFFFFF0;

* * * * signed char y;

* * * * y = x;

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

* * * * return 0;}
#include <stdio.h>
int main(void)
{
* * * * int x = 0xFFFFFFF0;
* * * * signed char y;
* * * * y = x;
* * * * printf("%x\n", y);
* * * * return 0;}


*程序的输出是
Output of the *program is


fffffff0
fffffff0


他们给出的解释如下:
The explanation they have given as bellow


由于%x正在用于打印,printf语句将

将y提升为整数,因此将打印fffffff0 。
Since %x is being used for printing, the printf statement will
promote y to an integer and hence fffffff0 will be printed.


我对这个解释有疑问。
I have doubt about the explanation.


好​​。 *解释错了。
Good. * The explanation is wrong.


我的理解是

1)* * *如果我们分配一个签名变量超过它的最大尺寸*它

将溢出,因此行为未定义。
My understanding is
1) * * *if we assign a signed variable more than its max size *it
will overflow so the behavior is undefined .


这并不是那么糟糕。 *如果0xfffffff0被解释为int

且ints有32位二进制补码表示[...]
It''s not quite as bad as that. *If 0xfffffff0 is interpreted as an int
and ints have a 32-bit two''s complement representation [ ... ]



C标准明确地为这些限制辩解。 int既不需要

为32位也不以二进制形式表示。它需要

保持在-32767到32767范围内的值。它可以是二进制补码,

补码或符号和大小等等。这不是明确说明的b $ b,但是我们可能假设一个int至少有16个值位,


The C standard explicitly excuses such restrictions. int neither needs
to be 32 bits nor be represented in twos-complement form. It needs to
hold values in the range -32767 to 32767. It could be twos-complement,
ones-complement or sign-and-magnitude or something else. It is not
explicitly stated but we may presume at least 16 value bits for an int,



更清楚一点。 />
因此,如果我将128分配给已签名的字符,则无需担保它将

转换为-128

例如

签名字符x = 0x80;

x并不总是包含-128?

只有2'的补码表示才有可能吗?

To get little bit clearer .
So if i assigned 128 to signed char it is not guranted that it will
be converted to -128
For example
signed char x = 0x80;
x will not always contains -128 ?
Is it only possible in case of 2''s complement representation ?


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

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