Q:
Which one is better: Pre-increment or Post increment?
belongs to collection: Embedded C interview questions and answers (2022)
Embedded C interview questions and answers (2022)
- What is the difference between C and embedded C?
- What is the volatile keyword?
- What is the use of volatile keyword?
- What is the difference between the const and volatile qualifiers in C?
- Can a variable be both constant and volatile in C?
- Can we have a volatile pointer?
- The Proper place to use the volatile keyword?
- What is ISR?
- Can we pass any parameter and return a value from the ISR?
- What is interrupt latency?
- How do you measure interrupt latency?
- How to reduce interrupt latency?
- Is it safe to call printf() inside Interrupt Service Routine (ISR)?
- Can we put a breakpoint inside ISR?
- What is the difference between an uninitialized pointer and a null pointer?
- What are the causes of Interrupt Latency?
- Can we use any function inside ISR?
- What is a nested interrupt?
- What is NVIC in ARM Cortex?
- Can we change the interrupt priority level of Cortex-M processor family?
- Why “C” language mostly preferred than assembly language?
- What is the start-up code?
- What are the start-up code steps?
- Infinite loops often arise in embedded systems. How do you code an infinite loop in C?
- How to access the fixed memory location in embedded C?
- Difference between RISC and CISC processor?
- What is the stack overflow?
- What is the cause of the stack overflow?
- What is the difference between the I2c and SPI communication Protocols?
- What is the difference between Asynchronous and Synchronous Communication?
- What is the difference between RS232 and RS485?
- What is the difference between Bit Rate and Baud Rate?
- What is segmentation fault in C?
- What are the common causes of segmentation fault in C?
- What is the difference between Segmentation fault and Bus error?
- Size of the integer depends on what?
- Are integers signed or unsigned?
- What is a difference between unsigned int and signed int in C?
- What is the difference between const and macro?
- How to set, clear, toggle and checking a single bit in C?
- What will be the output of the below C program?
- Write a program swap two numbers without using the third variable?
- What will be the output of the below C program?
- What is meant by structure padding?
- What is the endianness?
- What is big-endian and little-endian?
- Write a C program to check the endianness of the system
- How to Convert little-endian to big-endian vice versa in C?
- What is static memory allocation and dynamic memory allocation?
- What is the memory leak in C?
- What is the output of the below C code?
- What is the output of the below C code?
- What is the difference between malloc and calloc?
- What is the purpose of realloc( )?
- What is the return value of malloc (0)?
- What is dynamic memory fragmentation?
- How is the free work in C?
- What is a Function Pointer?
- How to declare a pointer to a function in C?
- Where can the function pointers be used?
- Write a program to check an integer is a power of 2?
- What is the output of the below code?
- What is the output of the below code?
- Write a program to count set bits in an integer?
- What is void or generic pointers in C?
- What is the advantage of a void pointer in C?
- What are dangling pointers?
- What is the wild pointer?
- What is a NULL pointer?
- What are the post-increment and decrement operators?
- Which one is better: Pre-increment or Post increment?
- How will you protect a pointer by some accidental modification with the pointer address?
- How to use a variable in a source file that is defined in another source file?
- Can static variables be declared in a header file?
- What is the difference between pass by value by reference in c and pass by reference in c?
- What is a reentrant function?
- What is the inline function?
- What is the advantage and disadvantage of the inline function?
- What is virtual memory?
- How can you protect a character pointer by some accidental modification with the pointer address?
- Consider the two statements and find the difference between them?
- Can structures be passed to the functions by value?
- What are the limitations of I2C interface?
- What is the Featured of CAN Protocol?
- What is priority inversion?
- What is priority inheritance?
- Significance of watchdog timer in Embedded Systems?
- What Is Concatenation Operator in Embedded C?
Answer:
Nowadays compiler is enough smart, they optimize the code as per the requirements. The post and pre-increment both have their own importance we need to use them as per the requirements.
If you are reading a flash memory byte by bytes through the character pointer then here you have to use the post-increment, either you will skip the first byte of the data. Because we already know that in the case of pre-increment pointing address will be increment first and after that, you will read the value.
Let’s take an example of the better understanding,
In the below example code, I am creating a character array and using the character pointer I want to read the value of the array. But what will happen if I used a pre-increment operator? The answer to this question is that ‘A’ will be skipped and B will be printed.
But in place of pre-increment if we use post-increment then the problem is getting solved and you will get A as the output.
Besides that, when we need a loop or just only need to increment the operand then pre-increment is far better than post-increment because in case of post increment compiler may have created a copy of old data which takes extra time. This is not 100% true because nowadays the compiler is so smart and they are optimizing the code in a way that makes no difference between pre and post-increment. So it is my advice, if post-increment is not necessary then you have to use the pre-increment.
Note: Generally post-increment is used with array subscript and pointers to read the data, otherwise if not necessary then use pre in place of post-increment. Some compiler also mentioned that to avoid to use post-increment in looping condition.
need an explanation for this answer? contact us directly to get an explanation for this answeriLoop = 0.