5 Embedded C programming interview questions — Part 1
Welcome to this series on embedded software and C programming related interview questions. This series can be helpful to crack interview for beginners and intermediate embedded software developers. All the questions in this series is going to be the one that were asked to me in all the interviews that I have given during 7 years of my industrial experience.

1. How can you set or clear nth bit in a variable?
This question was asked to me in at least 90% of the interviews I have given so far that is why this is the first question of this post as well.
Now there can be many ways to do this but I am going to share the optimal solution (If you know a better solution please feel free to leave in comments for all of us :D )for it.
int setBit(int* flag, int bitNum){
if(bitNum < 0 || bitNum > 7) {
return -1;
}
*flag |= 1 << bitNum;
return 0;
}
int clearBit(int* flag, int bitNum){
if(bitNum < 0 || bitNum > 7) {
return -1;
}
*flag &= ~(1 << bitNum);
return 0;
}First things first, in above example we are taking care of extreme cases first, in the programming interview you should never forget to cover boundary cases. After that with the help of bit shifting I am setting and clearing individual bits in a flag.
I have created functions in above example so that you can easily pass nth bit and the flag on which operations need to be done.
2. Write a function that can find out if a number is odd or even.
One of the most common solution to this problem is to take modulus of the number with 2 and check for the remainder to see if value is 0 which means number is even and if value is 1 then number is odd.
But there can be another solution for this which I am going to share here:
#include <stdio.h>
int main()
{
int bitNum = 11;
int flag = bitNum & 1;
if(flag){
printf("Number is odd\n");
}
else {
printf("Number is even\n");
}
return 0;
}Above solution is pretty self explanatory. But now I have this one interview experience where is was asked to print if number is odd or even without using if/else or switch statement, so this is the solution i came up with:
#include <stdio.h>
int main()
{
int number = 11;
printf("number is %s\n", (number&1)? "odd": "even");
return 0;
}3. What is the meaning of const keyword and it’s benefits?
Const of course, pretty clear from the syntax itself is that it is used to make a variable constant. But this as an answer does not explain much about how things are working. Better words here would be const makes a variable “read-only” as you could not update a variable if you make it read only.
Benefits: When you define a variable as const, you’re essentially telling the compiler that the value of that variable should not be modified after it is assigned. This helps improve code readability, maintainability, and can also lead to optimizations by the compiler.
4. What is volatile and it’s use cases?
Volatile keyword is used to indicate that a variable may be changed at any time by external forces that the compiler is not aware of. It essentially tells the compiler not to optimize or cache the variable and always read it from memory. The primary use case for volatile is while dealing with hardware registers or variables that can be changed by external events, such as interrupts.
Use Cases:
Hardware Registers: In embedded systems programming, you often interact with hardware registers that control various aspects of the microcontroller or peripheral devices. These registers can be modified by the hardware at any time. Using volatile when declaring variables that represent hardware registers ensures that the compiler doesn't optimize away read or write operations to these registers.
volatile uint32_t *const TIMER1_CONTROL_REGISTER = (uint32_t*)0x40010000;Interrupt Service Routine (ISR) Variables: Variables that are accessed and modified within an interrupt service routine should be declared as volatile. This is because an interrupt can occur at any time, including in the middle of a non-interrupt code block, and the compiler may not be aware of when these variables can change.
Global Variables Shared Between Threads or Tasks: In multithreaded or multitasking environments, global variables that are accessed and modified by multiple threads or tasks should be declared as volatile to prevent compiler optimizations that might lead to unexpected behavior.
5. Can a variable be both volatile and const?
This is mostly asked as a follow up question to either of above two questions. You might be surprise to know that a variable can be both volatile and const. even though both have different use cases from each other if you just think of it on surface level.
Reason being that const is from your point of view that you can not update that variable in your application. But at the same time that variable can be updated by hardware event or interrupt.
This combination is useful when you have a variable that represents a hardware sensor reading or a memory-mapped hardware register that should not be modified by the program but can change its value due to external events.
Keep in mind that the const keyword prevents you from modifying the variable directly in your code, while the volatile keyword tells the compiler not to optimize reads and writes to the variable.
Please feel free to ask me if there are any questions in the comments. I shall be posting next part soon. :)






