Skip to main content

Posts

Showing posts from May, 2016

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 n(e) { if (e) try { for (var n = e.querySelectorAll(".gr_"

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",b);     printf("a is %d\n",a);     printf(&quo

what is qualifiers?

what is qualifiers? Qualifiers defines the property of the variable. Two qualifiers are const and volatile. The const type qualifier declares an object to be unmodifiable. The volatile type qualifier declares an item whose value can legitimately be changed by something beyond the control of the program in which it appears, such as a concurrently executing thread / interrupt routine.

Static storage class

Static storage class  static variables in c static variable have lifetime over entire program run. scope of this variable is limited based on the place of declaration. if static variable is defined in a file and not inside any function, then scope of the variable is limited within that file. if static variable is defined inside a function, then the scope of the variable is limited within that function. we can use this variable any file and any function indirectly by accessing through pointer.

Embedded Systems

Hello, This is a the beginning of the series of topics on Embedded Systems it gives information and understanding on what are they with few examples,it describes the readers about their parts how each part are functioning and how are they put together to make a complete system for a working application. Embedded Systems An Embedded System primarily was meant for a device which was designed for performing a particular task (over 20 years ago - ex. Printer, electronic Calculator) later its definition was changed to a Computing System designed to perform a series of tasks meant to execute a particular Application (ex. Polaroid digital camera). Like-wise the definition of Embedded System has expanded to a widest spectrum and it can be now said that it is a high-speed processing /computing system designed for a set of Applications (ex. a smart phone). It is for the readers to identify what are the functional differences between a printer, a ‘polarid’ d

Register storage class

Register storage class Automatic variables are allocated in the main memory of the processor; accessing this memory location for computation will take a long time. when we required optimising the execution time, move the critical variable to the processor register. this can be done by using the register keyword. when storage class is register, a compiler is instructed to allocate a register for this variable. scope of the register variable is same as auto variable. NOTE: Allocation of register is not guaranteed always, it depends on number register available in processor and number register used for manipulation. if you define 4 variable as register storage class and and processor has only 2 register for variable allocation, then compiler will allocate 2 variable in registers and treat the remaining 2 variable as auto variable. therefore usage of register keyword should should be justified and cross checked with disassembly weather register is allocated or not.

C Programming Interview Question

Hello, friends in this blog i have listed c programming interview questions .This interview questions are asked in an interview .This   interview questions are commonly asked in pr ogramming interviews .This interview questions are very improtent for interviews . what is qualifiers? Modifier in C? Memory mapping in c embedded-systems extern-storage-clas s register-storage-class searching-for-duplicates-in-array How to convert a byte array to word array in C? auto-storage-class Finding middle of the single linked list pointers comming soon  click

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--;             }             else             {                 current++;             }         }     } }

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 middle of the single linked list

Finding middle of the single linked list in a single traversal. Step 1: Take two pointers P1 and P2, both pointed to the first element. Step 2: Increment P1 by 1 and P2 by two. Step 3: Whenever P2 reaches to the end, P1 will be at the middle of the list, just return P1->data.

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; }

given number is little endian or big endian?

 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; }

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;

Memory mapping in c

Memory mapping in C  Memory mapping in c program is nothing but how memory is allocated in RAM after running your c code In practical words, when we run any C-program, its executable image is loaded into RAM of a computer in an organised manner. 1. Text segment 2. Initialized data segment 3. Uninitialized data segment 4. Stack 5. Heap A typical memory layout of a running process 1. Text Segment: Text segment contains machine code of the compiled program and the text segment  is also known as a code segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions. As a memory region, a text segment may be placed on the heap or stack in order to prevent heaps and stack overflows from overwriting it. Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text

ISR Usage (interrupt service routine) and How to use ISR

ISR Usage (interrupt service routine) 1.understand how often the interrupt occurs 2.understand how much time it takes to service each interrupt 3.Make sure there is enough time to service all interrupts and to still get work done in the main loop there's only 1 sec of compute time per second to get everything done! -Keep ISRs short and simple.  (short = short time, not short code length) Do only what has to be done then RETI. Long ISRs may preclude others from being run Ask yourself, “Does this code really need to be here?” -Example:  alarm clock with 1Sec interrupts: -at interrupt, just increment the “seconds counter” and return -in the main code loop we can -do binary to BCD conversion  -lookup display code -write to display Effecting main program flow with ISRs ISRs are never called by the main routine.  Thus, -nothing can be passed to them (there is no “passer”) -they can return nothing (its not a real function call) So

ISR Interrupt Service Routine

ISR Interrupt Service Routine ISR Interrupt Service Routine is the software(program) service it will execute  when interrupt will generates.When interrupt signal comes to micro controller controller will stop the its current execution and it will go to the interrupt vector table and it will get the address of ISR Interrupt Service Routine and it will execute ISR Interrupt Service Routine program and it will return back to main program. AVR interrupt servicing  1. In response to the interrupt, the CPU finishes any pending instructions      and then ceases fetching further instructions.  Global Interrupt Enable      (GIE) bit is cleared. 2. Hardware pushes the program counter on the stack.   3. The CPU fetches the instruction from the interrupt vector table that      corresponds to the interrupt. This instruction is usually “jmp, address”.      The address is the address of the ISR. 00000000 <__vectors>:    0:   0c 94 46 00     jmp     0x8c    ; 0x8c

Interrupts

Interrupts Interrupts are the signal to the microcontroller or processor to mark the event that requires immediate attention. An interrupt is “requesting" the processor to stop to perform the current program and to “make time” to execute a special code(interrupt code). Whenever any device needs its interrupt service, the device notifies the microcontroller by sending it an interrupt signal. Upon receiving an interrupt signal, the microcontroller program_counter will go to the Interrupt vector table in that it will fetch the ISR address (interrupt service routine) and it will execute the ISR program and it will return to the main program where it stopped and it will start executing main program . Interrupts An interrupt is an exception, a change of the normal progression, or interruption  in the normal flow of program execution.   An interrupt is essentially a hardware generated function call. Interrupts are caused by both internal and external sou