声明:本文为博主原创文章,未经博主允许不得转载。

以下是英文翻译:

warnning: Copyright!you can't reprint this blog when you not get blogger's  permission.

It is a very excellent interview question that concludes serveal important knowledge which explain how to correctly use printf( ) function that come from  C language standard library.

 #include <stdio.h>

 int main ( )

 {    unsigned int a=0xfffffff7; 

     unsigned char i=(unsigned char)a; 

     char *b=(char*)&a;   

     printf("x,%x\n",i,*b); 

 }

The main function just conclude four statements. ​The frist statement declare a unsigned integer variable that is assigned as 0xfffffff7.The secend statement means unsigned integer variable,a is assigned to unsigned character variable, i ,but the way of this conservation cause a significant problem which is called as "Byte truncation". The third statement get a's address that is assigned to a pointer  pointing to character. The last statement use printf function print out value of variable i and *b according a particular pattern.

When i use vs2012 ​compile program , the value of i is 0x000000f7 and the *b is 0xfffffff7.

输出的i为0x000000f7并不奇怪,因为a是个无符号的整形数被赋值给无符号的字符数时发生了字节截断,故第二条语句执行完后i的值为0xf7,再 根据printf指定的输出格式即按十六进制输出,位数为8位。但是*b的值估计也“不难”理解:这里做了指针转换即把unsigned int a的地址转换为char类型的指针,影响了指针的寻址。故b所在的内存里存的值便为0xfffffff7。这种解释正是XX宝典中的解释,很明显这种解释 太过牵强,甚至可以说的错误的,试想一个字符型的指针指向的值怎么会是0xfffffff7。如果非要说这样是对的,我可以继续做下面这个测试。

这是比较讽刺的。a被赋值为0x123456f7后*b竟然依然为0xfffffff7。这到底发生了什么,正确的解释可是是这样的。:)

第三条语句其实同样发生了字节截断,&a的数据类型被强制转换为​char
*后,此时b只能指向0xfffffff7或是0x123456f7最低的一个字节所在的地址,原因是我们用的一般都是X86
PC机,它是以little-endian方式存储。(So ,what is little-endian ? you can
search it with internet.)
故*b的值为0xf7。当使用printf函数输出结果时,在参数发生了default argument
promotions . C语言规定了:不足int的整形提升为整形,float 提升为 double。对于char
*类型的b所取得值0xf7,
这是二进制补码表示形式转换为十进制整数为-9,而-9按int型入栈时为0xfffffff7,其实面试题上输出时还给定了格式x,其实这样分析完根本就不需要按八位输出,因为肯定是输出八位的
:)正如上图我略去08后依然输出fffffff7。

​是不是结束了?我觉得还没,如果我把代码改成这样,结果又会是什么呢?:

好了。结果貌似不是期望的ffffff78,哈哈,其实也没什么奇怪的。0x78转换为二进制最高位0,也就是正数,而正数的补码跟原码一样故输出为0x78,如果这时给出格式符x,输出就是00000078(很明显高位根据格式补零了)。

关于printf输出问题还有其他类似的情况,所以遇到实参和形参的类型不一样时务必要警惕。

到此,这个问题先告一段落了。 :)​

an interview question(1)的更多相关文章

  1. Core Java Interview Question Answer

    This is a new series of sharing core Java interview question and answer on Finance domain and mostly ...

  2. shit LeetCode interview Question

    shit LeetCode interview Question https://leetcode.com/interview/1/ 有点晕,啥意思,没太明白,到底是要按什么排序呀? 去掉 标识符 不 ...

  3. JavaScript interview Question - Create a Array with two papameters without using loop!

    JavaScript interview Question - Create a Array with two papameters without using loop! JavaScript - ...

  4. An interview question from MicroStrategy

    去年校招时的一道面试题,觉得蛮有意思,贴出来. Question: Spy start at a, during an interval he moves |b| to right when b &g ...

  5. an interview question(4)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 写这篇博客前请让博主先吐糟下自己的PC. i3+2G内存+开了一上午=C盘剩下0字节+打开VS2012花了半个小时+一晚上的心情不好 吐槽完PC, ...

  6. an interview question(3)

    最近看了些C面试题顺便复习一下C语言,现贴一些出来和大家分享. #include <stdio.h> void main () { ,,,,};--------- *(ptr++)+=; ...

  7. an interview question(2)

    感觉现在好多面试题还是很注重基础的,今天面试时就遇到这题,回来一查后才知道此题是国内某著名通信公司的一道机试题:) 给定一个数组input[ ],如果数组长度n为奇数,则将数组中最大的元素放到 out ...

  8. Interview Question

    HDS(11.16.2015): How to design an non-stop website like Google or Amazon? What design patterns are y ...

  9. Amazon Interview Question: Design an OO parking lot

    Design an OO parking lot. What classes and functions will it have. It should say, full, empty and al ...

随机推荐

  1. 设置更新源和下载ferret

    kali无法定位软件包 解决: deb http://http.kali.org/kali kali-rolling main non-free contrib kali可用的官方更新源(cd /et ...

  2. [原创]java WEB学习笔记109:Spring学习---spring对JDBC的支持:使用 JdbcTemplate 查询数据库,简化 JDBC 模板查询,在 JDBC 模板中使用具名参数两种实现

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. Media Queries使用方法

    1. 最大宽度max-width “max-width”是媒体特性中最常用的一个特性,其意思是指媒体类型小于或等于指定的宽度时,样式生效.如: @media screen and (max-width ...

  4. linux昨天修改的文件

    find ./ -mtime :返回最近24小时内修改过的文件. find ./ -mtime : 返回的是前48~24小时修改过的文件.而不是48小时以内修改过的文件. Linux查找文件内容的常用 ...

  5. 一:luecne初体验

    package com.cmy.lucene.lucene; import java.io.File; import java.io.FileReader; import java.nio.file. ...

  6. IIS7 全新管理工具AppCmd.exe的命令使用实例分享

    IIS 7 提供了一个新的命令行工具 Appcmd.exe,可以使用该工具来配置和查询 Web 服务器上的对象,并以文本或 XML 格式返回输出. 下面是一些可以使用 Appcmd.exe 完成的任务 ...

  7. 常用oracle表空间查询语句

    --查询数据库表空间使用情况 select a.tablespace_name,a.bytes/1024/1024 "Sum MB",(a.bytes-b.bytes)/1024/ ...

  8. iOS8.3发布了Swift 1.2带来哪些新变化

    苹果前几日在面向开发者推送iOS 8.3 Beta的同时,还发布了版本号为6D520o的Xcode 6.3 Beta,其中便包含了iOS 8.3 Beta和OS X v10.10 SDK,并进一步提升 ...

  9. 01.线性表 ArrayList

    public class MyArrayList { //容量 ; //存放数组元素 private object[] _items; //数组大小 private int _size; //元素个数 ...

  10. SQL 递归

    -- 查询指定部门下面的所有部门, 并汇总各部门的下级部门数 ) SET @Dept_name = N'MIS' ;WITH DEPTS AS( -- 查询指定部门及其下的所有子部门 -- 定位点成员 ...