Saturday, 5 July 2014

Declaration and Initialization ( Part3 )

Declaration and Initialization (C-FAQ's)



Q1: Can you find an error ?


            #include<stdio.h>

            int main()

            {

                        int * iPtr, iNum;

                        char *cPtr, ch;

                        void *vPtr, vData;

                        iNum=10;

                        iPtr=&iNum;

                        printf("%d\n", *iPtr);             

                               

                        return 0;

            }

 


            Output:        
                                Compiler will return an error: size of vData is unknown.
                        It is allowed to define a pointer as a void type but void type variable definition is not allowed.


Q2: What is the output of the following code?

          #include<stdio.h>

            int main()

            {

                   struct stud

                        {

                                    char name[20];

                                    int rollNo;

                                    int age;

                        };


                        struct stud student = {"xyz"};

                        printf("%s %d %d \n",student.name, student.rollNo, student.age};

                        return 0;

            }



            Output:         xyz 0 0
                        When an structure is partially initialized, the remaining elements are initialized to 0.

                                   

Q3: What is the output of the following code?

          #include<stdio.h>

          struct stud

            {

                        char name[20];

                        int rollNo;

                        int age;

            }


            int main()

            {

                        struct stud student={"xyz"};

                        printf("Name: %s \n", student.name);

                        return 0;

            }

           


            Output:        
                                Compiler will return an error " Expected identifier ; before int  main".


Q4: What is the output of the following code?

          #include<stdio.h>

          struct stud

            {

                        char name[20]="xyz";

                        int rollNo=20;

                        int age=18;

            }student;


            int main()

            {

                        printf("Name: %s \n", student.name);

                        return 0;

            }

           


            Output:         Compiler will return the following error. Because it is not allowed to initialize the members of structure at the time of declaration.
                               

1
2
3
Line 4: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
In function 'main':
Line 11: error: 'struct stud' has no member named 'name'




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