C语言题目及解析

C语言传递参数均是以值传递(pass by value),另外也可以传递指针(a pointer passed by value)。下面是小编为大家搜索整理的C语言题目及解析,希望大家能有所收获,更多精彩内容请及时关注我们应届毕业生考试网!

C语言题目及解析

  1> 如何判断一个板子的cpu 是big-endian 还是 Little-endian的?

用c实现非常简单,10行左右,就可以判断了, 关键考察新人是否了解了什么是endian ,big-endian与little-endian的区别在哪里, 如果这些不清楚,就算c再强,也是憋不出来的。

  2> 判断了 endian 后, 如何进行转换, 写两个函数。

如果说上面的那个, 可能不能正确的考察出新人的c水平,下面这个,可就可以显示了。

尤其是写一个宏, 来实现。 我觉得宏最能体现出一个人的水平了, 大家都知道一个功能强大的,但是写法又

非常简单的宏,是不好写的。 尤其是注意类型转换, 大扩号什么的。 写一个函数就容易多了。

实现起来,或者 用宏,或者 用函数的'形式, 都可以, 最好都试一下。

主要看的就是宏的使用。

比如:

写成函数的形式:

typedef unsigned int u32 ;

typedef unsigned short u16 ;

u16 bswap16(u16);

u32 bswap32(u32);

写成宏的形式:

#define BSWAP_16(x)

....

#define BSWAP_32(x)

....

比如: 0x1234 变成: 0x3412

或者: 0x12345678 变成 : 0x78563412

---

在下面的回复写出来,就有点乱了, 干脆在这里铁出来吧 ,格式比较好:

  1》判断endian的问题, 很简单。

判断endian :

#include

#include

int main(void)

{

short int a = 0x1234;

char *p = (char *)&a;

printf("p=%#hhxn",*p);

if(*p == 0x34)

printf("Little endian n");

else if(*p == 0x12)

printf("Big endian n");

else

printf("Unknow endian n");

return 0;

}

  2>如何进行转换:

#include

#include

typedef unsigned int u32;

typedef unsigned short u16;

#if 0

//simple: not check varible types

#define BSWAP_16(x)

( (((x) & 0x00ff) << 8 ) |

(((x) & 0xff00) >> 8 )

)

//complex:check varible types

#else

#define BSWAP_16(x)

(u16) ( ((((u16)(x)) & 0x00ff) << 8 ) |

((((u16)(x)) & 0xff00) >> 8 )

)

#endif

#define BSWAP_32(x)

(u32) ( (( ((u32)(x)) & 0xff000000 ) >> 24) |

(( ((u32)(x)) & 0x00ff0000 ) >> 8 ) |

(( ((u32)(x)) & 0x0000ff00 ) << 8 ) |

(( ((u32)(x)) & 0x000000ff ) << 24)

)

u16 bswap16(u16 x)

{

return (x & 0x00ff) << 8 |

(x & 0xff00) >> 8

;

}

u32 bswap32(u32 x)

{

return ( x & 0xff000000 ) >>24 |

( x & 0x00ff0000 ) >>8 |

( x & 0x0000ff00 ) <<8 |

( x & 0x000000ff ) << 24

;

}

int main(void)

{

//u16 var_short = 0x123490;

//u32 var_int = 0x1234567890;

//关键是要能对错误进行处理,给一个0x123490 照样能得出 0x9034的值,而且, 占内存要小的

printf("macro conversion:%#xn",BSWAP_16(0x123490 ));//要能正确转换

printf("macro conversion:%#xn", BSWAP_32(0x1234567890)); //要能正确转换

printf("-----------------n");

printf("function conversion:%#xn",bswap16(0x123490));

printf("function conversion:%#xn", bswap32(0x1234567890));

return 0;

}