Embedded C Programming Basic Questions for Job Interview and Lab Examination (Viva Tips)
http://ingenuitydias.blogspot.com/2013/01/embedded-c-programming-basic-questions.html
Node *Reverse (Node *p)
{
Node *pr = NULL;
while (p != NULL)
{
Node *tmp = p->next;
p->next = pr;
pr = p;
p = tmp;
}
return pr;
}
B. Finding Loop in a single linked list.
(1) If the linked list is read only, take two pointer approach( p1, p2). Both pointing to beginning of linked list. Now increment p1 by 1 and p2 by 2 and compare both. if they are equal there is a cycle. Repeat this untill p2 points to null.
(2) If you have the condition not to modify the node but you can change the links, then reverse the linked list. If you reach the head node then there is a cycle.
More Embedded C Programming Questions And Suitable Answers:
Difference Between Process and Thread
What happens When Interrupt Occurs
Differences between Mutex And Semaphore
Unions-Why And When To Use
What is Storage class
Embedded C Basic Questions
C Programming Questions for Job Interview
Data Declarations And qualifiers
Variables and Pointers Volatile Or Costant
Dangling and Wild Pointers
C. Finding middle of the single linked list in a single traversal.
Step 1:
Take two pointers P1 and P2, both pointed to the first element.
Step 2:
Increment P1 by 1 and P2 by two.
Step 3:
Whenever P2 reaches to the end, P1 will be at the middle of the list, just return P1->data.
D. Program for printing UINT32 number as string
static char print_nibble (int nibble)
{
if (nibble <= 9)
{
return ( (char)nibble + ’0′);
}
else
{
return ( (char)nibble + ‘A’ – 10);
}
}
void PrintUINT32(int hex)
{
char strng[10];
int i;
for (i=0; i<8; i++)
{
strng[i] = print_nibble( (hex >> (28-(4*i)) ) & 0x0000000FU);
}
strng[8] = 0×00;
printf(“%s \n”,strng);
}
16.Byte Swap program
function swap16(val) { return ((val & 0xFF) << 8) | ((val >> 8) & 0xFF); }
function swap32(val) { return ((val & 0xFF) << 24) | ((val & 0xFF00) << 8) | ((val >> 8) & 0xFF00) | ((val >> 24) & 0xFF); }
E. Searching for duplicates in array Simple algorithm
int yes = 1, i, j;
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
if (arr[i] == arr[j])
{
printf("Found a duplicate of %d\n", arr[i]);
yes = 0;
break;
}
if (!yes)
break;
}
if (yes)
printf("No duplicates");
Efficient Algorithim
void rmdup(int *array, int length) { int *current , *end = array + length - 1;
for ( current = array + 1; array < end; array++, current = array + 1 )
{
while ( current <= end )
{
if ( *current == *array )
{
*current = *end--;
}
else { current++;
}
}
}
}
F. Quick way to determine endianness of your machine?
#include <stdio.h>
int main()
{
unsigned int i = 1;
char *c = (char*)&i;
if (*c)
printf("Little endian");
else printf("Big endian");
getchar();
return 0;
}
G. 2D Memory Allocation using C
int **p;
int i;
int rows = 4,columns = 3;
p = (int **)
malloc(sizeof(int *)*rows);
for(i=0;i<rows;i++)
{
p[i] = (int *)malloc(sizeof(int)*columns);
}
H. How to convert a byte array to word array in C?
const byte input[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
unsigned long output[sizeof(input) / sizeof(unsigned long)];
memcpy(output, input, sizeof(input));
Word to byte:
unsigned char bytes[sizeof(int)];
int n = 123;
*(int *)bytes = n;