Skip to main content

Posts

Showing posts with the label important c interview questions

What are the differences between a union and a structure in C?

What are the differences between a union and a structure in C? A union is a way of providing an alternate way of describing the same memory area. In this way, you could have a struct that contains a union, so that the “static”, or similar portion of the data is described first, and the portion that changes is described by the union. The idea of a union could be handled in a different way by having 2 different structs defined, and making a pointer to each kind of struct. The pointer to struct “a” could be assigned to the value of a buffer, and the pointer to struct “b” could be assigned to the same buffer, but now a->some field and b->some otherfield are both located in the same buffer. That is the idea behind a union. It gives different ways to break down the same buffer area. The difference between structure and union are: 1. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory...

fibonacci series

Fibonacci series in c Fibonacci series in nothing but sum of the previous two number,below example shows the  Fibonacci series in c programming.using below code you can print  Fibonacci series. Fibonacci series in c programming example #include <stdio.h> #include <stdlib.h> int main() { int i=0,j=1,k,n,temp=0;     printf("enter the number of term\n");     scanf("%d",&n);     for(k=0;k<n-1;k++)     {     printf(" %d\n",temp);     i=j;     j=temp; temp=j+i;     }     return 0; } (function(){(function n(e) { function t(e) { if (e.parentNode) if (e.childNodes.length > 1) { for (var t = document.createDocumentFragment(); e.childNodes.length > 0; ) { var n = e.childNodes[0]; t.appendChild(n); } e.parentNode.replaceChild(t, e); } else e.firstChild ? e.parentNode.replaceChild(e.firstChild, e) : e.parentNode.removeChild(e); } function...

Factorial program in c

Factorial program in c using recursion #include <stdio.h> #include <stdlib.h>  #define b 10 int main() { const int a=0; int facto; if(a<=0) { printf("error a value should be more then 0"); } else { facto = factorial(a);     printf("factoria of %d is  %d\n",a,facto);  }     return 0; } int factorial(int k) { if(k<=1) { return 1; } k=k * factorial(k-1); return k; }

difference between const and Macros (#define)

Difference between const and #define(macros) #define    is processed by    the  preprocessor   doing what amounts to simple text replacement and that value cannot be changed. example   #define MY_CONST 42 Doesn't actually create a variable. It replaces the token MY_CONST across the file with the literal 42 at compile time. const is the  Qualifiers  it is not possible to modify but using pointer we can modify .  Actually creates a variable like normal but complains if you try and change it. A const value can sometimes be altered using pointers, but defining constants using macros creates literals, and literals cannot be changed. example   const int a=100; difference between const and #define #include <stdio.h> #include <stdlib.h>  #define b 10 int main() { const int a=100; int *ptr; printf("a is %d\n",a); ptr =&a; *ptr =200;     printf("b is %d\n...

Searching for duplicates in array

Searching for duplicates in array Simple algorithm int yes = 1, i, j; for (i = 0; i < n; ++i) {    for (j = i + 1; j < n; ++j) if (arr[i] == arr[j])    {        printf("Found a duplicate of %d\n", arr[i]);        yes = 0;        break;    }    if (!yes) break; } if (yes) printf("No duplicates"); Efficient Algorithim void rmdup(int *array, int length) {     int *current , *end = array + length - 1;     for ( current = array + 1; array < end; array++, current = array + 1 )     {         while ( current <= end )         {             if ( *current == *array )             {                 *current = *end--;             }         ...

Auto storage class

 Auto storage class in c They are declared at the start of a program’s block such as in the curly braces ( { } ).   Memory is allocated automatically upon entry to a block and freed automatically upon exit from the block. Automatic variables may be specified upon declaration to be of storage class auto.  However, it is not required to use the keyword auto because by default, storage class within a block is auto.

Finding Loop in a single linked list.

1) If the linked list is read only, take two pointer approach( p1, p2). Both pointing to beginning of linked list. Now increment p1 by 1 and p2 by 2 and compare both. if they are equal there is a cycle. Repeat this untill p2 points to null. (2) If you have the condition not to modify the node but you can change the links, then reverse the linked list. If you reach the head node then there is a cycle.

Program to Nibble and bit swapping

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 & 0x55 ) << 1 );   clrscr();   printf( “After Nibble Swap %d\n”, a );   printf( “Bit swapping %d\n”, b );   getch();   return 0; }

Reversing the String

Program for String Reverse int main(void) {         char    S[] = "Vijay C Programming";         int     i, j;         for( i=0, j=strlen(S)-1; i<((strlen(S))/2); ++i, --j )         {              S[i]  ^= S[j] ^=  S[i]  ^= S[j];         }         printf( "Reverse Is : %s\n", S );         return 0; }

Program to Reverse Bits

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 |= ( 0x8000 >> i );                }         }         printf( “Input is 0x%04x\n”, a );         printf( “Reverse Bit Is 0x%04x\n”, rev );         return 0; }

Swap two numbers without using third variable

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;