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

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

How to Create an HTML Form Linked with MySQL Database in PHP

If you're looking for example code and detailed discussion on how to create an HTML form that stores data in a MySQL database using PHP then this post might be what you're looking for. I assume that you're familiar with basic HTML, CSS, PHP coding, and  MySQL. I am going to divide this small project into two parts: The HTML form itself that takes input from the user and the PHP script that saves it into the database A table that displays the user-added data that has been saved in the database. We'll be dealing with the first part in this tutorial. Again I'd like to break this problem into a few parts so that it's easier for you to understand and probably gives you an insight into how breaking up a problem into smaller chunks can help make things clearer in your mind. Let's think about it, there is an HTML form (that is HTML code), then there is the PHP code that deals with the user-input data, and the MySQL database itself. For this tutorial, we'll b...

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