Embedded System Interview Questions
Const & Volatile
Const and Volatile are not opposite of each other!
Const - A variable whose value can not be changed, that is a
constant. A programmer can not change the value of a variable
declared as constant later in program. If it is not initialized at
deceleration, it can not be assigned/initialized later.
An integer constant can be defined as:
const int myConst = 10;
int const myConst = 10;
Both the declaration have the same meaning. But in case of
pointers, things are different.
const int *myPtr = (int *)0xFFFFFF04; /* 1 */
int const *myPtr = (int *)0xFFFFFF05; /* 2 */
int* const myPtr = (int *)0xFFFFFF06; /* 3 */
Declaration 1 & 2 are the same and indicates the myPtr is an
ordinary integer pointer and the integer pointed by it can not
be modified. As a programmer, I can modify myPtr as:
int anInt = 10;
,myPtr = &anInt;
But I can not do:
*myPtr = anInt;
However, the deceleration 3 defines a pointer myPtr that can
not be modified but the value pointer by it can be modified.
Const, actually volatile also, variables are useful in real-time C
programs. A const variable may never be initialized. An use
case of this can be a the address of memory mapped I/O which
is read only.
Volatile - Linux for you and Netrino articles have clearly
explainedvolatile keyword. So, I will just be summarizing it
here.
When a programmer declares a variable volatile, (s)he informs
the compiler that the variable at any time during the life of
program can change its value, so be careful while optimizing
the code. In reference to the example of memory mapped I/O, I
would define a pointer holding the address as a const variable
but the value pointed by as volatile.
volatile int* const memIO = 0xFFFFFF05;
Some more declerations.
int volatile alcohol;
,volatile int alcohol; /* are the same - alcohol is volatile */
Now the pointer and volatile.
volatile int *myPtr; /* ordinary integer pointer to
volatile integer data*/
int* volatile myPtr; /* volatile integer pointer to
usual integer data*/
volatile int* volatile myPtr; /* volatile integer pointer to
volatile integer data*/
Now do NOT confuse in an interview.
Usage of volatile variable (as pointer in Netrino article)
Memory-mapped peripheral registers
Global variables modified by an interrupt service routine
Global variables accessed by multiple tasks within a multi-
threaded application
What does volatile long * const timer = 0x0000ABCD mean?
, C Questions
As a C programmer you would like to give some thought to
following C question.
1. Can structures be passed to the functions by value?
2. Why cannot arrays be passed by values to functions?
3. What are advantages and disadvantages of using macro
and inline functions?
4. What happens when recursion functions are declared
inline?
5. What is scope of static variables?
6. What is the output of printf("\nab\bcd\ref"); C
statement?
7. #define cat(x,y) x##y concatenates x to y. But
cat(cat(1,2),3) does not expand but gives preprocessor
warning. Why?
8. Is it possible to define a constant volatile variable?
9. Is it possible to define a volatile pointer?
Const & Volatile
Const and Volatile are not opposite of each other!
Const - A variable whose value can not be changed, that is a
constant. A programmer can not change the value of a variable
declared as constant later in program. If it is not initialized at
deceleration, it can not be assigned/initialized later.
An integer constant can be defined as:
const int myConst = 10;
int const myConst = 10;
Both the declaration have the same meaning. But in case of
pointers, things are different.
const int *myPtr = (int *)0xFFFFFF04; /* 1 */
int const *myPtr = (int *)0xFFFFFF05; /* 2 */
int* const myPtr = (int *)0xFFFFFF06; /* 3 */
Declaration 1 & 2 are the same and indicates the myPtr is an
ordinary integer pointer and the integer pointed by it can not
be modified. As a programmer, I can modify myPtr as:
int anInt = 10;
,myPtr = &anInt;
But I can not do:
*myPtr = anInt;
However, the deceleration 3 defines a pointer myPtr that can
not be modified but the value pointer by it can be modified.
Const, actually volatile also, variables are useful in real-time C
programs. A const variable may never be initialized. An use
case of this can be a the address of memory mapped I/O which
is read only.
Volatile - Linux for you and Netrino articles have clearly
explainedvolatile keyword. So, I will just be summarizing it
here.
When a programmer declares a variable volatile, (s)he informs
the compiler that the variable at any time during the life of
program can change its value, so be careful while optimizing
the code. In reference to the example of memory mapped I/O, I
would define a pointer holding the address as a const variable
but the value pointed by as volatile.
volatile int* const memIO = 0xFFFFFF05;
Some more declerations.
int volatile alcohol;
,volatile int alcohol; /* are the same - alcohol is volatile */
Now the pointer and volatile.
volatile int *myPtr; /* ordinary integer pointer to
volatile integer data*/
int* volatile myPtr; /* volatile integer pointer to
usual integer data*/
volatile int* volatile myPtr; /* volatile integer pointer to
volatile integer data*/
Now do NOT confuse in an interview.
Usage of volatile variable (as pointer in Netrino article)
Memory-mapped peripheral registers
Global variables modified by an interrupt service routine
Global variables accessed by multiple tasks within a multi-
threaded application
What does volatile long * const timer = 0x0000ABCD mean?
, C Questions
As a C programmer you would like to give some thought to
following C question.
1. Can structures be passed to the functions by value?
2. Why cannot arrays be passed by values to functions?
3. What are advantages and disadvantages of using macro
and inline functions?
4. What happens when recursion functions are declared
inline?
5. What is scope of static variables?
6. What is the output of printf("\nab\bcd\ref"); C
statement?
7. #define cat(x,y) x##y concatenates x to y. But
cat(cat(1,2),3) does not expand but gives preprocessor
warning. Why?
8. Is it possible to define a constant volatile variable?
9. Is it possible to define a volatile pointer?