C语言面试题

来源:互联网 发布:json 美化 站长工具 编辑:程序博客网 时间:2024/06/13 12:36

0. 判断little-endian, big-endian。

#include <stdio.h>#include <stdlib.h>#include <stdbool.h>bool is_little_endian(void) {  int i;  if( *((char *)&i) == 1)    return true;  else    return false;}

1. 可重入函数:

void strcpy(char *dest, char *src) {

  while(*dest = *src) {

    ;

  }

}


不可重入函数:

int temp;

void swap(int *x, int *y) {

  temp = *x;

  *x = *y;

  *y = temp;

}

使上面函数可重入的方法:

1)把temp变为局部变量;

2)调用swap之前关中断;

3)用信号量或锁保护临界区;

****************************************

1. 冒泡排序

void bubble(int array[], int n) {
int i, j, tmp;
bool swapped = true;

for(i=0;i<n-1 && swapped;i++) {
swapped = false;
for(j=0;j<n-i-1 && ;j++) {
if(a[j] > a[j+1]) {
tmp = a[j];
a[j+1] = a[j];
a[j] = tmp;

swapped = true;
}
}
}

return;
}


参考:http://m.blog.csdn.net/blog/ysjian_pingcx/8653732


2. 单链表逆序打印
递归实现:
void print_reverse_list(node *head)
{
assert(head);


  if(!head->next)
    printf("%d\n", head->data);
  else {
    print_reverse_list(head->next);
    printf("%d\n", head->data);
  }
}
堆栈实现:
void print_reverse_list(node *head)
{
stack s;
node *tmp;


assert(head);


do {
push(head->data, &s);
} while(head->next);

while(!stackEmpty(s)) {
tmp->data = pop(s);
printf("%d\n", tmp->data);
}
}
数组实现:
void print_reverse_list(node *head)
{
int i = 0;
int array[MAX];
node *tmp = head->next;


assert(head);
while(tmp->next) {
array[i++] = tmp->data;
}
array[i] = tmp->data;

for(j=i;j>=0;j--)
printf("%d\n", array[j]);
}
先逆序再打印:
node* print_reverse_list(node *head) {
  node *prev=NULL;
  node *next, *tmp;
  
  if(!head || !head->next)
  return head;
  
  while(head) {
  next = head->next;
  head->next = prev;
  prev = head;
  head = next;
  }
  
  head = prev;
  tmp = head;
  do {
  printf("%d\n", tmp->data);
  tmp = tmp->next;
  }while(tmp->next)
  
  return head;  
}


3. container_of(ptr, type, memeber)
#define offsetof(type, member) (size_t)(&((type *)0->member))
#define container_of(ptr, type, member) ({\
const typeof(((type *)0)->member) * _mptr = (ptr); \
(type *)(_mptr - offsetof(type, member));


参考:
http://velep.com/archives/818.html
http://yaronspace.cn/blog/archives/1026


4. sizeof & strlen
参考: http://blog.csdn.net/lanpishu1984/article/details/4532847


5. memcpy
void *memcpy(void *dest, const void *src, size_t size) {
assert(!dest && !src);

byte *pTo = (byte *)dest;
byte *pFrom = (byte *)src;

while(size--) {
*pTo++ = *pFrom++;
}

return pTo;
}
参考:http://www.crifan.com/two_implementations_of_memcpy/


6. 最大组合
int maxSum(int *a, int n) {
int max = a[0];
int sum = 0;

for(i=0;i<n;i++) {
if(sum >= 0)
sum += a[i];
else
sum = a[i];
 
 if(sum > max)
  max = sum;
}


return max;
}
参考: http://www.cnblogs.com/waytofall/archive/2012/04/10/2439820.html


7. 写个程序,演示堆栈溢出
void foo(void) {
static int a = 3;

if(--a)
foo();


printf("%d\n", a);
}


void main(void) {
foo();
printf("Next\n");
foo();
printf("End\n");
}


Output:
0
0
0
Next
Segmentation fault(core dumped).


After removing the "static" in definition of "a":
Output:
Segmentation fault(core dumped).


8. 单链表逆序
node *reverse(node *head) {
node *prev = NULL;
node *next;

if(!head || !head->next)
return head;

while(head) {
next = head->next;
head->next = prev;
prev = head;
head = next;
}

return prev;
}


9. 把这个变成安全线程: 
static count;
   count++;


static atomic_t flag = ATOMIC_INIT(0);
atomic_set(&flag, 1);
atomic_set(&flag, 0);
atomic_read(&flag)


spinlock_t lock = SPIN_LOCK_UNLOCKED;
spin_lock(&lock);
spin_unlock(&lock);
spin_lock_irqsave(&lock, flags);
spin_unlock_irqrestore(&lock, flags);
spin_trylock(&lock);


static DECLARE_MUTEX(sem);
if(down_interruptible(&sem))
...
up(&sem);


10. RGB565 -> YUV420:
YUV和RGB的转换公式如下:
     Y = 0.299R + 0.587G + 0.114B 
   U = -0.147R - 0.289G + 0.436B 
   V = 0.615R - 0.515G - 0.100B 
   R = Y + 1.14V 
   G = Y - 0.39U - 0.58V 
   B = Y + 2.03U 
0 0