Saturday 27 July 2013

Embedded C Programming Interview Questions and answers

1. Advantages of a macro over a function?

Macro gets to see the Compilation environment.
It is expanded by the preprocessor.
For example, you can't do this without macros:
#define PRINT(EXPR) printf( #EXPR "=%d\n", EXPR)
PRINT( 5+6*7 ) // expands into printf("5+6*7=%d", 5+6*7 );
You can define your mini language with macros:
#define strequal(A,B) (!strcmp(A,B))
Macros are a necessary evils of life. The purists don't like them, but without it no real work gets done.
============================================================================
2. Difference between const char* p and char const* p

In const char* p, the character pointed by 'p' is constant, so you cant change the value of character pointed by p but you can make 'p' refer to some other location.
in char const* p, the ptr 'p' is constant not the character referenced by it, so you cant make 'p' to reference to any other location but you can change the value of the char pointed by 'p'.

============================================================================

3. Can a variable be both const and volatile?

Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. If a variable is both const and
volatile, the two modifiers can appear in either order.

=============================================================================
4. How are pointer variables initialized?

Pointer variable are initialized by one of the following two ways
- Static memory allocation
- Dynamic memory allocation

============================================================================

5. When should a type cast not be used?
A type cast should not be used to override a const or volatile declaration. Overriding these type modifiers can cause the program to fail to run correctly.
A type cast should not be used to turn a pointer to one type of structure or data type into another. In the rare events in which this action is beneficial, using a union to hold the values makes the programmer's intentions clearer.

No comments:

Post a Comment