Friday 12 April 2013

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.







No comments:

Post a Comment