What Is Embedded C Programming? How Is Embedded C Different From C Language?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8
At a glance
Powered by AI
Embedded C is an extension of C programming used for developing embedded systems with limited resources. It supports hardware operations and addressing.

A startup code is a piece of code that runs before main() to initialize the application platform in assembly language.

ISR stands for Interrupt Service Routine. It is a procedure called when an interrupt occurs to handle the interrupt event.

1. What is Embedded C Programming?

How is Embedded C different


from C language?

Embedded C is a programming language that is an extension of C programming. It


uses the same syntax as C and it is called “embedded” because it is used widely in
embedded systems. Embedded C supports I/O hardware operations and
addressing, fixed-point arithmetic operations, memory/address space access, and
various other features that are required to develop fool-proof embedded systems.

Following are the differences between traditional C language and Embedded C:

C Language Embedded C Language


It is of native development
It is used for cross-development purposes
nature
C is independent of hardware Embedded C is dependent on the hardware
and its underlying architecture architecture.
C is mainly used for developing Embedded C is used in embedded systems that
desktop applications. have limited resources like ROM, RAM, etc.

2. What do you understand by startup code?

A startup code is that piece of code that is called before the execution of the main
function. This is used for creating a basic platform for the application and it is
written in assembly language.

3. What is ISR?

ISR expands to Interrupt Service Routines. These are the procedures stored at a
particular memory location and are called when certain interrupts occur. Interrupt
refers to the signal sent to the processor that indicates there is a high-priority event
that requires immediate attention. The processor suspends the normal flow of the
program, executes the instructions in ISR to cater for the high priority event. Post
execution of the ISR, the normal flow of the program resumes. The following
diagrams represent the flow of ISR.
You can download a PDF version of Embedded C Interview Questions.

Download PDF

4. What is Void Pointer in Embedded C and why is it used?

Void pointers are those pointers that point to a variable of any type. It is a generic
pointer as it is not dependent on any of the inbuilt or user-defined data types while
referencing. During dereferencing of the pointer, we require the correct data type
to which the data needs to be dereferenced.

For Example:

int num1 = 20; //variable of int datatype


void *ptr; //Void Pointer
*ptr = &num1; //Point the pointer to int data
print("%d",(*(int*)ptr)); //Dereferencing requires specific data type

char c = 'a';
*ptr = &c; //Same void pointer can be used to point to data of
different type -> reusability
print("%c",(*(char*)ptr));

Void pointers are used mainly because of their nature of re-usability. It is reusable
because any type of data can be stored.

5. Why do we use the volatile keyword?

The volatile keyword is mainly used for preventing a compiler from optimizing a


variable that might change its behaviour unexpectedly post the optimization.
Consider a scenario where we have a variable where there is a possibility of its
value getting updated by some event or a signal, then we need to tell the compiler
not to optimize it and load that variable every time it is called. To inform the
compiler, we use the keyword volatile at the time of variable declaration.

// Declaring volatile variable - SYNTAX


// volatile datatype variable_name;
volatile int x;

Here, x is an integer variable that is defined as a volatile variable.

6. What are the differences between the const and volatile qualifiers
in embedded C?

const volatile
The keyword “const” is enforced by
The keyword “volatile” tells the compiler to
the compiler and tells it that no
not perform any optimization on the variables
changes can be made to the value of
and not to assume anything about the
that object/variable during program
variables against which it is declared.
execution.
Example: const int x=20;, here if the Example: volatile int x;, here the compiler
program attempts to modify the value is told to not assume anything regarding the
of x, then there would be a compiler variable x and avoid performing optimizations
error as there is const keyword on it. Every time the compiler encounters the
assigned which makes the variable x variable, fetch it from the memory it is
non-modifiable. assigned to.

7. What Is Concatenation Operator in Embedded C?

The Concatenation operator is indicated by the usage of ##. It is used in macros to


perform concatenation of the arguments in the macro. We need to keep note that
only the arguments are concatenated, not the values of those arguments.

For example, if we have the following piece of code:


#define CUSTOM_MACRO(x, y) x##y

main(){

int xValue = 20;


printf(“%d”, CUSTOM_MACRO(x, Value)); //Prints 20

We can think of it like this if arguments x and y are passed, then the macro just
returns xy -> The concatenation of x and y.

8. What do you understand by Interrupt Latency?

Interrupt latency refers to the time taken by ISR to respond to the interrupt. The
lesser the latency faster is the response to the interrupt event.

9. How will you use a variable defined in source file1 inside source
file2?
We can achieve this by making use of the “extern” keyboard. It allows the variable
to be accessible from one file to another. This can be handled more cleanly by
creating a header file that just consists of extern variable declarations. This header
file is then included in the source files which uses the extern variables. Consider an
example where we have a header file named variables.h and a source file
named sc_file.c.

/* variables.h*/
extern int global_variable_x;
/* sc_file.c*/
#include "variables.h" /* Header variables included */
#include <stdio.h>
void demoFunction(void)
{
printf("Value of Global Variable X: %d\n", global_variable_x++);
}

10. What do you understand by segmentation fault?

A segmentation fault occurs most commonly and often leads to crashes in the
programs. It occurs when a program instruction tries to access a memory address
that is prohibited from getting accessed.

11. What are the differences between Inline and Macro Function?

Category Macro Function Inline Function


Compile- Macro functions are
time expanded by the Inline functions are expanded by the
expansion preprocessor at the compile compiler.
time.
Argument Expressions passed to the
Expressions passed to Inline functions get
Evaluation Macro functions might get
evaluated once.
evaluated more than once.
Parameter Macro functions do not
Inline functions follow strict data type
Checking follow strict parameter data
checking of the parameters.
type checking.
Ease of Macro functions are hard to
debugging debug because it is replaced
Easier to debug inline functions which is
by the pre-processor as a
why it is recommended to be used over
textual representation which
macro functions.
is not visible in the source
code.
Example #define SQUARENUM(A) A * inline squareNum(int A){return A *
A -> The macro functions are A;} -> If we
expanded at compile time. have printf(squareNum(3+2));, the
Hence, if we pass, the output arguments to the function are evaluated
Category Macro Function Inline Function
will be evaluated
to 3+2*3+2 which gets
first to 5 and passed to the function,
evaluated to 11. This might
which returns a square of 5 = 25.
not be as per our
expectations.

12. Is it possible for a variable to be both volatile and const?

The const keyword is used when we want to ensure that the variable value should
not be changed. However, the value can still be changed due to external interrupts
or events. So, we can use const with volatile keywords and it won’t cause any
problem.

13. Is it possible to declare a static variable in a header file?

Variables defined with static are initialized once and persists until the end of the
program and are local only to the block it is defined. A static variables declaration
requires definition. It can be defined in a header file. But if we do so, a private copy
of the variable of the header file will be present in each source file the header is
included. This is not preferred and hence it is not recommended to use static
variables in a header file.

14. What do you understand by the pre-decrement and post-


decrement operators?

The Pre-decrement operator ( --operand) is used for decrementing the value of the
variable by 1 before assigning the variable value.

#include < stdio.h >


int main(){
int x = 100, y;

y = --x; //pre-decrememt operators -- first decrements the value


and then it is assigned

printf("y = %d\n", y); // Prints 99


printf("x = %d\n", x); // Prints 99
return 0;
}

The Post-decrement operator ( operand--) is used for decrementing the value of a


variable by 1 after assigning the variable value.

#include < stdio.h >


int main(){
int x = 100, y;
y = x--; //post-decrememt operators -- first assigns the value and
then it is decremented

printf("y = %d\n", y); // Prints 100


printf("x = %d\n", x); // Prints 99
return 0;
}

15. What is a reentrant function?

A function is called reentrant if the function can be interrupted in the middle of the
execution and be safely called again (re-entered) to complete the execution. The
interruption can be in the form of external events or signals or internal signals like
call or jump. The reentrant function resumes at the point where the execution was
left off and proceeds to completion.

Embedded C Interview Questions for Experienced


16. What kind of loop is better - Count up from zero or Count Down
to zero?

Loops that involve count down to zero are better than count-up loops. This is
because the compiler can optimize the comparison to zero at the time of loop
termination. The processors need not have to load both the loop variable and the
maximum value for comparison due to the optimization. Hence, count down to 0
loops are always better.

17. What do you understand by a null pointer in Embedded C?

A null pointer is a pointer that does not point to any valid memory location. It is
defined to ensure that the pointer should not be used to modify anything as it is
invalid. If no address is assigned to the pointer, it is set to NULL.

Syntax:

data_type *pointer_name = NULL;

One of the uses of the null pointer is that once the memory allocated to a pointer is
freed up, we will be using NULL to assign to the pointer so that it does not point to
any garbage locations.

18. Following are some incomplete declarations, what do each of


them mean?
1. const int x;
2. int const x;
3. const int *x;
4. int * const x;
5. int const * x const;

You might also like