Saturday, 5 July 2014

Declaration and Initialization ( Part4 )

Declaration and Initialization (C-FAQ's)



Q1: Can you find an error ?


            #include<stdio.h>

            int main()

            {

                        int iArr[5] = {-2, 100 };

                        int i=0;

                        for(i=0; i<5; i++)

                        {

                                    printf("iArr[%d] = %d\n ", i , iArr[i]);

                        }                 

                        return 0;

            }

 


            Output:        
                                There is no error in the code. Here iArr is an integer array, and it is partially initialized.  So the reset of the elements of iArr will be initialized by zero.
      iArr[0] = -2
        iArr[1] = 100
        iArr[2] = 0
        iArr[3] = 0
        iArr[4] = 0



Q2: What is the output of the following code?

          #include<stdio.h>

            struct stud

            {

                        char name[20];

                        int rollNo;

                        struct stud next;

            };

 

            int main()

            {

                   struct stud student={"xyz"};

                        printf("rollNo = %d",student.rollNo);

                        return 0;

            }



            Output:         Compiler will return an error. Because we are trying to create a variable next of struct stud type. This time struct stud declaration is an incomplete state, so compiler can't calculate the size of next.
1
2
Line 6: error: field 'next' has incomplete type

                                   

Q2: What is the output of the following code?

          #include<stdio.h>

            struct stud

            {

                        char name[20];

                        int rollNo;

                        struct stud * next;

            };

 

            int main()

            {

                   struct stud student={"xyz",20};

                        printf("rollNo = %d",student.rollNo);

                        return 0;

            }



            Output:        
    rollNo = 20

            No error in the above code. Because here we are trying to define a variable next which is  pointer to struct stud . Here compiler can get the size of next, because all pointers reserve the same size in memory.
            When an structure contains a pointer to itself, they called as a self referential structure.



Q4: What is the output of the following code?


            int main()

            {

                        enum state {idle , running , stopped , invalid } ;

                        printf("idle = %d\n",idle);

                        printf("running = %d\n", running);

                        printf("stopped = %d\n", stopped);

                        printf("invalid = %d\n", invalid);


                        return 0;

            }

           


            Output:
     idle = 0
      running = 1
      stopped = 2
      invalid = 3




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