Tuesday, 2 April 2013

Summary of Storage Duration /Scope/ Linkage of variables/stoarge area and storage class specifier

Storage class specifier,storage duration ,scope and linkage and storage area of variables are the related concepts .But there is very necessary to understand them distinctly.Storage class specifier declares that a variable or function declared in a program where stored.There are following types of storage specifier in C :-

1)auto
2)register
3)extern
4)static
5)typedef
6)__declspec
7)mutable
8)thread_local


(1)auto:-auto is a keyword used to declare an automatic storage class.It is declared as following types :-

auto  a=2;              //auto
auto  b=3.8;            //float
auto c='h';            //char

 auto  b1=3.8f;          //float 
 auto  b2=3.8F;          //float
 auto  b3=3.8l;         //double
 auto  b4=3.8L;          //long double 
 printf("a=%d\n",a);    //int 
printf("b=%f\n",b);    // float
printf("c =%c\n",c);   //char

2)resister:-resister is also a storage class used to faster access of variable because the storage will be done in CPU registers .It is a hint to the compiler that the variable will be heavily used and that  recommend it will be kept in a processor register if possible.We can declare a datatype register in following manner:-

register int ri=98;

3)extern:-extern is also a storage class specifier .It is used for the use a variable which is initialized in another file in same project.When we use extern modifier with any variables it is only declaration i.e. memory is not allocated for these variable.We can declare register keyword in following manner:-

extern int ei=23;

4)Static:-static is also a storage class specifier used in C/C++.It also stores variable in memory.The default initial value is zero.The value of the variable persists between different function calls.We can use a static datatype  in following manner:-

  

#include <stdio.h>

void func() {
        static int x = 0; // x is initialized only once across three calls of func()
        printf("%d\n", x); // outputs the value of x
        x = x + 1;
}

int main(int argc, char * const argv[]) {
        func(); // prints 0
        func(); // prints 1
        func(); // prints 2
        return 0;
}


(5)typedef:-typedef keyword provide us to rename a variable datatype into a short and meaningful way.
Ex:-
                typedef unsigned long int ULI;
                 ULI a;



13)_declspec:-It is a storage class modifier  used with many extended attribute like dllimport and dllexport.



7)mutable:-This keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.

mutable member-variable-declaration;

8)thread_local:-The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared thread_local have this storage duration. thread_local can only be declared for global variables, plus those declared with static or extern.It is declared as following:-


 thread_local unsigned int rage = 1



storage duration explain how much duration a variable exist.There are four types of storage duration:-

1) Automatic storage duartion:-In this type of storage duration the variables are allocated at the beginning of enclosing code block and de-allocated at the end.All non-global variables have this storage duration expect those declared static ,extern or thread_local .

2)Static storage duration:-The variable is allocated when the programs begins and de-allocated when the program ends.Only one instance of  the variable exists.All global variable have this storage duration & also which declared as static or extern.

3) Thread:-The variable is allocated when the thread begins and de-allocated when the thread ends .Each thread has its own instance of the variable .Only variable declared as thread_local have this storage duration.
Thread_local can only be declared for global variables and  those declared as static or extern.

4)dynamic:-The variable is allocated and de-allocated per request by using dynamic memory allocation execution.


Linkage:- linkage refers to the ability of a function or variable to be referred to in other scopes .If a variable or function with the same identifier is declared in several scopes ,but cannot be referred to from all of them,then several instances of the variable are generated .The following linkage are recognized:-

(i))No linkage:-The variables can be referred to only from the scope it is in .All variables with automatic ,thread and dynamic storage duration have this linkage.

(ii) Internal linkage:- The variable can be referred to from all scopes in the current translation unit. All variables with static storage duration which are either declared static, or const but not extern, have this linkage.

(iii)External linkage:-The variable can be referred to from the scopes in the other translation units. All variables with static storage duration have this linkage, except those declared static, or const but not extern.

Scope:-Scope of the variables defines the when and where the variable or function will be available and what was their meaning?There are basically four type of scopes available:-

i)function scope:- This only applies to labels ,whose names are visible throughout the function where they are declared ,irrespective of the block structure .No two labels in the same functions may have the same name because the name only has a function scope ,the same name can be used for labels in every function .Labels are not objects -they have no storage associated with them.

ii)File scope:-The name declared outside the function has file scope which means name is usable at any point from the declaration on to the end of the source code  file containing the declaration .It is possible for these names to be temporarily hidden by declaration within compound statement .A name introduced by by any function defintion be always file scope because function definition must be outside other functions.

iii)block scope:-A name declared inside a compound statement or as a formal parameter to a function has block scope and is usable up to the end of enclosing braces.Any declaration of a name within a compound statement hides any outer declaration    of the same name until the end of the compound statement .

iv)Function prototype scope:-In the function prototype scope,declaration of a name extends only to the function prototype.


wrong declartion:-
                                  void func(int i, int i);

correct declaration:-
                                   void func(int i, int j);



Storage area is the space where the variables stored.We have some important information  about storage area .There is basically three storage area :-


(i)Code Segment(CS)
(ii)Data Segment (data, bss)
(iii)Stack Segment (heap,stack)


C program instructions get stored in code/text segment
Register variables are stored in Register. 
The memory created dynamically are stored in Heap.
Local Variables/arrays (Except static) are stored in Stack.  
 Global, extern & static variables/arrays are stored in data segment.

 a) Uninitialized static/global/extern variables are stored in the BSS(Block started by symbol) of the data     segment .ie high address..
               (Note: Uninitialised static variable is automatically initialised to zero)
               (Note: Uninitialised global variable is automatically initialised to zero)
               (Note: Uninitialised extern variable is automatically initialised to zero)
 b) Initialized static/global/extern variables are stored in data section of the data segment. ie low address
               (Initialised static also includes zero initialised static variable)

const local variable(initialised) is stored on stack.
const local var (uninitialized) is NOT POSSIBLE. It must be initialized when declared.
const global var (initialized) is stored in data section of data segment.
const global var (uninitialized) is NOT POSSIBLE. It must be initialized when declared.
-----------
null pointer ->   .bss
static pointer (uninitialised)-> (initialized to null by default) -> null pointer ->   .bss
command line arguments (stack)
function parameters (stack)

----------
Data in the bss segment is initialized by the kernel to zero before the program starts executing.
------------
Default storage class specifiers:
For external declarations (outside a function) the default storage class specifier will be extern and for internal declarations(inside a function) it will be auto.

default storage class specifier for functions  is always extern.
We can understand the variable's storage area with the help of these snapshots:-



Objects:-There are basically two type of objects in C:-the internal and external objects.

Anything declared outside a function is external object,
Anything inside a function, including its formal parameters, is internal.

At the outermost level, a C program is a collection of external objects.

The exact meaning of each storage-class specifier depends on two factors:-

Whether the declaration appears at the external or internal to a function.

Whether the item being declared is a variable or a function


Wednesday, 27 March 2013

Structure of RAM and storage of variables during program in RAM

We know that If we execute a program then it goes into RAM and then all operation perform and we get a exe to display the output .So it is very important to know the structure of RAM.Here is a snapshot for Ram structure:-


Here we can see that the RAM have the extra segment at the top of it which has the highest address.Below it there is a stack segment,then data segment ,then code segment and at the lowest ROM section.Thus the ROM section has the lowest address.Now can we can study briefly about all of these  five parts:-

(1) Extra segment:-Extra segment is the topmost part of the RAM.It has the highest address.It stores the far pointers and used for keyboard buffer and video buffer.

(2)Stack segment:-Stack segment have the two main parts:-stack and heap.In Stack memory is allocated towards top to bottom while in heap memory is allocated towards bottom to top manner.We can understand  here with the help of these snap shot that what part of program they contains in program execution:-

                                           
(3)Data segment:-Data segment also have two parts.The first one is bss which stands for block started by symbol and the second one is data.We can understand what kind of data these two parts contains during execution of program with the help of these snapshot:-



(4)Code segment:-Code segment have the program instructions in it mainly contains body of program during program execution.

(5) ROM section:-It mainly stores information about the ram configuration.


Here some important information about default storage class specifier for variables and function is following:-

(1)For external declarations (outside a function) the default storage class specifier will be extern and for internal declarations(inside a function) it will be auto. 

(2)default storage class specifier for functions  is always extern.

Saturday, 23 March 2013

Some more keywords....

I run a program for complex numbers in C .There are various type of complex number keyword declared in
#include <complex> .Here are some:-

1)_complex                                    //takes double
2)_C_double_complex                   //takes double
3)_C_float_complex                       //takes float
4)_C_ldouble_complex                  //takes long double

I have used them in my program and perform various operation like addition ,substraction,multiplication and division.Here is the program and output:-

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<msclr/safebool.h>
#include<complex>

void main()
{
bool a=false; //C++
//printf("boola=",a);
_complex c={12.66,34.787};
_complex d={1,34};
_complex e={(c.x+d.x),(c.y+d.y)};
printf("%lf\t%lf\n",e.x , e.y);
_C_double_complex a={12.34,65.7};
_C_double_complex b={2.34,65.7};
_C_double_complex g={a._Val[0]-b._Val[0],a._Val[1]-b._Val[1]};
printf("%lf\t%lf\n",g._Val[0] , g._Val[1]);
    
_C_ldouble_complex f={234.79345,12.64233};
_C_ldouble_complex h={1,2};
_C_ldouble_complex i={f._Val[0]*h._Val[0],f._Val[1]*h._Val[1]};
printf("%Lf\t%Lf\n",i._Val[0] , i._Val[1]);
_C_float_complex j={11.89,2.001};
_C_float_complex k={1,2.001};
_C_float_complex l={j._Val[0]/k._Val[0],j._Val[1]/k._Val[1]};
printf("%f\t%f\n",l._Val[0] , l._Val[1]);
_getch();
}

Output:-

13.660000       68.787000
10.000000       0.000000
234.793450      25.284660
11.890000       1.000000

*Here we use an another variable bool which takes two values true and false.It is declared in     header #include<msclr/safebool.h> .

Exception handling in C

Exception handling provides us a special type of facility in C.It handles the run-time errors occurred in the program.In the large programs it is very difficult to handle these type of exceptions.Some minor mistakes like divide by zero can create and interrupt in execution of a large program.So it is very difficult to find these smaller problems in a many thousand lines code.So to escape from these problems we use exception handling.There are following four keywords used in exception handling in C:-

  1. __try
  2. __finally
  3. __except
  4. __leave
1)__try :-__tyr is a keyword in C which is used in __try-__finally and __try-__except statemnt.A part of program in which there is chance of exception occurring that part of program write into this part.

2)__leave:-The __leave keyword is valid within a __try -__finally statement it jump to the end of try-finally.The termination handler is executed .Here __leave keyword jump the control to the finally when an exception is occur.We can seen it from this program:
3)__finally:-__finally gives a guarantee execution of a portion of a program when execution of a block code is interrupted.

int a=10,b=0;
__try
{
if (b == 0)
{
__leave;
}
c=(a/b);
cout<<"c="<<c;
}
__finally
{

cout<<"program terminates now";
}

4)__except:-_It is used in __try-__except statement .It is use as exception handler .It uses filter to catch the exception.If an exception occurs during execution of the guarded section or in any routine the guarded section calls, the __except expression (called the filter expression) is evaluated and the value determines how the exception is handled. There are three values:


EXCEPTION_CONTINUE_EXECUTION (–1)   Exception is dismissed. Continue execution at the point where the exception occurred.
EXCEPTION_CONTINUE_SEARCH (0)   Exception is not recognized. Continue to search up the stack for a handler, first for containing try-except statements, then for handlers with the next highest precedence.
EXCEPTION_EXECUTE_HANDLER (1)   Exception is recognized. Transfer control to the exception handler by executing the __except compound statement, then continue execution after the __except block.


It is used in following way:-


int* p = 0x00000000;
__try
{
__try
{
puts("Exception occured");
*p = 13;
//c=(a/b);
}

__finally
{
printf("%s","In finally ...exception handler");
}

}

 __except(filter(GetExceptionCode()))
{
puts("in except");
    }


//

int filter(unsigned int code)
//int filter(unsigned int code, struct _EXCEPTION_POINTERS *ep)
{

   puts("in filter.");

   if (code == EXCEPTION_ACCESS_VIOLATION || code ==STATUS_INTEGER_DIVIDE_BY_ZERO)
   {

      puts("caught AV as expected.");

      return EXCEPTION_EXECUTE_HANDLER;

   }

   else
   {

      puts("didn't catch AV, unexpected.");

      return EXCEPTION_CONTINUE_SEARCH;

   }

}

Various datatypes and their initialization

We can initialize variables with different type.I run a program for it which has following codes:-


#include<stdio.h>
#include<conio.h>
void main()
{
int a1=5;
int a2='5';
int a3=5.5; //intialising with double value
int a4=5.5f; //initialising with float value
int a5=5.5F; //initialising with float value
int a6=5.5l; //double
int a7=5.5L; //long double
int a8=0xff;  int a8a=0xffffffff;  int a8b=0xfff;  int a8c=0xffff;
int a9=0xFF;
int a10=0123; //Octal   int a=06; //octal
printf("a=%d\n",a1);
printf("a2=%lf\n",a2);    // double
printf("a3=%f\n",a3);   //float
printf("a4=%f\n",a4);   //float
printf("a5=%lf\n",a5);   //double
printf("a6 =%Lf\n",a6);//long double
printf("a7 =%Lf\n",a7);//long double
printf("a8 =%Lf\n",a8);//long double
printf("a8a =%Lf\n",a8a);//long double
printf("a8b =%Lf\n",a8b);//long double
printf("a8c =%Lf\n",a8c);//long double
printf("a9=%Lf\n",a9);//long double
printf("a10 =%Lf\n",a10);//long double

        auto  a=2;              //auto
        auto  b=3.8;            //double
        auto  b1=3.8f;          //float
        auto  b2=3.8F;          //float
        auto  b3=3.8l;         //double
        auto  b4=3.8L;          //long double
        auto c='h';            //char
        printf("a=%d\n",a);
        printf("b=%lf\n",b);    // double
        printf("b1=%f\n",b1);   //float
        printf("b2=%f\n",b2);   //float
        printf("b3=%lf\n",b3);   //double
        printf("b4 =%Lf\n",b4);    //long double
        printf("c =%c\n",c);   //char


char a12='a';//char
char a13=20;//int
char a14='\x40';//hex
char a15='\45';  //octal
char a16='\u0040'; //unicode  16 bit
char a17='\U00000040'; //unicode  32 bit
   printf("a12=%c\n",a12);
        printf("a13=%c\n",a13);  
printf("a14=%c\n",a14);
printf("a15=%c\n",a15);
        printf("a16=%c\n",a16);
        printf("a17=%c\n",a17);
   

char* a18="abdb";
char* a19="\x0040"; char* a20="\x40\xff";
char* a21="\334"; char* a22="\334\100";
char* a23="\u00ff"; char* a24="\u00ff\u00ff";
char* a25="\U000000F6"; char* a26="\U000000F6\U000000F6";
char*a59="\U000000F6\u00ff\x10\100";
        printf("a18=%s\n",a18);
        printf("a19=%s\n",a19);  
printf("a20=%s\n",a20);
printf("a21=%s\n",a21);
        printf("a22=%s\n",a22);
        printf("a23=%s\n",a23);
   printf("a21=%s\n",a24);
        printf("a22=%s\n",a25);
        printf("a23=%s\n",a26);
printf("a59=%s\n",a59);


wchar_t a27='a';
wchar_t a28=L'a';
wchar_t a31=20;
wchar_t a32='\x40';
wchar_t a35=L'\x79';
wchar_t a36='\33'; wchar_t a37='\51'; wchar_t a38=L'\334'; wchar_t a39=L'\51';
wchar_t a40='\u00ff'; wchar_t a41='\u00ff'; wchar_t a42=L'\u00ff'; wchar_t a43=L'\u00ff';
wchar_t a44='\U000000F6'; wchar_t a45='\U000000F6'; wchar_t a46=L'\U000000F6'; wchar_t a47=L'\U000000F6';
wprintf(L"a27=%lc\n", a27);
wprintf(L"a28=%lc\n", a28);
wprintf(L"a31=%lc\n", a31);
wprintf(L"a32=%lc\n", a32);
wprintf(L"a35lc=%lc\n", a35);
wprintf(L"a36=%lc\n", a36);
wprintf(L"a37=%lc\n", a37);
wprintf(L"a38=%lc\n", a38);
wprintf(L"a39=%lc\n", a39);
wprintf(L"a40=%lc\n", a40);
wprintf(L"a41=%lc\n", a41);
wprintf(L"a42=%lc\n", a42);
wprintf(L"a43=%lc\n", a43);
wprintf(L"a44=%lc\n", a44);
wprintf(L"a45=%lc\n", a45);
wprintf(L"a46=%lc\n", a46);
wprintf(L"a47=%lc\n", a47);

wchar_t* a48=L"a"; wchar_t* a49=L"abdsf";  
wchar_t* a50=L"\x40"; wchar_t* a50a=L"\x1079";
                wchar_t*                    a51=L"\x7910\x1879\x7979\x0056";
wchar_t* a52=L"\333"; wchar_t* a53=L"\333\33\333";
wchar_t* a54=L"\u00ff";         wchar_t* a55=L"\u00ff\u00ff";
wchar_t* a56=L"\U000000F6"; wchar_t* a57=L"\U000000F6\U000000F6";
wchar_t*a58=L"\U000000F6\u00ff\x1079\x10\345tyur1233$#%";
wprintf(L"*a48=%s\n", a48);
wprintf(L"*a49=%s\n", a49);
wprintf(L"*a50=%s\n", a50);
wprintf(L"*a50a=%s\n", a50a);
wprintf(L"*a51=%s\n", a51);
wprintf(L"*a52=%s\n", a52);
wprintf(L"*a53=%s\n", a53);
wprintf(L"*a54=%s\n", a54);
wprintf(L"*a55=%s\n", a55);
wprintf(L"*a56=%s\n", a56);
wprintf(L"*a57=%s\n", a57);
wprintf(L"*a58=%s\n", a58);

_getch();
}



Output:- 



a=5
a2=0.000000
a3=0.000000
a4=0.000000
a5=0.000000
a6 =0.000000
a7 =0.000000
a8 =0.000000
a8a =0.000000
a8b =0.000000
a8c =0.000000
a9=0.000000
a10 =0.000000
a=2
b=3.800000
b1=3.800000
b2=3.800000
b3=3.800000
b4 =3.800000
c =h
a12=a
a13=¶
a14=@
a15=%
a16=@
a17=@
a18=abdb
a19=@
a20=@
a21=▄
a22=▄@
a23=
a21=
a22=÷
a23=÷÷
a59=÷ ►@
a27=a
a28=a
a31=¶
a32=@
a35lc=y
a36=←
a37=)
a38=▄
a39=)
a40=?
a41=?
a42=
a43=
a44=?
a45=?
a46=÷
a47=÷
*a48=a
*a49=abdsf
*a50=@
*a50a=?
*a51=???V
*a52=█
*a53=█←█
*a54=
*a55=
*a56=÷
*a57=÷÷
*a58=÷ ?►σtyur1233$#%


 Following intialization has their own meaning:-

'\ooo': char with octal value
'\xhh':char with hex value
'\u':16-bit, assumed UTF16
'\U'-32-bit, assumed UCS-4

*L is used for declare a wide character literal or wide char string literal.

Friday, 22 March 2013

Use of some specific keyeword


Here are some important  keywords which are very useful for us.So it is very important to know about them.


  1. auto
  2. enum
  3. register
  4. typedef
  5. extern
  6. union
  7. const
  8. continue and break 
  9. static
  10. __inline
  11. inline
  12. mutable
  13. thread_local
(1)auto:-auto is a keyword used to declare an automatic storage class.It is declared as following types :-

auto  a=2;              //auto
auto  b=3.8;            //float
auto c='h';            //char

 auto  b1=3.8f;          //float 
 auto  b2=3.8F;          //float
 auto  b3=3.8l;         //double
 auto  b4=3.8L;          //long double 
 printf("a=%d\n",a);    //int 
printf("b=%f\n",b);    // float
printf("c =%c\n",c);   //char

(2)enum:-The enum data type data type gives us a chance create our own data types and define what value that data type can take.We can use enum in our progran in following manner

enum day{sun,mon};
enum day d;
d = mon;
printf("%d\n ",sun);
printf("%d\n",mon);

here if we want to initialize d with any other value rather than sun ,mon then it cause an error.If we want to print the value of sun ,mon then it gives 0 and  1 respectively because by default value in enum intilialize with zero.

(3)resister:-resister is also a storage class used to faster access of variable because the storage will be done in CPU registers .It is a hint to the compiler that the variable will be heavily used and that  recommend it will be kept in a processor register if possible.We can declare a datatype register in following manner:-

register int ri=98;

(4)typedef:-typedef keyword provide us to rename a variable datatype into a short and meaningful way.
Ex:-
                typedef unsigned long int ULI;
                 ULI a;

(5)extern:-extern is also a storage class specifier .It is used for the use a variable which is initialized in another file in same project.When we use extern modifier with any variables it is only declaration i.e. memory is not allocated for these variable.We can declare register keyword in following manner:-

extern int ei=23;

(6)union:- union is a data type in C which is similar to structure in declaration and use but different in memory allocation.  

union a
{
char name[4];
int sal;
};
union a detail;
detail.sal=1024;
printf("detail.name[0]=%d\n",detail.name[0]);
printf("detail.name[1]=%d\n",detail.name[1]);
 printf("detail.name[2]=%d\n",detail.name[2]);
printf("detail.name[3]=%d\n",detail.name[3]);
printf("detail.sal=%d\n",detail.sal);

here char name and int sal uses same memory location .Here printf function prints 0,4,0,0,1024 respectively.
because the 1024 stored in following manner for 32-bit  :-


7)const:-The const qulaifier explicitly declares an data object that cannot be changed .Its value is set at initialization.We cannot use a const variable in expression requiring a modifiable lvalue.It can be used with four types:- 
int * const ic=&v4;        //a const pointer to an integer ,the value of integer can be changed.The value of pointer cannot be changed
const int *ci=&v5;        // a pointer to a const integer the value of integer cannot be changed .The value of pointer can  be changed   .

int const *icptr=&v6;       //same as second
const int* const cicptr=&v7;       //It is a const pointer to a const integer so neither  the integer can be changed nor the point to anything else.


8)continue and break:-break keyword is used in that case when in a loop an special condition is satisfied and we want to exit from the loop.For example:-

for(int i=0;i<5;i++)
{
if(i==3)
{
break;
}
printf("%d\n",i);
}
continue keyword is used in that case if we want to take control to the beginning of the loop then it is used.
We can use continue in following manner:-

for(int j=0;j<5;j++)
{
printf("%d\n",j);
if(j==3)
{
continue;
}
printf("%d\n",j);
}

(9)Static:-static is also a storage class specifier used in C/C++.It also stores variable in memory.The default initial value is zero.The value of the variable persists between different function calls.We can use a static datatype  in following manner:-

  

#include <stdio.h>

void func() {
        static int x = 0; // x is initialized only once across three calls of func()
        printf("%d\n", x); // outputs the value of x
        x = x + 1;
}

int main(int argc, char * const argv[]) {
        func(); // prints 0
        func(); // prints 1
        func(); // prints 2
        return 0;
}



(10)__inline:-This function copy a function body whenever we call a funcion. The __inline and __forceinline keywords are available in both C and C++. For compatibility with previous versions, _inline is a synonym for __inline.We can declare a function _inline in following manner:-

__inline void congratulate(int score);


(11)inline:-It has the same woak as __inline .The main difference is that it is only available in C++.The inline keyword used as following manner:-

inline int add(int i, int j) { return i + j; }

(12)mutable:-This keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.

mutable member-variable-declaration;

(13)thread_local:-The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared thread_local have this storage duration. thread_local can only be declared for global variables, plus those declared with static or extern.It is declared as following:-


 thread_local unsigned int rage = 1


Thursday, 21 March 2013

Creating and using a Dll from Empty project

It is very important to know that how cane we create a Dll from an empty project and how can we use it.

Creating a library:-

We can create it by following steps:-

1)Create an empty project into the visual studio.


2)We need the function with the following syantax.


#include<iostream>
#include <cstdio> // instead of <stdio.h>
#include <cstdlib> // instead of <stdlib.h>
 #include <conio.h> //-- do not use

#include <cstring> // instead of <string.h>
using namespace std;

//C++ style header decoration in the dll
  __declspec(dllexport)  int __stdcall add(int a ,int b )
{
 return a+b;
}
//c style header declartion in the dll
 extern "C"  __declspec(dllexport) int  _stdcall sub(int a ,int b )
{
 return a-b;
}
//C++ style header decoration in the dll
  __declspec(dllexport)int _cdecl mul(int a ,int b )
{
 return a*b;
}
//c style header declartion in the dll
extern "C" __declspec(dllexport)int  _cdecl divide(int a ,int b )
{
 return a/b;
}


3)Now, we need to follow these steps:-
 project properties -> general->project defaults ->configuration type->choose dynamic library(.dll)


4)Then we need to build it .After building it the .lib file is generated .

(5)Now we Can use these functions into other files by including .lib file and header files into that project.

Using the created library: -.We can use the created library by adding this .lib file to that project in which we want to use it.We need to create a new project and call the functions in that project after adding the library.We need to write following code for using this library.


#include<iostream>
using namespace std;

//C++ style header decoration in the dll
int __stdcall add(int a ,int b );

//C style header declartion in the dll
extern "C" int  _stdcall sub(int a ,int b );

//C++ style header decoration in the dll
int _cdecl mul(int a ,int b );

//C style header declartion in the dll
extern "C" int  _cdecl divide(int a ,int b );

void main()
{
 cout<<add(3,4);
 cout<<sub(4,3);
 cout<<mul(3,4);
 cout<<divide(4,2);
}



There are two methods to adding the .lib fie:-

1)We need to go to add->existing item->choose .lib file
2)
(a)In the project property  Go to  VC++ Directories   .In librery Directories add the  full path where the .lib is placed.



(b)n the linker -->Input-->additional Dependencies -->add the .lib file of the dll generated.



Now we can easily use the library by adding any of these two methods.

*.lib function is required to build the function and .dll and .lib both required for the execution of the program.