您的位置:程序门 -> c/c++ ->



求2道c语言面试题目答案


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求2道c语言面试题目答案
发表于:2007-10-29 14:31:16 楼主
    昨天去单位面试,有两道题目,各位大哥能帮我解决一下么?我是新来的,不知道怎么给分,谢谢各位了。
      一.   write   a   simple   routine   that   shows(i.e.   prints   to   stdout   as   a   human-readable   string)   how   a   given   32-bit   float   is   represented   internally,in   hexadecimal   form.
      for   example:3.1415929-> "dc   0f   49   40"     //这个例子是怎么从float转到十六进制的????
      notes:
          1.you   do   not   need   to   assume   byte   order.that   is,"40490fdc"   is   as   good   as   "dc0f4940"   for   the   float   3.1415929
          2.you   may   use   the   standard   i/o   functions   such   as   printf(),where   appropriate.
          3.printf("%x",float)   is   not   the   correct   answer,since   %x   requires   an   integer   argrment.
    我看了一下没有头绪,大家能给一个算法我,好么?

    二.     if(x==123)   和   (123==x)   有什么不同啊,我上机调试也没发现,我在书上也没看到,哪位能给我解释一下么?
发表于:2007-10-29 14:42:391楼 得分:0
if(x==123)   和   (123==x)功能上没有区别,后者只是一种好的风格,一种不会出错的写法。
发表于:2007-10-29 14:49:182楼 得分:0
1.浮点数内存分布有三块,符号,小数,指数
2.123==x即使少写了一个=,编译器会提醒你,因为不允许常量作左值
    如果x==123,少写了=号就神仙也不知道了
发表于:2007-10-29 15:34:473楼 得分:0
看不懂第一个,顶起
发表于:2007-10-29 15:37:574楼 得分:0
第一题只是要求   输出   在内存的   表示而已。
不需要知道浮点数怎么表示的
    8           float   a   =   3.1415926;
    9  
  10           char   base[17]   =   "0123456789abcdef";
  11           int   i;  
  12           for   (i   =   0;   i   <   sizeof(float);   i++)
  13           {      
  14                   char   *tmp   =   (char*)&a   +   i;
  15                   printf("%c%c   ",   base[(((*tmp)> > 4)&0xf)],   base[((*tmp)&0xf)]);
  16           }
  17          
  18           printf("\n");
发表于:2007-10-29 15:56:385楼 得分:0
c/c++ code
13 { 14 char *tmp =char*)&a + i; 15 printf("%c%c ", base[(((*tmp) > >4)&0xf)], base[((*tmp)&0xf)]); 16 }

这段代码是什么意思啊?
发表于:2007-10-29 16:05:026楼 得分:0
1.文件操作搞定。。

file   *fp;
float   a   =   3.1415929;

fwrite(&a,   sizeof(float),   1,   fp);
发表于:2007-10-29 16:34:507楼 得分:0
float   a=3.1415926;
unsigned   char   *p=(unsigned   char   *)(&a);
printf("%x%x   %x%x   %x%x   %x%x\n",(*p)> > 4,(*p)&0x0f,
                                *(p+1)> > 4,*(p+1)&0x0f,*(p+2)> > 4,*(p+2)&0x0f,
                                *(p+3)> > 4,*(p+3)&0x0f);
       
发表于:2007-10-29 16:40:068楼 得分:0
#include   <stdio.h>
 
int   main()
{
        float   fnum=   3.1415929;  
        printf("\n%x",*(unsigned   *)&fnum);
        return       0;  
}  
发表于:2007-10-29 16:58:529楼 得分:0
http://blog.csdn.net/biblereader/archive/2006/06/21/819428.aspx
发表于:2007-10-29 17:12:2210楼 得分:0
哦,原来是这样子.....
发表于:2007-10-29 17:44:3811楼 得分:0
谢谢各位了


快速检索

最新资讯
热门点击