C Programming Questions for Job Interview and Lab Examination (Viva Tips)

C Programming Questions Part - I

A. Write a macro for set and reset, swap.

#define SET_BIT( _X_, _NO_ ) ( 1<<(_X_-1)) | _NO_
#define RESET_BIT( _X_, _NO_ ) ~( ( 1<<(_X_-1) ) ) & _NO_
#define SWAP_BIT( _X_, _NO_ ) ( 1<<(_X_-1)) ^ _NO_

B. How to find give no is 2 power of n?

if ( ( no & ( no-1 ) ) == 0 )
printf( “Given number is 2 Power of N\n” );
else
printf( “Given number is not 2 Power of N\n” );


More Embedded C Programming Questions And Suitable Answers:

Difference Between Process and Thread

What happens When Interrupt Occurs
Differences between Mutex And Semaphore
Unions-Why And When To Use
What is Storage class
Embedded C Basic Questions
Embedded C Programming Basic Questions for Job Interview
Data Declarations And qualifiers
Variables and Pointers Volatile Or Costant
Dangling and Wild Pointers



C. Swap two numbers without using third variable.
A = B / A (now, A will have value val b/ val a and B will have value val b)

B = B / A (now, A will have value val b/ val a and B will have value val a)

A = A * B (now, A will have value val b and B will have value val a)

Above method overflow should be take care

A = A + B; 

B = A – B; 
A = A – B; 
A = A ^ B; 
B = A ^ B;
A = A ^ B;

D. Write a C program to count the number of set bits in an unsigned integer

/*
Program to count no. of bits in an unsigned integer
*/
void main( void )
{
unsigned int a = 15;
int count = 0;

while( a )
{
++count;
a = a & ( a – 1 );
}

clrscr();
printf( “Count is %d\n”, count );
getch();
}

E. Program to Reverse Bits – 16 bits

int main( void )

{
        int a= 0xFF00, i, rev=0;
for( i = 0; i < 16; ++i )
{
     if( a & ( 1 << i ) ) 
   { 
     rev |= ( 0×8000 >> i );
    }
 }

printf( “Input is 0x%04x\n”, a );

printf( “Reverse Bit Is 0x%04x\n”, rev );

return 0;

}



F. How to find the given number is little endian or big endian?

#include <stdio.h>
int main()
{
unsigned int n = 1;
char *p;
p = (char*)&n;
if (*p == 1)
printf(“Little Endian\n”);
else if (*(p + sizeof(int) – 1) == 1)
printf(“Big Endian\n”);
else
printf(“Surprise output!!!!\n”);
return 0;
}

G. Program to Nibble and bit swapping

int main( void )
{
unsigned char a = 40, b=20;
a = ( a>>4 ) | ( a<<4 );
b = ( ( b & 0xAA ) >> 1 ) | ( ( b & 0×55 ) << 1 );
clrscr();
printf( “After Nibble Swap %d\n”, a );
printf( “Bit swapping %d\n”, b );
getch();
return 0;
}

Related

Interview Questions 2364124710926308489

Post a Comment

  1. Thanks for Your Feedback.We appreciate readers comments for continuous improvement of this blog-log.

    ReplyDelete

SPAMMING will not be Appreciated.

emo-but-icon

Hot in week

Recent

Comments

Our Channel

Contact Us

Name

Email *

Message *

item