In this blog post I explain mostly asked Embedded C language Interview question
Content:
1. What is volatile in C?
Volatile is a type-qualifier in C language. It is used for code optimization. Volatile tells the compiler once you take repeated tasks don’t store value in cache memory. take from memory. once we do the same thing repeat again n again compiler store data in the cache memory and the register of the controller take data from the cache memory for faster execution. Using Volatile we inform the compiler volatile variable value is not stored in cache memory.
Declaring a variable as volatile tells the “compiler” not to perform any optimizations on that variable, basically, volatile means: “Always read this variable from memory, because it may change underneath you at any time”.
It’s “sort of” related to caching, but only in that you could say that the compiler may cache a variable in a register or embed it in the instruction itself. Volatile does nothing regarding actual hardware caches (i.e. L1, L2, L3, etc.). As such, for multicore / multithreaded applications, you cannot rely on using volatile only to guarantee correct operations.
A volatile variable absolutely will be stored in hardware caches.
In an embedded system, volatile is typically used for some sort of memory mapped IO or hardware registers, where the hardware can change the value asynchronous to what the compiler or code will see.
For detail Understanding watch video in Hindi : Volatile Type Qualifier in Hindi
in English : Volatile type qualifier in English
2. What is const in C?
const makes data constant. which means we can store data only at an initialization time. after initialization, we can’t able to modify it.
3. What is Pointer?
A pointer is one of the secondary data types. A pointer is a variable that stores the memory address of another variable as its value.
using a pointer we can access another datatype value and address it indirectly.
4. Compilation process in C?
The compilation process is done in 4 different steps.
Step1. Preprocessor:
- Preprocessors are useful for Including Header files.
- Removing Comments
- conditional compilation
- Convert .C file to .I format
Step 2: Translator
Translator translate .I file to Assembly language or source code .S format
Step3: Assembler
Assemblers convert assembly code to machine-understandable code objects.
convert .s file to .o format
Step4: Linker
The linker is responsible for .o objects code to link with libraries.
linker converts .o file to .exe (Executable file)
For detail Understanding watch video : Compilation process and Types of Errors in C
5. Types of errors in C?
2 types of errors in c.
- Compile time Error:
- Preprocessor:
- Header file name mistake.
- condition compilation name mistake.
- Assembler
- Syntax error
- Mis-spelled variable and function names
- Missing semicolons
- Improperly matches parentheses, square brackets, and curly braces
- The incorrect format in the selection and loop statement.
- Linker
- Library not available in the system
- Preprocessor:
- Run time error.
- Bus Error
- Floating point exception error.
6. Types of storage classes in C language.
Auto, Extern, Static, and Register are storage classes in c language.
Auto:
Automatic variables by default any variable assigned as auto variables.
Memory: Auto variable store stack section in memory.
Init value: Garbage / Random data
Scope: Within a block
life: End of block
Extern:
we have to use variables in another file at that time we will use the Extern variable.
Memory: Data Section of memory
Init value: Zero
Scope: Global multiple files.
life: Till the end of the program.
Static:
we have to use variables within a file only. or inside a function, local static variable re-initialization is not possible.
Memory: Data Section of memory
Init value: Zero
Scope: Within a block.
life: Till the end of the program.
for detail understanding watch video: Static, Extern Storage Class in C.
Register:
Register storage class can be applied only to local variables.
Memory: CPU Register.
Init value: Garbage / Random data
Scope: Within a block.
life: End of the block.
7. What is Structure in C language?
Structures in C is a user-defined data type available in C that allows to combining of data items of different kinds. Structures are used to represent a record.
Defining a structure: To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than or equal to one member. The format of the struct statement is as follows:
struct [structure name]
{
member definition;
member definition;
…
member definition;
};
(OR)
struct [structure name]
{
member definition;
member definition;
…
member definition;
}structure variable declaration;
8. What is union in C language ?
Union in C is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.
Defining a Union: To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows:
union [union name]
{
member definition;
member definition;
…
member definition;
};
(OR)
union [union name]
{
member definition;
member definition;
…
member definition;
}union variable declaration;
9. What is Difference between Structure and Union?
Struct | Union |
The struct keyword is used to define a structure. | The union keyword is used to define union. |
When the variables are declared in a structure, the compiler allocates memory to each variables member. The size of a structure is equal or greater to the sum of the sizes of each data member. | When the variable is declared in the union, the compiler allocates memory to the largest size variable member. The size of a union is equal to the size of its largest data member size. |
Each variable member occupied a unique memory space. | Variables members share the memory space of the largest size variable. |
Changing the value of a member will not affect other variables members. | Changing the value of one member will also affect other variables members. |
Each variable member will be assessed at a time. | Only one variable member will be assessed at a time. |
We can initialize multiple variables of a structure at a time. | In union, only the first data member can be initialized. |
All variable members store some value at any point in the program. | Exactly only one data member stores a value at any particular instance in the program. |
The structure allows initializing multiple variable members at once. | Union allows initializing only one variable member at once. |
It is used to store different data type values. | It is used for storing one at a time from different data type values. |
It allows accessing and retrieving any data member at a time. | It allows accessing and retrieving any one data member at a time. |
10. What is recursion in C?
When a function in C calls a copy of itself, this is known as recursion. To put it another way, when a function calls itself, this technique is called Recursion. Also, this function is known as recursive function.
void do_recursion()
{
… .. …
do_recursion();
… .. …]
}
int main()
{
… .. …
do_recursion();
… .. …
}
11. What is the difference between global int and static int declaration?
The difference between this is in scope. A truly global variable has a global scope and is visible everywhere in your program.
#include <stdio.h>
int my_global_var = 0;
int
main(void)
{
printf("%d\n", my_global_var);
return 0;
}
global_temp is a global variable that is visible to everything in your program, although to make it visible in other modules, you’d need an ”extern int global_temp; ” in other source files if you have a multi-file project.
A static variable has a local scope but its variables are not allocated in the stack segment of the memory. It can have less than global scope, although – like global variables – it resides in the .bss segment of your compiled binary.
#include <stdio.h>
int
myfunc(int val)
{
static int my_static_var = 0;
my_static_var += val;
return my_static_var;
}
int
main(void)
{
int myval;
myval = myfunc(1);
printf("first call %d\n", myval);
myval = myfunc(10);
printf("second call %d\n", myval);
return 0;
}
12. Difference between const char* p and char const* p?
const char* p is a pointer to a const char.
char const* p is a pointer to a char const.
Since const char and char const are the same, it’s the same.
13. What is pointer to pointer in C?
In C, a pointer can also be used to store the address of another pointer. A double pointer or pointer to pointer is such a pointer. The address of a variable is stored in the first pointer, whereas the address of the first pointer is stored in the second pointer.
The syntax of declaring a double pointer is given below:
int **p; // pointer to a pointer which is pointing to an integer
14. What is call by reference in functions?
When we caller function makes a function call bypassing the addresses of actual parameters being passed, then this is called call by reference. In incall by reference, the operation performed on formal parameters affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters.
15. How can I store value in bit in C language.
Using Structure we can allocate memory in bits in c language.
struct {
type [member_name] : width ;
};
type:
An integer type that determines how a bit-field’s value is interpreted. The type may be int, signed int, or unsigned int.
member_name:
The name of the bit-field.
width:
The number of bits in the bit-field. The width must be less than or equal to the bit width of the specified type.
Example:
#include <stdio.h>
#include <string.h>
struct {
unsigned int age : 3;
} Age;
int main( ) {
Age.age = 4;
printf( "Sizeof( Age ) : %d\n", sizeof(Age) );
printf( "Age.age : %d\n", Age.age );
Age.age = 7;
printf( "Age.age : %d\n", Age.age );
Age.age = 8;
printf( "Age.age : %d\n", Age.age );
return 0;
}
When the above code is compiled it will compile with a warning and when executed, it produces the following result −
Sizeof( Age ) : 4
Age.age : 4
Age.age : 7
Age.age : 0
16. What is typedef?
typedef is a C keyword, used to define alias/synonyms for an existing type in C language. In most cases, we use typedef’s to simplify the existing type declaration syntax. Or to provide specific descriptive names to a type.
typedef <existing-type> <new-type-identifiers>;
typedef provides an alias name to the existing complex type definition. With typedef, you can simply create an alias for any type. Whether it is a simple integer to complex function pointer or structure declaration, typedef will shorten your code.
17. Which is better #define or enum?
- If we let it, the compiler can build enum values automatically. However, each of the defined values must be given separately.
- Because macros are preprocessors, unlike enums, which are compile-time entities, the source code is unaware of these macros. So, if we use a debugger to debug the code, the enum is superior.
- Some compilers will give a warning if we use enum values in a switch and the default case is missing.
- Enum always generates int-type identifiers. The macro, on the other hand, allowed us to pick between various integral types.
- Unlike enum, the macro does not have a defined scope constraint.
18. Differentiate between the macros and the functions.
The differences between macros and functions can be explained as follows:
Macros | Functions |
---|---|
It is preprocessed rather than compiled. | It is compiled not preprocessed. |
It is preprocessed rather than compiled. | Function checks for compilation errors. |
Code length is increased. | Code length remains the same. |
Macros are faster in execution. | Functions are a bit slower in execution. |
Macros are useful when a small piece of code is used multiple times in a program. | Functions are helpful when a large piece of code is repeated a number of times. |
19. What is the difference between the local variable and global variable in C?
Following are the differences between a local variable and global variable:
Basis for comparison | Local variable | Global variable |
---|---|---|
Declaration | A variable which is declared inside function or block is known as a local variable. | A variable which is declared outside function or block is known as a global variable. |
Scope | The scope of a variable is available within a function in which they are declared. | The scope of a variable is available throughout the program. |
Access | Variables can be accessed only by those statements inside a function in which they are declared. | Any statement in the entire program can access variables. |
Life | Life of a variable is created when the function block is entered and destroyed on its exit. | Life of a variable exists until the program is executing. |
Storage | Variables are stored in a stack unless specified. | The compiler decides the storage location of a variable. |
20. What is call by value and call by reference in c?
Following are the differences between a call by value and call by reference are:
Call by value | Call by reference | |
---|---|---|
Description | When a copy of the value is passed to the function, then the original value is not modified. | When a copy of the value is passed to the function, then the original value is modified. |
Memory location | Actual arguments and formal arguments are created in separate memory locations. | Actual arguments and formal arguments are created in the same memory location. |
Safety | In this case, actual arguments remain safe as they cannot be modified. | In this case, actual arguments are not reliable, as they are modified. |
Arguments | The copies of the actual arguments are passed to the formal arguments. | The addresses of actual arguments are passed to their respective formal arguments. |
21. What is null pointer?
A pointer that doesn’t refer to any address of value but NULL is known as a NULL pointer. When we assign a ‘0’ value to a pointer of any type, then it becomes a Null pointer.
22. What is a dangling pointer?
- If a pointer is pointing any memory location, but meanwhile another pointer deletes the memory occupied by the first pointer while the first pointer still points to that memory location, the first pointer will be known as a dangling pointer. This problem is known as a dangling pointer problem.
- Dangling pointer arises when an object is deleted without modifying the value of the pointer. The pointer points to the deallocated memory.
Read my other blogs:
C Program to find Given Number is Prime or not.
Write a program to find Factorial Numbers of a given numbers.
Embedded C language Interview Questions.
Automotive Interview Questions
Understanding AUTOSAR Architecture: A Guide to Automotive Software Integration
Big Endian and Little Endian in Memory
Zero to Hero in C language Playlist
Embedded C Interview Questions
Subscribe my channel on Youtube: Yogin Savani