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 >>

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 >>

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 >>


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 >>

Monday, 19 May 2014

How to solve Rubik’s Cube Google Doodle

Hello Friends,

            This post is not related with any programming concept, this is for FUN !!

            How many of you have solved Rubik’s Cube Google Doodle? If not then I will tell you how you can do that in very minimum moves.

            Steps :-

1)    Open Rubik’s Cube Google Doodle





2)    Use your keyboard

Press the following key sequences:-

  L s L L D D r B u r D L R F D D L u





Please re-share it, if you solved it J

Let me know from your comments, if the solutions works for you !!

Friday, 28 February 2014

Horizontal Marquee

Hello Friends,
            Welcome you all in the Android Development Tutorial.
            In this post we are going to learn “How to create a horizontal marquee banner with the help of Graphical Layout”.

            There are two ways of creating User Interface for an Android Application.
1)    Code
2)    Drag and Drop

Suppose I want to put a TextView widget in my Android Application, How do I do? First way is I write a code for creating a TextView. Another way is, use the widgets provide by ADT for creating a User Interface.

So today we will create a horizontal marquee without any coding. I mean with the help of Graphical Layout. Just change the properties of the widgets, according to that code will also get reflected automatically.

Steps to follow:-

1)    Create a new Android Application

Create a new Android Project.
Go to File à New à Android Application Project
Change the Application name to “Horizontal Marquee”.
Select all the default options.


Click Next
Click Next
Click Next
Click Next
Finish

Now you will have a HorizontalMarquee application on the Screen.

The Graphical View you are seeing, that is called as a Graphical Layout of the activity_main.xml

You are also seeing a default TextView (Hello World) in the middle of the GraphicalLayout.

We will modify this TextView properties to demonstrate Horizontal Marquee.

Zoom the Graphical Layout by the help of zoom in icon.



2)    Modify the TextView Properties

Click on the default TextView (“Hello World”).
On the right hand side you will be seeing a window names as Properties.



Whatever widget you will select it will represent the Properties of that Widget.

 From here we will modify the following TextView Properties.

Change the default string:-



      Go to the Text ID in TextView, click on the small right hand side button.
      Click on the New String button.
When you click you will see the following window.
Change the String Value, please give any long text here.

Change the New R.string to marqueeString
Click Ok
Click Ok
                        Your default text has been changed.

Change the Ellipsize :-



      Go to Ellipsize ID in TextView.
      Click on the right hand side small button.
      Select the marquee.

Change the Marquee Repeat Limit :-



      Go to Marquee Repeat Limit ID in TextView.
      Click on the right hand side small button.
      Select marquee_forever.

Change the Focusable and Focusable In Touch Mode:-



      Go to Focusable ID in View.
      Click on the checkbox, make it TRUE.
      Also change the Focusable In Touch Mode to TRUE.

Change the Single Line:-



      Go to the Deprecated section, select the Single Line ID and make it True by clicking on the CheckBox.

Modifications have been done. Have a look of the activity_main.xml file. Whatever properties we have modified, code related to that has been automatically added in activity_main.xml


3)    Run the application

Save all the changes. Do clean and Build.
Run the Application.
Your application will come up with the Horizontal Marquee floating in the middle of the Screen.




Please keep sharing and give your suggestions / comments.