Skip to main content

Posts

Showing posts with the label C Programming

Function Pointer In C

Function Pointers in C Function Pointer that refers to the address of other function.   We will passed variable, constants, structure variables to a function as argument. But have you ever tried or thought to pass a function as an argument to another function . If not, then let us do it together. At first sight it may appear a bit confusing to you but trust me it is as simple as writing “int main()”. It is used to pass the function address to the another function or create the array of function or create function inside the structure  or create call back function used in layered software like AUTOSAR. Function_pointer_in_C                             Syntax : return_type(*pointer_name)(function_argument) example : void (*foo) (void); now foo can point to any function it should have return type of void and argument type is void. ...

infinite loop in C

Infinite loop or endless loop in C An infinite loop program is a thread that never ends stable in a "natural". Ending thread is exceptionally user or a break in software or hardware. Sample loops. The classic example for infinity. infinite loop  using for loop: #include "stdio.h"   int main () {    for( ; ; ) {       printf("This loop will run forever.\n");    }    return 0; } infinite loop  using do while loop:     #include "stdio.h" int main () {    do    {       printf("This loop will run forever.\n");    }    while(1);    return 0; } infinite loop  using while loop: #include "stdio.h"   int main () {    while (1)    {       printf("This loop will run forever.\n");    }     return 0; }

State Machine in C

 State Machine in C tutorial State Machine will help to do and complete some task where real-time event changes its state (like complete, pending, initial ). example take: you have write some sensor information data to NVM when  someone press switch, but writing in NVM it will take some 10ms time if u press two times in 10ms then NVM write will not complete. 1st NVM write will not complet it will write only second one. if you use State machan you can over come this problem, Using State Machine (SM) is widely spread over programming. A state MS is an automated scheduled to execute certain functions in correlation with certain events and external signals. Design decisions of State Machine: - Representing events - representing states - representing transitions - Distribution of events by StateMachine Methods and Models of creating an State Machine: - Switches nested - Table of states - OO State Design Pattern - mixed approach Cereintele a State Machine: - ...

differences between C and embedded C

C and embedded C Hi, there are very few differences between C and embedded C, some of them are: C is hardware independent and embedded C  is hardware dependent C is for desktop computers, embedded C usually is for microcontroller based applications. C use the resources of desktop computers (memory, OS, etc) Embedded C use only limited resources available in the chip (limited RAM, ROM, ports, etc). Embedded  C could be a subset of C. Other differences between C and embedded C what makes embedded c so different from the regular c is *absence of console *restriction on code size *and the compiler regular compilers create os dependent executable file where as embedded c compilers create a file which is downloaded to controllers to realise the required task regular compilers don't give abstraction for all the resources of the system where as in embedded c compiler give access to all the resources directly so code efficient code written in embedded c are thou...

Important C Programs

important C Programs  This is the very important C programs asked in interviews. 1. Write a program to find factorial of the given number. 2. Write a program to check whether the given number is even or odd. 3. Write a program to swap two numbers using a temporary variable. 4. Write a program to swap two numbers without using a temporary variable. Material from Interview Mantra. Subscribe to free updates via email. 5. Write a program to swap two numbers using bitwise operators. 6. Write a program to find the greatest of three numbers. 7. Write a program to find the greatest among ten numbers. 8. Write a program to check whether the given number is a prime. 9. Write a program to check whether the given number is a palindromic number. 10.Write a program to check whether the given string is a palindrome. 11.Write a program to generate the Fibonacci series. 12.Write a program to print "Hello World" without using semicolon anywhere in the code. 13.Writ...

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...

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.

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

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.

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

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,...

Storage Classes in C programming

C has a concept of 'Storage classes' which are used to define the scope (visibility) and a lifetime of variables and functions.And storage class determines how long it stays in existence.In c-programming, 4 type of storage class is there. So what Storage Classes are available? 1.auto 2.static 3.extern 4.register 1.Auto - storage class Auto is the default storage class for local variables.         {             int Count;             auto int Month;         } The example above defines two variables with the same storage class. auto can only be used within (inside) functions, i.e. local variables.Local variables is stored in stack memory. 2.static - Storage Class Notes: For some reason, static has different meanings in different contexts. When specified on a function declaration, it make...

Volatile

Volatile Keyword is important keyword in c programing language.  volatile   keyword is one type of qualifier in c programming languages. But, when it comes to writing code especially in Embedded programming the use of “volatile” keyword is very often.Volatile Keyword in Embedded System. But now the question is, What is it (volatile)? What it does? Why do we need it? Does my program/code really need this keyword? All these and more I will cover in this article. What does volatile mean? Ok, let’s find out the meaning of volatile in dictionary. VOLATILE means: – UNSTABLE, UNPREDICTABLE…etc. So, the basic meaning of volatile is we can’t predict what is going to happen next. Now, What is the significance of volatile keyword? The significance of volatile keyword in programming language is to inform/tell the compiler not to pre-predict/assume/believe/presume the value of the particular variable which has been declared as...