Tuesday, May 22, 2012

C programming

Storage classes
  • Automatic
  • Static
  • Allocated

Volatile keyword
  • Prevents CPU optimizing the variable
  • Not atomic
  • Eg: variable pointing to hardware register,  system time

Register keyword
  • Normal memory access can be slow compared to cpu access. 
  • Compiler can decide what var to be stored in register at what time
  • Variable are kept in register - those that needs to be accessed often or access time is critical
Extern Variable
  • Variable defined outside any function block
  • Defining - allocates memory for the variable
  • Declare - requires the variable be defined already in the memory
Extern function


Static variable 
  • Scope same as auto variable
  • Storage is kept permanent as long as the program is running
  • If defined in header file, each source file including it will have its own copy of the variable
Memory segment



Swap two variables without using a temp variable

a = 5
b = 3

a = (a+b);
a=8
b = a-b;
b=5;
a= a-b;


Can a variable be const and volatile
Yes. The const modifier implies that this particular program code cannot change the value of the actual variable, but that will not imply that the value can not be changed by means outside this code by hardware


Difference between calloc and malloc
  • Calloc allocates block of memory each of certain size and inits it to 0. 
  • Malloc allocates certain bytes of memory
Why use C for embedded system

Difference between exit and return
  • Return - returns call to the calling function
  • Exit - exit out of the program and return control to the operating system 
  



No comments:

Post a Comment