嵌入式c语言面试_嵌入式C面试问答

嵌入式c语言面试

嵌入式C中的热门面试问答 (Top Interview Questions and Answers in Embedded C)

1) What is an Embedded C?

1)什么是嵌入式C?

Embedded C is an extension of C programming language. C programming language is used to develop desktop based applications. While, Embedded C is used to develop micro-controller based applications such as device drivers (memory device driver, camera device driver, WIFI device drive etc.)

嵌入式C是C编程语言的扩展。 C编程语言用于开发基于桌面的应用程序。 同时,嵌入式C用于开发基于微控制器的应用程序,例如设备驱动程序(内存设备驱动程序,相机设备驱动程序,WIFI设备驱动器等)。

Most of the low-level applications which interact with the hardware are written in C programming language, it is also used to develop complete operating systems to electronic devices – which are generally known as firmware’s

与硬件交互的大多数低层应用程序都是用C编程语言编写的,它也用于为电子设备开发完整的操作系统-通常称为固件的



2) What are some common causes for the segmentation fault error in C?

2)C中的分段错误错误有哪些常见原因?

Segmentation fault is a runtime error, which may occur due to some causes (listed below) when the program is running properly. There are some of the cases (causes), when segmentation fault error may occur,

分段错误是一种运行时错误,当程序正常运行时,可能由于某些原因(下面列出)而发生分段错误。 在某些情况下(原因),可能会出现细分错误,

  • Usages of the dereferenced pointer (i.e. a pointer which may not have a valid address/memory location to point).

    取消引用的指针的用法(即,可能没有指向有效地址/内存位置的指针)。

  • If you are trying to access a memory area which is read-only. In that case, the program may return segmentation fault error.

    如果您试图访问只读存储区。 在这种情况下,程序可能会返回分段错误错误。

  • It may also occur when you try to free a memory (using a pointer), which is already freed.

    当您尝试释放(使用指针)已经释放的内存时,也可能发生这种情况。

  • Segmentation fault is the reason to generate stack overflow error in C.

    分段错误是在C中生成堆栈溢出错误的原因。



3) What is ‘stack overflow’ error in C?

3)什么是C语言中的“堆栈溢出”错误?

This error may occur if the program tries to access the memory beyond its available maximum limit. We can also say that if a pointer exceeds the stack limitations (boundaries).

如果程序尝试访问内存超出其可用的最大限制,则可能会发生此错误。 我们也可以说,如果指针超出了堆栈限制(边界)。

When this error occurs program terminates and does not execute further instructions. Therefore, we must be careful while using the pointer and limit boundaries.

发生此错误时,程序终止,并且不执行进一步的指令。 因此,在使用指针和限制边界时必须小心。



4) Why do we use ‘volatile’ keyboard in C?

4)为什么我们在C语言中使用“易失性”键盘?

volatile is used to prevent the compiler to optimize any variable.

volatile用于防止编译器优化任何变量。

When any variable is used frequently, the compiler optimizes it and keeps the variables in his memory (there are some specific memory blocks (registers), from there variable is accessibility is fast) to serve its value faster to the program.

当频繁使用任何变量时,编译器会对其进行优化,并将变量保留在其内存中(有一些特定的内存块(寄存器),从那里可以快速访问变量)以更快地将其值提供给程序。

Therefore, a volatile is used to prevent the compiler for any type of optimization on the variable. Volatile variables are used with those variables which are used to communicate with the computer hardware, signals, handlers etc.

因此,使用一个volatile可以防止编译器对该变量进行任何类型的优化。 易变变量与那些用于与计算机硬件,信号,处理程序等进行通信的变量一起使用。

Consider the given code below:

考虑下面的给定代码:

//global declaration 
unsigned char isDeviceAttached = 0;
//other code segment 

int main()
{
	//system configuration
	//other code segment
	//any loop/condition which is using the variable 
	which(isDeviceAttached)
	{
		//code to handle particular device 
	}
	//other code

	return 0;
}

In the above code, variable isDeviceAtteched is declared globally and is using frequently in the program. The compiler may optimize it and keep its ‘0’ in the memory to serve it faster.

在上面的代码中,变量isDeviceAtteched是全局声明的,并且在程序中经常使用。 编译器可以对其进行优化,并将其“ 0”保留在内存中以更快地提供服务。

But, the value of isDeviceAtteched may change when we attached the device to signal/handler/interrupt function.

但是,当我们将设备连接到信号/处理程序/中断功能时, isDeviceAtteched的值可能会更改。

If we will not use volatile above condition loop’s condition will be false always. And if we make it volatile variable, the actual value will be served to the program.

如果我们不使用volatile以上条件循环,则条件循环始终为假。 如果我们将其设置为volatile变量,则将实际值提供给程序。

volatile variable’s declaration:

volatile变量的声明:

    volatile unsigned char isDeviceAttaced;

volatile variable’s declaration, definition (when it is used it two files)

volatile变量的声明,定义(使用两个文件时)

Definition:

定义:

    volatile unsigned char isDeviceAtteched =0;

Declaration:

宣言:

    extern volatile unsigned char isDeviceAtteched;



5) How to use a variable in a source file which is defined in another source file?

5)如何在另一个源文件中定义的源文件中使用变量?

extern keyboard can be to declare a variable which allows accessing the variable in another file.

extern键盘可以声明一个变量,该变量允许访问另一个文件中的变量。

Let’s understand how it will possible?

让我们了解如何实现这一目标?

file1.c

文件1.c

/*include header file "file2.h"
where variable is declare*/ 
#include "file2.h"

...
/*Access the variable, where it is 
Required*/
if(flgUSB){

    ...;
}

file2.c

文件2.c

/*define variable in global scope*/
unsigned char flgUSB=0;
/*while defining a variable, 
you can assign a default value to it*/

file2.h

文件2.h

/*declare variable in global scope*/
extern unsigned char flgUSB;
/*do not use default value here, it will generate an error*/

Explanation:

说明:

If the variable is defined in "file2.c" and you want to access it in "file1.c", then you have to create a header file like "file2.h".

如果变量是在“ file2.c”中定义的,而您想在“ file1.c”中对其进行访问,则必须创建一个类似于“ file2.h”的头文件。

Declare the variable using extern in the header file and then include it file in "file1.c" or wherever you want to access the variable.

在头文件中使用extern声明变量,然后将其包含在“ file1.c”或您要访问该变量的任何位置。



6)How will you protect a character pointer by some accidentally modification with the pointer address?

6)如何通过意外修改指针地址来保护字符指针?

Constant character pointer (const char*) prevents the unnecessary modifications with the pointer address in the string.

常量字符指针( const char * )防止对字符串中的指针地址进行不必要的修改。

Example:

例:

///declaration 
const char *str;

//we can reassign the pointer
//string will be declared somewhere in the memory and 
//its address will be assigned to the "str" 
str = "Hello world";

//but, we cannot modify the data 
//that is pointing by the pointer

*str = 'P';//not allowed



7) Write a Macro that will accept two arguments and return the largest argument.

7)编写一个将接受两个参数并返回最大参数的宏。

    #define MAX(A,B) ( (A>B) ? A : B )



8) How do you write code for an infinite loop?

8)如何编写无限循环的代码?

An infinite loop is the main component of an embedded application, which is used to run an application all time, an infinite loop can be coded by using while (1) and for(;;)

无限循环是嵌入式应用程序的主要组件,该组件始终用于运行应用程序,可以使用while(1)和for(;;)对无限循环进行编码。

Syntax for an infinite loop by using while(1)

使用while(1)进行无限循环的语法

    while(1)
    {
	    ...;
	    ...;
    }

Syntax for an infinite loop by using for(;;)

使用for(;;)进行无限循环的语法

    for(;;)
    {
	    ...;
	    ...;
    }



9) Write a declaration for ‘an array of 10 pointers to an integer’.

9)为“由10个指向整数的指针组成的数组”编写声明。

An array of 10 pointers to an integer is declared as,

由10个指向整数的指针组成的数组声明为:

    int *ptr[10];



10) Why do we use ‘static’ variable in C?

10)为什么在C语言中使用“静态”变量?

The purposes to use a static variable are:

使用静态变量的目的是:

  • A static variable does not redeclare that means if it is declared in a function it will not redeclare on each function call, therefore, static is able to maintain the value.

    静态变量不会重新声明,这意味着如果在函数中声明它,则不会在每个函数调用时重新声明,因此,静态变量能够保留该值。

  • Its scope is local but it is live until the end of the program.

    它的作用域是本地的,但是一直有效到程序结束。

  • Generally, it is used to count something, for example, there is function openBakAccount() which calls every time when a new account opens in the bank. Then, to count the total number of the opened account, we can declare a static variable in the function and can increase it on each function call.

    通常,它用于计数,例如,有一个函数openBakAccount() ,每次在银行中开设新帐户时都会调用一次。 然后,要计算已开设帐户的总数,我们可以在函数中声明一个静态变量,并可以在每次函数调用时增加它。

翻译自: https://www.includehelp.com/c/embedded-c-interview-questions-and-answers.aspx

嵌入式c语言面试

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值