Saturday, 5 July 2014

Declaration and Initialization ( Part1 )

Declaration and Initialization (C-FAQ's)



Q1: What is Scope of a variable ?


Scope of a variable is the region over which that variable is accessible.

            There are following types of scopes :-

            1)        GLOBAL SCOPE

                        Its means that a variable which have global scope, can be accessible from anywhere  in the program.

                        Example:
                                    int iNum=0;             
                                    int main()
                                    {
                                                printf("%d",iNum);
                                                return 0;
                                    }
                                    variable iNum is having a global scope, because it is declared outside of all the functions.
           
            2)        FUNCTION SCOPE

                                Its means that a variable can be accessible only inside the function where it is defined.

                        Example:
                                    void func()
                                    {
                                                int iNum=0;
                                                printf("%d",iNum);
                                    }
                                    int main()
                                    {
                                                printf("%d",iNum);
                                    }
                                                variable iNum is having a function scope, it is accessible only inside the function func(). If try to access that function outside, compiler will return error.

            3)        BLOCK SCOPE

                        Its means that a variable is accessible only inside a block where it is defined.

                        Example:
                                    int main()
                                    {
                                                {
                                                int iNum=20;           // BLOCK SCOPE
                                                printf("%d\n",iNum);
                                                }
                                                printf("%d\n",iNum);      
                                                // CAN'T ACCESS here
                                    }
                                    variable iNum is having a block scope, it is accessible only inside the block. If try to access outside that block, compiler will return error.

 

Q2: What will be output of the following program  ?

          #include<stdio.h>

            int main()

            {

                        int iNum =100;

                        {

                                    int iNum =20;

                                    printf("%d ",iNum);

                        }

                        printf("%d\n",iNum);

                        return 0;

            }


            Output:         20 100
                        Here the first printf() will print 20, because it is accessing the iNum that is having a block scope. The second printf() will print 100 because it is accessing the iNum that is defined in main() function.





<< PREV               [C FAQ's]            NEXT >>