Saturday, 5 July 2014

Declaration and Initialization ( Part2 )

Declaration and Initialization (C-FAQ's)



Q1: Differences between a Declaration and a Definition ?


            When we define a variable, a space is reserved for the variable and it also got some default value. But declaration only identifies the type of a variable.
               
                Redefinition is an error. But re-declaration is not an error.
               
                Example:-
                        extern int iNum;
                        float fVar;
                                    Here extern int iNum is a declaration and float fVar is definition.

 


Q2: Can you identify which of the following are declarations and definition ?

          extern int iNum;

            double dVar;

            void vFunc(int, float);

            int getVar() {}


            Output:        
                        extern int iNum       is a declaration
                        double dVar             is a definition
                        void vFunc(int, float)        is a declaration
                        int getVar() {}          is definition


Q3: Will the following code compile fine. If it is what will be the output ?

          #include<stdio.h>

            int main()

            {

                        extern int iNum;

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

                        return 0;

            }

            int iNum=30;

 


            Output:        
                                There is no error in the above code, this will compile fine.
                        It prints 30.

Q4: What is the output of the following code?

          #include<stdio.h>

            int main()

            {

                        extern float fNum;

                        printf("%f\n",fNum);

                        return 0;

            }

           


            Output:        
                                Compiler will return an error "Undefined fNum". Because extern float fNum is a declaration not a definition.




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

No comments:

Post a Comment