请解释输出 [英] Please explain the output

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

问题描述

我最近在接受采访时被问到这个问题。不幸的是我没有能够回答它而且面试官基于这个单一的问题对我的C

强度(或周长)做出了决定,这是一个

对我的采访感到悲伤。这是程序:


#include< stdio.h>


int main()

{

char * c =" abc";

int * i =(int *)c;

printf("%x" ,* i);

返回0;

}


输出结果为:

636261 。


我知道hex 61是十进制97,这是a的ASCII码。 hex 62

是b的代码,依此类推。我的疑问是为什么它以相反的顺序打印ascii代码




如果这个问题非常基本,我会道歉。

解决方案

文章< 11 ********************* @ g44g2000cwa。 Google网上论坛。 com>,

Jaspreet< js *********** @ gmail.com>写道:

#include< stdio.h>
int main()
{* * char * c =" abc";
int * i =(int *)c;
printf("%x" ;,* i);
返回0;
}
输出结果为:
636261。


不一般,不是。例如,在我正在使用的系统上,

输出为61626300

我知道十六进制61是十进制97,这是a的ASCII码。 hex 62
是b的代码,依此类推。我的查询是为什么它以相反的顺序打印ascii代码




在整数表示为little-endian的系统上,

字节顺序有时是4321(有时是2143)。整数(或浮点数或双精度数)存储在RAM中的顺序

通常与内部处理器的寄存器顺序不同。

-

没有人有权通过要求经验证据来摧毁另一个人的信念。 - Ann Landers


Jaspreet写道:

我最近在一次采访中被问到这个问题。不幸的是,我无法回答这个问题,而且面试官基于这个单一的问题决定了我的优势(或周长),这对我的采访来说是一个悲伤的结局。这是程序:

#include< stdio.h>

int main()
{
char * c =" abc" ;
int * i =(int *)c;
printf("%x",* i);
返回0;
}
输出是:
636261.

我知道hex 61是十进制97,它是a的ASCII码。 hex 62
是b的代码,依此类推。我的问题是为什么它以相反的顺序打印ascii代码


我很抱歉这个问题是非常基本的。




首先,IIRC,这个程序调用未定义的行为,因为

它取消引用已转换为另一种类型的指针。

由于行为未定义,输出可以是任何东西。对于你们所有人来说,b $ b知道可能没有输出,或者你的计算机可能会燃烧。


除此之外,你可能在32位平台上

的整数以大端格式存储在内存中。字符串abc表示字符串abc。在内存中存储了




|''''|'''b''|'''''| 0 |

^

|

|

char * c


现在当你将c转换为int *并取消引用它时,这四个字节是

被解释为大端格式的整数,这意味着0是

MSB和'a''LSB。


Jaspreet写道:

我最近在一次采访中被问到这个问题。不幸的是,我无法回答这个问题,而且面试官基于这个单一的问题决定了我的优势(或周长),这对我的采访来说是一个悲伤的结局。这是程序:

#include< stdio.h>

int main()
{
char * c =" abc" ;


最好在指向char的const指针中存储指向字符串文字的指针。

int * i =(int *)c;


此指针转换具有未定义的行为;首先,指向char的

指针可能无法正确对齐int!

printf("%x",* i);


1.指针可能未对齐

2.如果sizeof(int)> 4则部分值未经初始化

3.该值可能是int的陷阱表示
4.%x指定unsigned int,而不是signed int

5. stdout stream应以换行符结尾

返回0;
}

输出结果为:
636261。




这只是未定义行为的一个可能结果。


-

Simon。


I was recently asked this question in an interview. Unfortunately I was
not able to answer it and the interviewer made a decision on my C
strengths (or weekness) based on this single question and that was a
sad end to my interview. Here is the program:

#include <stdio.h>

int main()
{
char *c ="abc";
int *i=(int*)c;
printf("%x", *i);
return 0;
}

The output is:
636261.

I know that hex 61 is decimal 97 which is the ASCII code for a. hex 62
is code for b and so on. My query is why is it printing the ascii codes
in the reverse order.

I apologise if this questios is a very basic one.

解决方案

In article <11*********************@g44g2000cwa.googlegroups. com>,
Jaspreet <js***********@gmail.com> wrote:

#include <stdio.h> int main()
{
char *c ="abc";
int *i=(int*)c;
printf("%x", *i);
return 0;
} The output is:
636261.
Not in general, it isn''t. For example on the system I''m using,
the output is 61626300
I know that hex 61 is decimal 97 which is the ASCII code for a. hex 62
is code for b and so on. My query is why is it printing the ascii codes
in the reverse order.



On systems in which the representation of integers is little-endian,
the byte order is sometimes 4321 (and sometimes 2143). The order
that an integer (or float or double) is stored into RAM is often
not the same as the internal processor in-register order.
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers


Jaspreet wrote:

I was recently asked this question in an interview. Unfortunately I was
not able to answer it and the interviewer made a decision on my C
strengths (or weekness) based on this single question and that was a
sad end to my interview. Here is the program:

#include <stdio.h>

int main()
{
char *c ="abc";
int *i=(int*)c;
printf("%x", *i);
return 0;
}

The output is:
636261.

I know that hex 61 is decimal 97 which is the ASCII code for a. hex 62
is code for b and so on. My query is why is it printing the ascii codes
in the reverse order.

I apologise if this questios is a very basic one.



In first place, IIRC, this program invokes undefined behaviour because
it dereferences a pointer that has been converted to another type.
Since behaviour is undefined, the output could be anything. For all you
know there could be no output, or your computer may burn.

Besides that, you''re probably in a platform with 32 bit integers that
are stored in memory in big endian format. The string "abc" is stored
in memory:

|''a''|''b''|''c''| 0 |
^
|
|
char *c

Now when you cast c to an int* and dereference it, these four bytes are
interpreted as an integer in big endian format, wich means that 0 is
the MSB and ''a'' the LSB.


Jaspreet wrote:

I was recently asked this question in an interview. Unfortunately I was
not able to answer it and the interviewer made a decision on my C
strengths (or weekness) based on this single question and that was a
sad end to my interview. Here is the program:

#include <stdio.h>

int main()
{
char *c ="abc";
It''s best to store pointers to string literals in a const pointer to char.
int *i=(int*)c;
This pointer conversion has undefined behaviour; for one thing, the
pointer to char may not be aligned correctly for an int!
printf("%x", *i);
1. the pointer may be mis-aligned
2. if sizeof(int)>4 then part of the value is uninitialised
3. the value may be a trap representation for int
4. the %x specifies an unsigned int, not a signed int
5. the stdout stream should end with a newline character
return 0;
}

The output is:
636261.



That''s just one possible result of the undefined behaviour.

--
Simon.


这篇关于请解释输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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