嵌入式LinuxC笔试题积累(转)

更新时间:2023-05-12 13:42:48 阅读: 评论:0

嵌⼊式LinuxC笔试题积累(转)
/h_armony/article/details/6764811
1.  嵌⼊式系统中断服务⼦程序(ISR)
中断是嵌⼊式系统中重要的组成部分,这导致了很多编译开发商提供⼀种扩展—让标准C⽀持中断。具代表事实是,产⽣了⼀个新的关键字__interrupt。下⾯的代码就使⽤了__interrupt关键字去定义了⼀个中断服务⼦程序(ISR),请评论⼀下这段代码的。
__interrupt double compute_area (double radius)
{ double area = PI * radius * radius;
printf(" Area = %f", area);
return area;
}
1). ISR 不能返回⼀个值。
2). ISR 不能传递参数。
3). 在许多的处理器/编译器中,浮点⼀般都是不可重⼊的。有些处理器/编译器需要让额处的寄存器⼊栈,有些处理器/编译器就是不允许在ISR中做浮点运算。此外,ISR应该是短⽽有效率的,在ISR中做浮点运算是不明智的。
4). 与第三点⼀脉相承,printf()经常有重⼊和性能上的问题。
2.C语⾔中对位的操作,⽐如对a的第三位清0,第四位置1.本来应该会的,⼀犯晕写反了,以后注意!
#define BIT3 (1<<3)
#define BIT4 (1<<4)
a &= ~BIT3;  a |= BIT4;
3.volatile
表⽰这个变量会被意想不到的改变,每次⽤他的时候都会⼩⼼的重新读取⼀遍,不适⽤寄存器保存的副本。
volatile表⽰直接存取原始地址
例:
并⾏设备的硬件寄存器(状态寄存器)
在多线程运⾏的时候共享变量也要时时更新
⼀个中断服务⼦程序中访问到的的⾮⾃动变量(定义变量时默认为⾃动变量,这⾥指全局变量或加修饰的变量)
4.Const:
Const char*p    //p 指向的内容不能被修改
Char const *p;    // p指针指向内容不能修改
Const (char*) p;  //p指针不能修改,p++ 操作会出错
Const type fun();    // 返回值类型为⼀个const type类型,不能修改
Fun( const char *p);    //保护指针,引⽤传递的值不被修改.
类成员函数:中 fun() const;    //表明FUN不能修改成员变量,不调⽤⾮const 成员函数.
5.要求设置⼀绝对地址为0x67a9 的整型变量的值为0xaa66
int *ptr = (int *)0x67a9;
*ptr = 0xaa66;
6、
#include "stdio.h"
int a=0;
int b;
static char c;
int main(int argc,char *argv[])
{
char d=4;
static short e;
a++;
b=100;
c=(char)++a;
e=(++d)++;
printf("a=%d, b=%d, c=%d, d= %d, e=%d",a,b,c,d,e);
return 0;
}
a) 写出程序输出
答案:a = 2, b = 100, c = 2, d = 6, e = 5
7a)对于整形变量A=0x12345678,请画出在little endian及big endian的⽅式下在内存中是如何存储的?
little endian        big endian 刚好反过来
⾼地址--〉 0x12      低地址--〉 0x12
0x34                          0x34
0x56                          0x56
低地址--〉 0x78      ⾼地址--〉 0x78
记忆⽅法:
⼩端模式:地址的增长顺序与值的增长顺序相同(x86为⼩端模式)
⼤端模式:地址的增长顺序与值的增长顺序相反
b)在ARM系统中,函数调⽤的时候,参数是通过哪种⽅式传递的?
参数<=4时候,通过R0~R3传递,>4的通过压栈⽅式传递
8 .1请实现内存复制函数void* memcpy(void* dst, void* src, int count)。
[cpp]
1. <span >#include <stdio.h>
2. #include <asrt.h>
3. void* mem_cpy(void *dst, const void *src, int count)  //参数定义为空指针类型,并且源地址内容不应该被改变,因此⽤const修饰
4. {
5. /*
6.    if(NULL==dst || NULL==src)
7.        return dst;
8. */
9.    asrt(dst);  //若传⼊参数不为真,程序退出
10.    asrt(src);
11.    while(count--)
12.    {
13.        *(char*)dst = *(char*)src;    //强制转化为字符指针类型,并进⾏内容复制
14.        dst = (char*)dst +1;
15.        src = (char*)src +1;
16.    }
17.    return dst;
18. }
19.
20. int main(int argc, char* argv[])
21. {
22.    char From[100] ="Hello baby!";
23.    char To[100] = {0};
24.    mem_cpy(To,From,100);      //前两个参数位⼦不要弄错
25.    printf("%s\n",From);      //输出字符串
26.    printf("%s\n",To);
27.    return 0;
28.
29. }
30. </span>
精简版
[cpp]
1. char * strcpy(char * strDest,const char * strSrc)
2. {
3.    char * strDestCopy=strDest;
4.    asrt( (strDest!=NULL) && (strSrc!=NULL) ); // #include <asrt.h>
5.
6.    while ((*strDest++=*strSrc++)!='\0');
7.    return strDestCopy;
8. }
8.2、不使⽤库函数,编写函数int strcmp(char *source, char *dest)相等返回0,不等返回-1; [cpp]
1. #include "stdafx.h"
2. #include <asrt.h>
3. #include <string.h>
4.
5. int strcmp(char *source, char *dest)
6. {
7.
8.    int i;
9.    asrt((NULL != source)&&(NULL != dest));  //判断指针是否为空
10.    if(strlen(source) != strlen(dest))  //判断两字符串长度时候相等,不相等肯定不相等,直接退出
11.    {
12.        return -1;
13.    }
14.    for(i = 0;i < strlen(source);i++)  //利⽤指针,将两字符串逐字⽐较
15.    {
16.        if(*(source + i) != *(dest + i))  //如果不相等,退出
17.            return -1;
18.    }
19.
20.    return 0;
21. }
22.
23. int main(int argc, char* argv[])
24. {
25.    static char a[2][10];        //定义⼀个⼆维数组
26.    int N;
27.    printf("input two string\n");
28.    for(int i=0;i<2;i++)
29.    {
30.        scanf("%s",&a[i][0]);    //输⼊两个字符串
31.    }
32.
33.    N=strcmp(&a[0][0],&a[1][0]);  //调⽤⾃定义函数
34.    if(N == -1)
35.        printf(" two different string\n");
36.    el
37.        printf("same string\n");
38.    return 0;
39. }
9.1、在数组定义int a[2][2]={{3},{2,3}};则a[0][1]的值为0。(对)
9.2、
[cpp]
1. <span >#include <stdio.h>
2. intmain(int argc,char * argv[])
3. {
4. int a [3][2]={(0,1),(2,3),(4,5)};
5. int *p;
6. p=a [0];
7. printf("%d",p[0]);
8. }</span>
问打印出来结果是多少?
答案:1.
分析:花括号⾥嵌套的是⼩括号⽽不是花括号!这⾥是花括号⾥⾯嵌套了逗号表达式!其实这个赋值就相当于int a [3][2]={ 1, 3, 5};
10.0 输⼊任意字符串,打印输出其逆序:
[cpp]
1. <span >#include "stdafx.h"
2. #include<stdio.h>
3. #include <stdlib.h>
4. #include<string.h>
5.
6. void s_back(char *p)
7. {
8.    int i=0;
9.    char *ps;
10.    char *pe;
11.    char temp;
12.    if(!p)
13.        return;
14.    ps=p;
15.    while(*p!=0)
16.        p++; //最后⼀个字符满⾜要求,p++后指针指向了字符串外的⼀个地址;
17.    pe=p;
18.
19.    for(i=0;i<(pe-ps)/2;i++)
20.    {
21.        temp=*(ps+i);
22.        *(ps+i)=*(pe-i-1); //由于pe指向的是字符串外的⼀个地址,因此这⾥还必须减⼀
23.        *(pe-i-1)=temp;
24.    }
25. }
26.
27. int main(void)
28. {
29.    printf("input:");
30.    char *p=(char *)malloc(10);
31.    scanf("%s",p);
32.
33.    s_back(p);
34.    printf("output:%s\n",p);
35.    free(p);
36.    return 0;
37. }</span>
11、写⼀函数int fun(char *p)判断⼀字符串是否为回⽂,是返回1,不是返回0,出错返回-1 [cpp]
1. <span >#include "stdafx.h"
2. #include <asrt.h>
3. #include <string.h>
4.
5. int fun(char *p)
6. {
7.    int len;
8.    if(NULL == p)      //检测指针是否有效
9.        return -1;
10.    len=strlen(p)-1;    printf("len = %d\n",len);
11.    for(int i=0;i<(len/2);i++)
12.    {
13.        printf("*(p+%d)=%c\n *(p+len-%d)=%c\n",i,*(p+i),i,*(p+len-i-1));
14.        if(*(p+i) != *(p+len-i-1))  //判断⾸尾是否依次相等,即是否为回⽂
15.            return 0;              //不是回⽂返回0
16.    }
17.    return 1;        //是回⽂返回1
18.
19.
20. }
21.
22. int main(int argc, char* argv[])
23. {
24.    int R;
25.    char ch[]="abcdedcba";
26.    R=fun(ch);
27.    switch (R)
28.    {
29.        ca -1:
30.            printf("error!\n");
31.            break;
32.        ca 0:
33.            printf("it's not huiweng\n");
34.            break;
35.        ca 1:
36.            printf("it's huiweng\n");
37.            break;
38.        default:
39.            break;
40.    }
41.    return 0;
42. }
43.
44. </span>
/s/blog_6050059b0100dbj3.html linux C程序员经典⾯试题⼀
(2009-05-27 10:58:16)
先发基本问题,再发编程问题..........
想成为嵌⼊式程序员应知道的0x10个基本问题:
预处理器(Preprocessor)

本文发布于:2023-05-12 13:42:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/888173.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:指针   不能   变量   相等
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图