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

How to Remove Bullet Points in CSS

This will be a short guide on how to remove bullet points from ordered/unordered lists with CSS, you only need two lines of CSS for this. The first removes the actual bullet points and the second one removes the space to the left, as evident from the following video: We'll also be doing some bullet beautification in the last section if that's what you are looking to do. How to Remove Bullet Points in HTML/CSS Now that we know what CSS properties actually accomplish what we want let's see how we can implement this in our HTML code. Using Inline "style" Tag (The quick and dirty way) As the title suggests, this is the quickest way to remove bullet points in which you wouldn't have to edit any CSS files (for example, for Blogger or WordPress). This method would be useful for a one-off case - just add the following " style " attribute to the list you want to remove the bullet points from: <h1>Ordered List</h1> <ol  style="list-styl