Home » C programming language

Embedded C Interview Questions and Answers

Embedded C programming Interview Questions and Answers: This page contains some of the top Interview questions and Answers of Embedded C programming language.
Submitted by IncludeHelp, on May 24, 2018

Top Interview Questions and Answers in Embedded C

1) What is an Embedded 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.)

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


2) What are some common causes for the segmentation fault error in 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.

3) What is ‘stack overflow’ error in 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?

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

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.

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.

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

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 variable’s declaration:

    volatile unsigned char isDeviceAttaced;

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

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?

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

Let’s understand how it will possible?

file1.c

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

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

    ...;
}

file2.c

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

file2.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".

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.


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

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

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.

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

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

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(;;)

Syntax for an infinite loop by using while(1)

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

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

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

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

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

    int *ptr[10];

10) Why do we use ‘static’ variable in 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.



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.