Embedded C Interview Questions and Answers

    1 Votes

41) Consider the two statements below and point out which one is preferred and why?

#define B struct A *
typedef struct A * C;
The typedef is preferred. Both statements declare pointer to struct A to something else and in one glance both looks fine. But there is one issue with the define statement. Consider a situation where we want to declare p1 and p2 as pointer to struct A. We can do this by -
C p1, p2;

But doing - B p1, p2, it will be expanded to struct A * p1, p2. It means that p1 is a pointer to struct A but p2 is a variable of struct A and not a pointer.

42) What will be the output of the following code fragment?
char *ptr; 
if ((ptr = (char *)malloc(0)) == NULL)
{
puts("Got a null pointer");
}
else
{
puts("Got a valid pointer");
}

The output will be “Got a valid pointer”. It is because malloc(0) returns a valid pointer, but it allocates size 0. So this pointer is of no use, but we can use this free pointer and the program will not crash.

43) What is purpose of keyword const?

The const keyword when used in c means that the value of the variable will not be changed. But the value of the variable can be changed using a pointer. The const identifier can be used like this -

const int a; or int const a;

Both means the same and it indicates that a is an constant integer. But if we declare something like this -

const int *p

then it does not mean that the pointer is constant but rather it is pointing to an constant integer. The declaration of an const pointer to a non-constant integer will look like this -

int * cont p;

44) What do the following declarations mean?

const int a; 
int const a;
const int *a;
int * const a;
int const * a const;
  • The first two means that a is a constant integer.
  • The third declaration means that a is a pointer to a constant integer.
  • The fourth means that a is a constant pointer to a non-constant integer.
  • The fifth means that a is a constant pointer to a constant integer.

45) How to decide whether given processor is using little endian format or big endian format ?

The following program can find out the endianness of the processor.

#include<stdio.h>
main ()
{
union Test
{
unsigned int i;
unsigned char c[2];
};
union Test a = {300};
if((a.c [0] == 1) && (a.c [1] == 44))
{
printf ("BIG ENDIAN\n");
}
else
{
printf ("LITTLE ENDIAN\n");
}
}

46) What is the concatenation operator?

The Concatenation operator (##) in macro is used to concatenate two arguments. Literally, we can say that the arguments are concatenated, but actually their value are not concatenated. Think it this way, if we pass A and B to a macro which uses ## to concatenate those two, then the result will be AB. Consider the example to clear the confusion-
#define SOME_MACRO(a, b) a##b
main()
{
int var = 15;
printf(“%d”, SOME_MACRO(v, ar));
}

Output of the above program will be 15.

47) Infinite loops often arise in embedded systems. How does you code an infinite loop in C?
There are several ways to code an infinite loop -
while(1)
{ }
or,
for(;;)
{ }
or,
Loop:
goto Loop

But many programmers prefer the first solution as it is easy to read and self-explanatory, unlike the second or the last one.

48) Guess the output:

main()
{
fork();
fork();
fork();
printf("hello world\n");
}

It will print “hello world' 8 times. The main() will print one time and creates 3 children, let us say Child_1, Child_2, Child_3. All of them printed once. The Child_3 will not create any child. Child2 will create one child and that child will print once. Child_1 will create two children, say Child_4 and Child_5 and each of them will print once. Child_4 will again create another child and that child will print one time. A total of eight times the printf statement will be executed.

49) What is forward reference w.r.t. pointers in c?

Forward Referencing with respect to pointers is used when a pointer is declared and compiler reserves the memory for the pointer, but the variable or data type is not defined to which the pointer points to. For example 

struct A *p; 
struct A
{
// members
};

50) How is generic list manipulation function written which accepts elements of any kind?

It can be achieved using void pointer. A list may be expressed by a structure as shown below

typedef struct 
{
node *next;
/* data part */
......
}node;

Assuming that the generic list may be like this 

typedef struct 
{
node *next;
void *data;
}node;

This way, the generic manipulation function can work on this type of structures.

Page 5 of 7

Popular Videos

communication

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

C++ Interview Questions and Answers for Freshers, Experienced
Advanced C Interview Questions and Answers for Experienced
C Interview Questions and Answers