面試題1:
不用sizeof來求出系統的位元,是16位還是32位?
思路:是根據系統的位元用一個數進行測試!
源碼:
#include <stdio.h>
int main(int argc,char *argv[])
...{
unsigned int a= ~0;
if(a>65536)
...{
printf("system is 32 bit ");
}
else
...{
printf("system is 16 bit ");
}
return 0;
}
總結:如果用sizeof的程式
#include <stdio.h>
int main(int argc,char *argv[])
...{
printf("system is bits %d ",sizeof(unsigned long));
return 0;
}
為什麼要用long的大小,來判斷系統的位元組的大小,因為這個和ALU有關。
在系統中怎麼查看呢?用命令
在ubuntu下,核心是2.6.15,可以用命令查看宏定義
getconf WORD_BIT
就可以查看當前系統的位元
面試題2
實現char *strstr(char *source ,char *dist)函數
/**//*
* 要實現 char *strstr(char *,char *)功能
* */
#include <stdio.h>
char *strstd(char *source,char *dist)
...{
char *movesource,*movedist,*temp;
int length=0,i=0;
movesource=NULL;
movedist=NULL;
temp=dist;
while(*temp++!='')
length++;
while(*source!='')
...{
if(*source==*dist)
...{
movesource=source;
movedist=dist;
while(*movedist!='')
...{
if(*movedist==*movesource)
...{
movedist++;
movesource++;
i++;
if(i==length)
...{
return source;
}
}
else
...{
return NULL;
}
}
}
source++;
i=0;
}
}
int main(int argc,char *argv[])
...{
char *a="wangyong";
char *b="dd";
char *result;
result=strstd(a,b);
if(result!=NULL)
printf("result==%s ",result);
}
面試題3