Header Ads

Embedded C Interview Questions with Answers | Part 1

How to multiply number without using multiplication operator 

It is require to multiply the number without using multiplication to optimize the code which will take less time of execution to save some machine cycles at many instance to embedded development. The solution to this is very simple where you will multiplication using bit shifting operator and later will do summation or subtraction.

#include "stdafx.h"
#include "conio.h"
#include <stdio.h>


/* Multiplication to 16 then subtract (multiplication of 4 & 2), 16 -4 -2 =10 */
#define MULBY10(x) ((x<<4) - (x<<2) - (x<<1))

/* multiply with 8 + 1 */ 
#define MULBY9(x) ((x<<3) + x)

#define MULBY5(x) ((x<<2) + x)
#define MULBY50(x) ((x<<5) + (x<<4) + (x<<1))
#define MULBY100(x) ((x<<6) + (x<<5) + (x<<2))
#define MULBY200(x) ((x<<7) + (x<<6) + (x<<3))
#define MULBY1024(x) (x<<10)

int _tmain()
{
    printf("Hello, World!\n");
    
    int a = 10;
    printf("%d\n",MULBY10(a));
    printf("%d\n",MULBY9(a));
    printf("%d\n",MULBY5(a));
    printf("%d\n",MULBY50(a));
    printf("%d\n",MULBY100(a));
    printf("%d\n",MULBY200(a));
    printf("%d\n",MULBY1024(a));
    getch();
    return 0;
}

OutPut:
Hello, World!
100
90
50
500
1000
2000
10240

Find Greater of Two Numbers without using Conditional Operator

This is one of the tricky question in interview which you may face in interview. 


#include <stdio.h>
#include <math.h>

int isGreater(int a, int b)
{
    return (!((a-b) >> (sizeof(a) * 8 >> 1)));
}

#define IS_GREATER(a,b) (!((a-b) >> (sizeof(a) * 8 >> 1)))
#define IS_LOWER(a,b) (((a-b) >> (sizeof(a) * 8 >> 1)))
#define MAXOFTWO(a,b) (a - ((!(IS_GREATER(a,b))*(a-b))))
#define _MAXOFTWO(a,b) ((a+b+(abs(a-b)))/2)
#define __MAXOFTWO(a,b) (a ^ ((a^b) & -(a < b)))
//#define GET_MAXOFTWO(a,b) ((sqrt(a*a+b*b-2*a*b) + a + b)/2)

int main()
{
    printf("Hello, World!\n");
    int a = 123, b = 90;
    printf("%d\n",isGreater(a,b));
    printf("%d\n",IS_GREATER(a,b));
    printf("%d\n",MAXOFTWO(a,b));
    printf("%d\n",_MAXOFTWO(a,b));
    printf("%d\n",__MAXOFTWO(a,b));
    //printf("%d\n",GET_MAXOFTWO(a,b));
    return 0;
}

OutPut:
Hello,World!                                                                                                                                      
1                                                                                                                                                                  
1                                                                                                                                                                
123                                                                                                                                                                  
123                                                                                                                                                                  
123

What is Static Keyword, Static Variable & Static Functions


The static keyword is frequently used in C programming language. Programmers use this keyword without knowing the complete meaning of it. Let's see how and when to use this keyword in C program.


Declaring the variable as static:

The static keyword for variables is mainly used for the following reasons:
1. To restrict the visibility of the variable to the function/file where the variable is declared.
2. To retain the value stored in the variable even after the execution of the function is over. This means when a variable is declared as static within a function, the visibility of the variable is limited to the function, but the variable exists throughout the lifetime of the program.


For example: 
void func(void)
{
static int i =0;
i = i +1;
printf("%d\n", i);
}

int main(void)
{
func();
func();
return 0;
}

output: 
1
2

If we observe in the above program, when the func( ) is called for the second time in the main program, the value of 'i' is remaining as 1 and it is incremented to 2.

Note: Always be careful when declaring the variable as static in a header file, this is because whenever the header file is included in different .c files, each new copy of the variable will be created.

Also it is very important to remember that a static variable should always be initialized with constant literal in C. This is because, the value of the static variable should be resolved before the execution of the main( ) function starts.

For Ex: int a = 100;
             static int b = a; // Is not allowed and compiler will throw an error.


Declaring the function as static:

Generally a function is declared as static in large projects because of the following reason:

There could be a chance that a function with the same name could be declared/used in some other part of the file. So in this scenario if it is identified that the function that you are going to define will be used in only one file then it is better to make the function as static. This solves the ambiguity when the compiler compiles the project. In short static function is not visible outside of a translation unit(same .c file).


/*static_test.c*/
int f1(int);    
static int f2(int); 

int f1(int foo) {

    return f2(foo); 
}

int f2(int foo) {

    return 42 + foo;
}


/*main.c*/

int f1(int); 
int f2(int); 

int main(void) {

    f1(10); /* f1 is visible to the linker */
    f2(12); /* f2 is not visible to the linker */
    return 0;

}


If you could see in the above example, function f1 is visible to main.c as it is not made static in static_test.c, whereas f2() will not be visible to the linker and compilation will fail as the definition of the function f2() is not visible. 

That's all about a static keyword if you feel any correction is needed. Please let us know. 

How to toggle particular bit in given number

There are many ways & everyone has different thinking to do the same. I would like to demonstrate my way to do the same. For the same first you need to have the clear understanding of bit-wise operation in C. You need to know what is "bit wise AND" & what is "logical AND" operation.


  • && Means Logical AND
  • & means Bitwise AND

Program:


#include <stdio.h>
#define MSB (1 << ((sizeof(int)*8)-1))
#define BITPOS(pos) (1 << pos)
#define MAXVAL (MSB | (MSB-1))

int toggleBit(int num, char pos)

{

    int temp = (num & (BITPOS(pos)));

    temp = ~temp;
    //printf("%x\n%x\n",temp, MAXVAL); //To debug the code
    if(temp != MAXVAL)
    {
        num = num & temp;
    }
    else
    {
        temp = (temp & (BITPOS(pos)));
        num = num | temp;
    }
    //printf("%x\n",num); //To debug the code
    return num;

}


int main()

{
    int ProgBits = 0xFF08; //Number with Programmable Bits
    char bitposition = 2;
    printf("Given number: %x\n",ProgBits);
    ProgBits = toggleBit(ProgBits, bitposition); //Toggle particular bit in a given number
    printf("Number with toggle bit position %d: %x\n",bitposition, ProgBits);
    return 0;
}                                                                                                                                                                    

Output:

Given number: FF08
Number with toggle bit position 2: FF0C