[实战演练]格科微电子软件类笔试题目

1. 写一个C函数,判断计算机系统是大端模式(big endian)还是小端模式(small endian)。

答案参考自:http://blog.csdn.net/ce123_zhouwei/article/details/6971544

第一种思路:short int 强制转换为char,看转换的结果。代码如下:

void test1()
{
    short int x;
    char x0, x1;
    x = 0x1122;
    x0 = ((char*)&x)[0];//低地址
    x1 = ((char*)&x)[1];//高地址
    if(0x11 == x0)
    {
        printf("big endian
");
    }else
    {
        printf("little endian
");
    }
}

第二种思路:使用union类型

void test2()
{
    union aword
    {
        unsigned int a;
        char b[4];
    } c;
    
    c.a=1;
    if(0==c.b[0])
    {
        printf("big endian
");
    }else
    {
        printf("little endian
");
    }
}

2. 给定一个单向链表的第一个结点,反转链表。

代码如下:

typedef struct Node{
    int val;
    Node *next;
} Node;

Node* inverseList(Node *first){
    if(first==NULL || first->next == NULL)
        return first;
    Node *pre = NULL;
    Node *curr = first;
    Node *nex = first->next;
    while(nex!=NULL){
        curr->next = pre;
        pre = curr;
        curr = nex;
        nex = nex->next;
    }
    curr->next = pre;
    return curr;
}

3. 解释volatile关键字的作用。

答:volatile属于类型修饰符,在多线程程序中使用较多,一般用来修饰被多个线程共享且修改的变量。被volatile修饰的变量,指令不会因为编译器优化而省略,这个关键字告诉程序,该变量随时可能会被改变,每次访问变量必须重新在内存读取变量的值,而不是在寄存器中提取变量值。嵌入式系统的程序用到volatile机会更多一些。

4. int a=1, b=2, c=3, d=4, 则 a<b?a:b<c?c:d 的结果是什么?

答:结果是 1,原题目相当于 (a<b)?(a):(b<c?c:d); 由于a<b是成立的,结果直接返回a的值,也就是1,后面的b<c?c:d不需要执行。

原文地址:https://www.cnblogs.com/xuning/p/3383017.html