Thursday 18 April 2013

C String functions and Numeric format

Today ,I wrote two programs one for C string functions and other one for numeric format conversions in C.First, we will discuss string functions which are used frequently in programs.Here are some important C functions:-

Here is the program for the same functions:-


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str1[] = "amar";
char stra[] = "amar123";
char strb[] = "12334amar";
char str10[] = "my,name.is-amar";
char str2[40] ;
char str3[40];
char str4[40];
char str5[40];
char str6[40] = "singh";
char str7[40] ="hi";
char str8[40] = "bye" ;
char str9[40] ;
char str11[40];
char *ch = strchr(str6 ,'n');
char *ch1 = strrchr(str1 ,'a');
char keys[] = "1234567890";
char vowel[] ="aeiou";
int i = strcspn(stra,keys);
int j = strspn(strb,keys);

//cpoy
strcpy(str3,str1);
strcpy(str2,"hi");
strncpy(str4,str6,sizeof(str6));
strncpy(str5,str1,3);
strcat(str6,str1);
strncat(str7,str1,3);
strxfrm(str8,str1,3);

memcpy(str9,str6,sizeof(str6));
memset(str10,97,sizeof(str10));
memmove(str10+5,str10+3,10);
int k=memcmp(strb,stra,sizeof(stra));
char* ch2 = strpbrk(str6,vowel);

printf("str10 = %s\n",str10);
printf("str2 = %s\n",str2);
printf("str3 = %s\n",str3);
printf("str4 = %s\n",str4);
printf("str5 = %s\n",str5);
printf("str6 = %s\n",str6);
printf("str7 = %s\n",str7);
printf("str8 = %s\n",str8);
printf("str9 = %s\n",str9);
printf("str10 = %s\n",str10);
printf("k = %d\n",k);

printf("strlen = %d\n",strlen(str1));
printf("strcmp = %d\n",strcmp(str1,str6));
printf("strncmp = %d\n",strncmp(str7,str5,3));
printf("strcoll = %d\n",strcoll(str7,str5));
printf("ch = %u\n",ch-str6+1);
printf("ch1 = %u\n",ch1-str1+1);
printf("i = %u\n",i+1);
printf("j = %u\n",j);
printf("ch2=%c\n",*ch2);

_getch();
}


Output of the program:--


str10 = aaaaaaaaaaaaaaaa
str2 = hi
str3 = amar
str4 = singh
str5 = ama
str6 = singhamar
str7 = hiama
str8 = ama
str9 = singhamar
str10 = aaaaaaaaaaaaaaaa╠╠╠╠╠╠╠╠12334amar
k = -1
strlen = 4
strcmp = -1
strncmp = 7
strcoll = 1
ch = 3
ch1 = 3
i = 5
j = 5
ch2=i


Numeric format conversion easily converts one format of datatype to other format of datatype.It uses various 
functions which are following:-


Here is the program  for the numeric formats .Here are some functions for C99 which are not used in my program and some are not supported:-

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int i1=323,i2=345,i3=890,i4;
unsigned long int uli;
long int li,li1,li2,li3,li4,li5;
char ch1='a',ch2=99,ch3=0;
char str1[40]="345.88";
char str2[40]="36467";
char str3[40]="654647868";
char str4[40]="32.64 16.32";
char str5[40]="32.23424";
char str6[100]="684069 35a5c7 -10101011 0xe72878 010";
char *chend;
char *chend2;
float f1 = 3263.809;
float f2 = 668.098;
float f3 = 676.8798;
i4=atoi(str2);
printf("i4 = %d\n",i4);
li = atol(str3);
uli = strtoul(str2,NULL,0);

li1 = strtol(str6,&chend,10);
li2 = strtol(chend,&chend,16);
li3 = strtol(chend,&chend,2);
li4 = strtol(chend,&chend,16);
li5 = strtol(chend,NULL,8);
printf("li1 = %ld\n",li1);
printf("li2 = %ld\n",li2);
printf("li3 = %ld\n",li3);
printf("li4 = %ld\n",li4);
printf("li5 = %ld\n",li5);

double d2 = strtod(str4,&chend);
double d3 = strtod(chend ,NULL);
printf("d2 = %lf\n",d2);
printf("d3 = %lf\n",d3);

printf("uli=%lu\n",uli);
printf("li=%ld\n",li);
double d1 = atof(str5);

printf("d1=%lf\n",d1);

_getch();
}

Output:-

i4 = 36467
li1 = 684069
li2 = 3515847
li3 = -171
li4 = 15149176
li5 = 8
d2 = 32.640000
d3 = 16.320000
uli=36467
li=654647868
d1=32.234240




  

Friday 12 April 2013

Declaration and Definition of variables and functions

The main difference declaration and definition of variables and functions is that when we declare a variable or function then there is no memory allocated for that variable and function .If we declare a variable and put some value in it along with then it is called definition .We can understand this by following examples:-

Functions:-

void some_function(void); 
//function declaration because there is no memory created for it because by default it is extern outside the function

extern void some_function(void);
//function declaration because it is declared as extern it is always be a declaration because there is no memory allocated for it either will be in function or outside the function

some_function()
{
..................//this is a definition because defintion created for it
}
============
Variables:-

int i;        
//definition because there is 4-byte memory allocated for it and some garbage value put into it.

int i=0;
//definition because there is 4-byte memory allocated for it and zero put into it.

int i=5;
//definition because there is 4-byte memory allocated for it and 5 put into it.

extern int i;
//declaration because there is no memory allocated for it.

extern int i=5;
//definition because there is 4-byte memory allocated for it and 5 put into it 

Function scopes (extern storage class) and Declaraing functions within functions

Today,I run a program related to the topic "function scopes (extern storage class)  and declaraing functions within functions".We need to create two program in the same project to observe the behaviour of the extern function .Here is the both program files:-

//Source.cpp


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void f11();
void f21()
{
printf("%s\n","f21");
}
void f71()
{
printf("%s\n","f71");
}

extern void f81();

void main()
{
f11();
f21();
        void f31();
// f31();
        void f41();
f41();

        f81();
extern void f51();
f51();
        extern void f61();
f61();
        extern void f71();
f71();
_getch();
}

void f11()
{
printf("%s\n","f11");
}
void f31()
{
printf("%s\n","f31");
}
void f61()
{
printf("%s\n","f61");
}


//source1.cpp


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

void f51()
{
printf("%s\n","f51");
}
void f41()
{
printf("%s\n","f41 test");
}
extern void f81()
{
         printf("%s\n","f81");
}


Output of the program:-


f11
f21
f41 test
f81
f51
f61
f71


Now we can see here that f11 and f21 which are declared as extern are called f31 is not called because there is only a declration in the function.if we uncomment the f31 .Then it is also called .In the same f41 ,f51 and f81 also called with extern keyword  and f71 and f21 have no need to declaration  because their defintion is above the main.If f41,f51 and f81 have declare without extern keyword then there will be no effect and they printed as well as.







Wednesday 10 April 2013

Good programming practices

Here are some important points which we need to follow during programming as good programming practices:-

1)We need to initialize each variable with zero or null while declaring it.By this we reduce the chances of occurring an error due to garbage value initialized to the variables which are not intilaized.The pointrer which is not initialized while declaration may be dangerous.

2)We need to create any project in many modules means we need to create function for each specific task.It increase the reusablity of the code.

3)Always check file pointer is null or not and exit if null in the file handling programs because it may cause more problems because we don't come to know what the problem is.


  if (ColorImageFile == NULL  ||BlackandwhiteImageFile == NULL)
  {
printf("can't open the file");
exit(1);
  }

4)In file handling program use fread and fwrite in the place of fputc and fgetc because it is more efficient .

5)Use try for that part of program where is chance of an exception and handle it after try block in catch or finally.Use __try with __finally and __leave and use try for catch and throw.

6)Use modifiers with datatypes according to need .While if we have only positive values in an array then we need to declared it as unsigned int.It also increase the range twice.

7)Always prefer dynamic memory allocation because it saves memory.

8)Always give a meaning full name to the variables and functions.It plays a very important role to understand the program by anyone else or the programmer himself.

9)Adding comments is also a very important thing which helps anybody to understand a block of code in seconds.

10)Always use the format specifier corresponding to the variable declared because minor change in format specifier does major change in output .For example if we store a negative value in a signed int and print it in %u then it gives different output.So we need to be careful in this.
 

Common programming mistakes

We  discuss here some common mistakes which we do during programming.Here are the same list of these type of mistakes:-

(1)When we passing a pointer to a function which is  a pointer for array then there is no need to do malloc again then we allocate a new memory which is pointed by that poniter.Example:-
int* f1(int* );

void main()
{
int valueforb = 10;
int *bb = (int*)malloc(size*sizeof(int));
for(long unsigned int iterator = 0 ;iterator < size;iterator++)
{
bb[iterator]= valueforb ;
valueforb+=2;
}
f1(bb);
_getch();
}
int* f1(int* b)

{
        *b = (int*)malloc(size*sizeof(int));
for(long unsigned int iterator = 0;  iterator < size ; iterator++)
{
printf("%d  ",b[iterator]);
}
return b;
}


(2)Compatibility of various datatypes and modifiers:-If we don't know which  modifier used with which datatype it causes a big trouble.So we can understand it by these link:
http://amarsinghbishen007.blogspot.in/2013/03/compatibility-of-various-data-types-and.html

(3)Unusual behaviour of signed and unsigned modifier with float:-Here is a link which describe us about this problem:-
http://amarsinghbishen007.blogspot.in/2013/03/unusual-behaviour-of-signed-and.html

(4)Right Shift of signed,unsigned char & int:-Here is a link which describe us about this problem:-
http://amarsinghbishen007.blogspot.in/2013/02/right-shift-of-signedunsigned-char-int.html

(5)using shift operator:-Here is a link which describe us about this problem:-http://amarsinghbishen007.blogspot.in/2013/01/shift-operator-signedunsigned.html

(6)We need to be very careful in giving a path for the file because if we left a \ (slash ) only then it does not give an error .We are unable to understand which type of error occurred .

Returning an array

Returning an array allocated by malloc gives an expected result.We can check it by two experiments:-

(1)By malloc in a function f() and printing it in a main
(2)By malloc in main and printing it in function f1

We use both method in a program to check if we return an array allocated by malloc gives correct result or not.Here is the program:-


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include<math.h>
#define size 50

int* f();
int* f1(int*);

void main()
{
int valueforb = 10;
int *aa=(int*)f();
int *bb = (int*)malloc(size*sizeof(int));
for(long unsigned int iterator = 0 ;iterator < size;iterator++)
{
bb[iterator]= valueforb ;
valueforb+=2;
}
f1(bb);
printf("\n");
//print aa in loop;
for(long unsigned int iterator = 0;  iterator < size ; iterator++)
{
printf("%d  ",aa[iterator]);
}
_getch();
}

int* f()
{
int * a= (int*)malloc(size*sizeof(int));
int value=0;
//initialise a with even numbers
for(long unsigned int iterator = 0 ;iterator < size;iterator++)
{
a[iterator]= value ;
value+=2;
}
return a;
}
int* f1(int* b)
{
for(long unsigned int iterator = 0;  iterator < size ; iterator++)
{
printf("%d  ",b[iterator]);
}
return b;
}
OUTPUT:-
10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48
50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82  84  86  88
90  92  94  96  98  100  102  104  106  108
0  2  4  6  8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  4
2  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  8
2  84  86  88  90  92  94  96  98

Here we allocated memory by malloc for a in function f and call function  then print its value in main we get the same value which is initialized in function f().In case of bb the value is initialized in main same printed in f1().Thus we must say that the array allocated by malloc is safe to return.

Now we can discuss returning an array allocated statically .There are two cases occur :-

1)If we declare an array with static duration
2)if we declare an array with automatic duration

1)If we declare an array with static duration:-If we declare an array in function f1 with static duration the value will be the remain safe in the main we can check it ny printing it.

2)if we declare an array with automatic duration:-If we declare an array in f() with automatic duration which is default duration .Then there the value will be unsafe . It means the auto variables are destroyed after there use and memory is cleaned up.

NOTE:-It may be possible if we returning an array from a function f which is allocated staticaly and have static duration the value of array in function main is same because sometimes memory is not cleaned up and in that case value will be same.

Here is the program for the issues related with returning an array having static allocation:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h> 
#include<math.h>
#define size 50
int* f();
int* staticFun();

void main()
{
int *aa=f();
int* bb=staticFun();
//print aa in loop;
for(long unsigned int iterator = 0;  iterator < size ; iterator++)
{
printf("%d  ",aa[iterator]);
}

printf("\n"); 

for(long unsigned int iterator = 0;  iterator < size ; iterator++)
{
printf("%d  ",bb[iterator]);
}
_getch();
}
int* f() 
{
int  a[size];
int value = 0;
//initialise a with even numbers
for(long unsigned int iterator = 0 ;iterator < size;iterator++)
{
a[iterator]= value ;
value  += 2;
}
return a;
}
int* staticFun()
{
static int b[size];
int value=10;
for(long unsigned int iterator = 0 ;iterator < size;iterator++)
{
b[iterator]= value ;
value  += 2;
}
return b;
}
OUTPUT:-

-858993460  7801500  4585992  1547167480  1  1547167469  2094676724  4586380  45
86128  2130567168  1  1  1  3  7806216  7800832  4586096  1547215168  547612116
 -2  4586024  1546466082  4586380  2130567168  4586128  4586024  1546360630  154
7513552  4586036  1546458873  17  4586112  1546485755  1  1547498680  1546485737
  2094676604  4586380  4586128  2130567168  1  1  3  4586124  -858993460  -85899
3460  4586444  1547215168  547632892  -2
10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48
50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82  84  86  88
90  92  94  96  98  100  102  104  106  108

We can see here from the output that the array declared as auto duration prints the other value in the main than intialized in f() while the array declared as a static prints the same value in the main as initialized in f1().

Saturday 6 April 2013

Storage class specifier


Storage class specifier declares that a variable or function declred 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


Here are some inportant notes about storage class specifiers:-

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 this more easily by following details:-

Object Storage Class Specifiers Storage Duration (Life Time) Linkage Scope (Accesibility/Visibility) Storage Area
Internal variable auto Automatic No Function/Block/Compound Stmt SS:stack     Initialized with garbage value
Internal variable register Automatic No Function/Block/Compound Stmt Register
Internal variable static static No Function/Block/Compound Stmt DS Uninitialized  DS:BSS Initialized with zero
Internal variable extern static (???) external (???) Function/Block/Compound Stmt None Initialized  DS:data
Internal variable none (default auto) Automatic No Function/Block/Compound Stmt SS:stack     Initialized with garbage value
Internal Function auto static (???) external (???) Project (multiple files) CS
Internal Function register invalid invalid invalid invalid
Internal Function static invalid invalid invalid invalid
Internal Function extern static (???) external (???) Project (multiple files) CS
Internal Function none (default extern) static (???) external (???) Project (multiple files) CS
External variable auto static external Project (multiple files) DS  
External variable register invalid invalid invalid invalid  
External variable static static internal File DS Uninitialized  DS:BSS Initialized with zero
External variable extern static external Project (multiple files) DS/None Initialized  DS:data
External variable none (default extern) static external Project (multiple files) DS  
External Function auto static external Project (multiple files) CS
External Function register invalid invalid invalid invalid
External Function static static internal File CS   Storage Area
External Function extern static external Project (multiple files) CS extern int i; None intialized to 5
External Function none (default extern) static external Project (multiple files) CS int i=5;  
   
thread_local thread No extern int i=6; DS:data intialized to 6
dynamic mem allocation Dynamic No   SS:Heap    
extern int i; None intialized to 0
Automatic No int i;  
Thread No
Dynamic No

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