Skip to main content

Half an Hour to Variables

I wrote this article yesterday when I was getting bored and was feeling too lazy, all thanks to the hot weather here. I planned of meeting my friends but at last I insisted on staying at home when I wrote this. Regarding the title, I thought that if it took me an hour to write it then you would have to hardly give Half an Hour to Variables. Now coming to the serious stuff…

What is Variable

During program run we need to manipulate data and the data to be manipulated are stored in memory locations known as variable. In the system memory, data are stored in memory locations such as 0x00243 which are not easy to remember if we have quite a lot of them. Variables are memory locations having a name so that different memory locations can be distinguished easily.

Declaration and Initialization of Variables

The declaration of variable generally takes the following form: type varname; Here type is any valid data type and varname is the variable name. Ex.

int age; float temp;

Static initialization: Initialization means giving initial value to a variable. Ex. int var=100; Here 100 is the static number with which the variable is initialized (given initial value). This is type of initialization is known as static initialization because the initial value given to the variable is known at the time of programming.

Dynamic initialization: Have a look at the following line of code: float avg=sum/count; H ere avg is initialized with a value that is not known at the time of programming and it may have different values during different program runs.(since sum and count on which the value of avg depends, may have different values under different circumstances)

Access Modifiers

Access modifiers change the way we access or modify a particular variable. The two types of access modifiers are discussed below:

const: By declaring any variable as const, we are telling the C++ compiler that the value of the variable will remain unchanged during program run. If we try to modify a variable declared as const, the compiler will report an error and the program won’t compile.

   //program to illustrate const Access Modifier
   //this program would RUN
   #include<iostream.h>
   void main(void)
   {
   const float pi=3.14;//declration and initialization of variable as const

   int rad;
   float area;
   cout<<"Enter radius of circle:";
   cin>>rad;
   area=(rad*rad)*pi;//here the variable is only being accessed an NOT modified

   cout<<endl<<"Area of the circle:"<<area;

   }

Let us have a look at another program:

   //program to illustrate const Access Modifier
   //this program would NOT RUN
   #include<iostream.h>
   void main(void)
   {
   const float pi;//declaration of variable as const
   pi=3.14;//variable is being modified, NOT ALLOWED
   int rad;
   float area;
   cout<<"Enter radius of circle:";
   cin>>rad;
   area=(rad*rad)*pi;//here the variable is only being accessed an NOT modified

   cout<<endl<<"Area of the circle:"<<area;

   }

volatile: By declaring any variable as volatile, we are telling the compiler that the value of the variable can change by ways other than explicitly defined by the program. In other words the value of the variable can be changed by programs or routines outside the program (say, by the Operating System). For example we can pass the variable declared as volatile to the operating system clock routine to automatically keep track of the time changes. It is necessary to declare those types of variables whose value can change outside the program as volatile. C++ treats variables in such a way that if we don’t and since the program is not modifying the variable’s value, the program may output unexpected results.

Related Articles:

Popular posts from this blog

Fix For Toshiba Satellite "RTC Battery is Low" Error (with Pictures)

RTC Battery is Low Error on a Toshiba Satellite laptop "RTC Battery is Low..." An error message flashing while you try to boot your laptop is enough to panic many people. But worry not! "RTC Battery" stands for Real-Time Clock battery which almost all laptops and PCs have on their motherboard to power the clock and sometimes to also keep the CMOS settings from getting erased while the system is switched off.  It is not uncommon for these batteries to last for years before requiring a replacement as the clock consumes very less power. And contrary to what some people tell you - they are not rechargeable or getting charged while your computer or laptop is running. In this article, we'll learn everything about RTC batteries and how to fix the error on your Toshiba Satellite laptop. What is an RTC Battery? RTC or CMOS batteries are small coin-shaped lithium batteries with a 3-volts output. Most laptops use

The Best Way(s) to Comment out PHP/HTML Code

PHP supports various styles of comments. Please check the following example: <?php // Single line comment code (); # Single line Comment code2 (); /* Multi Line comment code(); The code inside doesn't run */ // /* This doesn NOT start a multi-line comment block /* Multi line comment block The following line still ends the multi-line comment block //*/ The " # " comment style, though, is rarely used. Do note, in the example, that anything (even a multi-block comment /* ) after a " // " or " # " is a comment, and /* */ around any single-line comment overrides it. This information will come in handy when we learn about some neat tricks next. Comment out PHP Code Blocks Check the following code <?php //* Toggle line if ( 1 ) {      // } else {      // } //*/ //* Toggle line if ( 2 ) {      // } else {      // } //*/ Now see how easy it is to toggle a part of PHP code by just removing or adding a single " / " from th

Designing a Simple Order Form Application in PHP

Ok guys, for this post we’re going to create a simple yet complete order form page. Order forms are used on many sites to take customers order online. Order forms should have the capability to take orders from visitors regarding what items they want to purchase and store the information for further processing. For this post’s example, we are going to create an order form for a Book Seller. The form will be designed to take order of five different items (books). Our order form application should be able to take order of five different items in any separate quantities tht user wants, it should also ask for shipping address and name of the customer. It should then store the information provided in a file along with the date and time order was placed. The application should also be able to take any number of orders and store them all linearly for further human processing. For this, we need a front end of a HTML form to which the user would interact a