嵌入式面试总结-C语言函数相关

来源:互联网 发布:java notify notifyall 编辑:程序博客网 时间:2024/05/17 01:26

部分参考Bruce.yang大神的博客   http://blog.csdn.net/morixinguan

1、 分离16进制的高低位

#include<stdio.h>#include<stdlib.h>intmain(void) {     unsigned int  temp = 0x10 ;     unsigned int  high = (temp - temp % 0x10) / 0x10 ;     unsigned int  low = temp % 0x10 ;     printf("high:%u    low:%u\n",high , low) ;      system("PAUSE");          return 0;  }   

2、 分离unsigned int的高低位

#include<stdio.h>#include<stdlib.h>intmain(void) {     unsigned int offset = 0x1234 ;     unsigned int high = 0 ;     unsigned int low = 0 ;     high = ((offset >> 8 )&0xff);     low = (offset&0xff) ;     printf("high = %p->%d   low = %p->%d\n",high , high ,low,low);       system("PAUSE");           return 0;  }   

3、 一个字节16进制组合成2字节16进制

#include<stdio.h>#include<stdlib.h>intRecordBuffer[10]; intbuffer_write(unsigned int *buffer , int size_to_write) ; intmain(void) {     unsigned int buffer[] ={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};     buffer_write(buffer , 10);     int i ;     for(i = 0 ; i < 10 ; i++)         printf("Recordbuffer[%d]:%p\n",i , RecordBuffer[i]);     system("PAUSE");           return 0;  }  intbuffer_write(unsigned int *buffer , int size_to_write) {     int *p = (int *)buffer ;     int i ;     for(i = 0 ; i < size_to_write ;i +=2)      {         RecordBuffer[i/2] = *(p+i)|(*(p+i+1)<< 8) ;               }           }  

4、 无符号数转换成ascii

#include<stdio.h>#include<stdlib.h>#definetoascii(c) (((unsigned char)(c))&0x7f) //因为0x7f代表127,ascii码从0开始到127结束 intmain(void) {     int ret = 0 ;     ret = toascii('a');  //小a的ascii     printf("a的ascii码:%d\n",ret);     ret = toascii('A');  //大a的ascii     printf("A的ascii码:%d\n",ret);     ret = toascii('z');  //小z的ascii     printf("z的ascii码:%d\n",ret);     ret = toascii('Z');  //大Z的ascii     printf("Z的ascii码:%d\n",ret);     system("PAUSE");           return 0;  }  

5、 16进制数字高低位数据交换

#include<stdio.h>#include<stdlib.h>#definetoascii(c) (((unsigned char)(c))&0x7f) //因为0x7f代表127,ascii码从0x7f开始到0xff结束 //system("PAUSE");    //将一个8位数高低4位交换  static inlineunsigned char bswap_8(unsigned char v) {    return ((v & 0xff) << 4) | (v >> 4) ;  //将参数(v & 0xff)<< 4 相当于放到高位, v>> 4 位相当于放在低位 }                                               //以下两个代码分析雷同 //将一个16位数高低8位交换  static inlineunsigned short bswap_16(unsigned short v) {     return ((v & 0xff) << 8) | (v>> 8); } //将一个32位数高低16位交换  static inlineunsigned int bswap_32(unsigned int v) {     return ((v & 0xff) << 24) | ((v& 0xff00) << 8) |         ((v & 0xff0000) >> 8) | (v>> 24); } intmain(void) {      unsigned short v = 0x1000 ;     printf("1、\n原来的v:%d\n",v);     printf("16位数高低8位转化后的v:%d(0x%x)=========>0x%x--->%d\n",v,v,bswap_16(v),bswap_16(v)) ;     unsigned char  a = 0x0a ;     printf("\n2、\n原来的a:%d\n",a);     printf("8位数高低4位转化后的a:%d(0x%x)==========>0x%x--->%d\n",a,a,bswap_8(a),bswap_8(a)) ;        unsigned int  b = 0x00001111;      printf("\n3、\n原来的b:%d\n",b);     printf("32位数高低16位转化后的b:%d(0x%x)========>0x%x--->%d\n",b,b,bswap_32(b),bswap_32(b)) ;     system("PAUSE");          return 0 ; }  

6、 字符串操作

#include<stdio.h>#include<stdlib.h>#definetoascii(c) (((unsigned char)(c))&0x7f) //因为0x7f代表127,ascii码从0x7f开始到0xff结束 //system("PAUSE");    #include<string.h> #define ucharunsigned char  #defineuint  unsigned int  //删除字符串中所有的数字  voiddel_str_Num(char* str) {     static int i , j;     while(str[i]!='\0')     {         if(str[i]>'9'||str[i]<'0')             str[j++]=str[i];         i++;     }     str[j]='\0'; } //删除字符串中的第一个字符  static char*ltrim(char *s, char c) {   while(*s!=0&&*s==c)         s++;   return s; }  //查找并删除字符串中指定的任意字符  static char*delstr_c(char s[100] , char c) {     char *p , *q ;     for(p = s , q = s ; *p != '\0' ; p++)         if(*p != c)*q++ = *p ;      *q = *p ;     return s ; }   /*将字符串s中出现的字符c删除*/  voiddel_str_c(char s[],int c)  {      inti,j;      for (i = 0, j = 0; s[i] != '\0'; i++)      {          if (s[i] != c)          {              s[j++] = s[i];          }      }      s[j] = '\0';   }  //查找字符串中的字符并将对应的字符删除  char*strdel_c(char *s,char c)  {   char *p=s,*q=s;   for(;*p;p++)   {     if(*p != c )     {        if(q == p)           q++ ;         else            *q++ = *p ;     }   }   *q = '\0';   return s; } //将字符串左右翻转  char*strfilp(char* str)   {       //h指向str的头部     char* h = str;           char* t = str;       char ch;          //t指向s的尾部      while(*t++) ;     t--;   //与t++抵消       t--;   //回跳过结束符'\0'        //当h和t未重合时,交换它们所指向的字符     while(h < t)       {           ch = *h;           *h++ = *t;   //首尾移动         *t-- = ch;         }          return(str);   }    int main() {     char str[20] = "23.3hhlo965";     char *str1 = "hello";      del_str_c(str , '.');  //删除该字符串中的.     printf("%s\n",str);     del_str_Num(str);      //删除该字符串中的所有数字     printf("%s\n",str);     strfilp(str);          //将字符串左右翻转     printf("%s\n",str);     system("PAUSE");          return 0; }


7、数据段、代码段

#include<stdio.h> #include<stdlib.h> int BSS ;       //位于BSS段,存放在程序组未初始化的内存区域  int data = 100;  //位于数据段,存放在程序中已经初始化的内存区域  static int y ;//静态区  int stack(void);  intmain(void) {     static int k  ;  //静态区      int i , j ;   //栈区  内存自动申请自动释放       int *p = NULL ;        p = malloc(1024) ; //堆区  内存手动申请手动释放      free(p);     return 0 ; } intstack(void) {     //栈区      int i ;      return 0 ; }  

8、将字符串中的数字返回为整数型

#include<stdio.h>#include<stdlib.h>//system("PAUSE");    typedef int U32;  U32 String2Dec(const char *pstr ) {     char ch;     U32 value;     value = 0;     //从字符串的第一个字符遍历到'\0'      while( *pstr != '\0' )     {         //获取字符          ch = *pstr++;         //判断字符是否在0-9这个范围          if( ch >= '0' && ch <='9' )         {             //ch-'0'相当于将字符转换为整数              value = value * 10 + ch - '0';         }     }     //返回      return value; } intmain(void) {     char *pstr = "123a456" ;     int num = String2Dec(pstr);     printf("num:%d\n",num);     system("PAUSE");           }  



9、使用strstratoi的结合实现一个C语言获取文件中数据的工具

#include<stdio.h> #include<fcntl.h> #include<string.h> #include<stdlib.h> #include<unistd.h> intget_buf_data(char *buf,char *data) {     char *p1 =NULL,* p2=NULL;     int num =0;     p1 = buf;     p2 = strstr(p1,data);     if(p2 == NULL)     {        printf("%s no find  %s ---%s\r\n",__FUNCTION__ ,buf,data);         return 0;     }     p1 = p2+strlen(data);     num = atoi(p1);     return num;  } intmain(void) {     int fd = -1 ;      char buf[1024];     fd = open("abc.txt",O_RDWR);     if(-1 == fd)     {         printf("open fair!\n");         return -1 ;     }     memset(buf,0,sizeof(buf));     read(fd,buf,1024);     close(fd);     int num =get_buf_data(buf,"student_num:");     printf("num:%d\n",num);     system("PAUSE");          return 0 ; }   

原创粉丝点击