Variable (part 1)

    


  

     

                               variable in c...

  In today's lesson,we are going to talk about variables in C programming. Think of a glass of water.If you want to drink some water, then you put water inside glass and drink it. You can think of a variable as a glass of water.As a glass can store water in it similarly variables can store some value in it.After storing  the value,you are free to use this  value in your program.

           In reality, variables are simply names that points to some memory  location.Thanks to the inventor of C language who makes it easy for us to store values in memory by simply using the name of our choice.You don't need to worry about at which location of the memory your value is stored. Your task is to just provide the name and internally it will point to some memory location.But there is one thing that we need to be sure about.You must have to declare variables before using it in your program.Declaration simple means,announcing the properties of the variable to the compiler. Here, by saying properties we mean that what will be the size of the variable and what will be the name of  the variable Don't worry, it is not a very difficult job to do.You will see in a moment how easy it is to declare a variable.

      There is one more term called definition of a variable,which means allocating memory to a variable.Most of the time declaration and definition will be done at the same time.But this is not always the case.It depends on the modifiers you had mentioned with the variables. We will talk about modifiers later and then we will also explain how declaration and definition is different for different modmodifier Let me give you an example of how to declare a variable.Here, the name of the variable is var and it has an  integer data type.Data type simply means how much space a variable is going to occupy in the memory.Therefore by writing....... int var you are declaring a variable along with that you are requesting the compiler to allocate memory for this variable.  Please note down on one thing here after int varwe have put a semicolon over here.This is very important.Because this is how your compiler separates one statement from the other.So don't forget to add a semicolon at the end of the variable.

     Now, the memory location depends upon the type you use Here because in our example,we are using integer data type therefore it may take either two  bytes of memory or may be four bytes of memory.It purely depends on the system you are working on. Again you  don't have to worry about how much space is allocated to your variable.You only have to concentrate on the declaration and definition of the variable If you assign some value to a variable,at the time of declaration itself Then this thing is called initialization.This is how you will do initialization. Note- initialization of a variable doesn't mean that you cannot change the value of a variable afterwards in your code.If you want the value to be changed somewhere,then you can do that this clearly captures the meaning of variable.Variable in its pronunciation has word vary.Something that can vary over time.On the other  hand constant is just the opposite.once defined will never change. 

                  Let's see how we can change the value of the variable after  initialization.Let me write down the code here.I am writing .........#include that is our header file because we have to print some output. Then I am declaring main function.Inside this main function,I'm first declaring a variable named var.Suppose I initialize it first to 3,and afterwards I change this value to 4.You can observe that again I have not written int var in this statement when I'm assigning value to it.because the memory is allocated to the variable once and defining it once again means you want to allocate memory again for the same name. And this is illegal.Note- each variable must be defined only once but it can be used multiple times with different assignment in your program.But there is one exception as well.when we study scope rules later you will understand that same variable name can be defined in different blocks of code.You will slowly understand what does it mean.For now it is enough to know that inside this main function you cannot define multiple variables with the same name.Then suppose we write,printf with %d and we print the value of the variable.I will explain you more about what this %d means.For now, I can only tell you that this will print the  contents of whatever is there inside this variable var you have to put at  the end return 0 Let me save this.And build and run the code.You can see the output here which is 4. Previously I had initialized the Value of 3, and then changed it to 4 and print it.And as expected,my output is equal to 4 without any error.

               You can also assign variable to a variable instead of this constant value 3 or 4.int var1 is equal to 3. Suppose my new variable is var1,I declare another variable var2 and I initialized or you can say I assigned value to it  with the value of var1.Here you can see that I am assigning the value of 3 actually because writing the name of the variable means that I am assigning its constant value and I'm assigning it to variable 2. Lets print it using printf.Lets debug As expected, the output turns out to be 3.You can also assign same values to different  variables in a single line.As you can see I declared and defined variables in the same line using the same data type.All these variables are of integer type.Instead  of defining and declaring in different lines it is better to write them down  in a single line.Suppose I assign them the value equal to 4 all in the same  line and then I print it.

            Lets build and run.And as expected we get the value 4 4 4 because we had printed three times.There are three different variables var1 var2 var3. Because all of them has been assigned to the same value 4 That is why we are getting three values of 4 in the output. 


कोई टिप्पणी नहीं:

एक टिप्पणी भेजें

Data Types in C (part 2)

        Data Types in C.......             Today, we will continue our discussion on integer data type. Our outline for today...