| You are here: | About>Computing & Technology>C / C++ / C#> Getting Started> What is a Variable? |
![]() | C / C++ / C# |
What is a Variable?What Exactly is a Variable?: A variable is the name for a place in the computer's memory where you store some data. If you are new to programming, please take a look at these articles first. These will give you an overview on programming.A Real World Example of Variables: Imagine a very large warehouse with lots of storage bays, tables, shelves, special rooms etc. These are all places where you can store something. Lets imagine we have a crate of beer in the warehouse. Where exactly is it located? We wouldn't say that it is stored 31' 2" from the west wall and 27' 8" from the north wall. In programming terms we also wouldn't say that my total salary paid this year is stored in four bytes starting at location 123,476,542,732 in RAM. Data in a PC: The computer will place variables in different locations each time our program is run. However our program knows exactly where the data is located. We do this by creating a variable to refer to it and then let the compiler handle all the messy details about where it is actually located. It is far more important to us to know what type of data we will be storing in the location. In our warehouse, our crate might be on section 5 of shelf 3 in the drinks area. In the PC, the program will know exactly where its variables are located. Variables are Temporary: They exist just as long as they are needed and are then disposed of. Another analogy is that variables are like numbers in a calculator. As soon as you hit the clear or power off buttons, the display numbers are lost.
How Big is a Variable?As big as is needed and no more. The smallest a variable can be is one bit and the largest is millions of bytes. Current processors handle data in chunks of 4 or 8 bytes at a time (32 and 64 bit CPUs), so the bigger the variable, the longer it will take to read or write it. The size of the variable depends upon its type.What is a Variable Type?: In modern programming languages, variables are declared to be of a type. Apart from numbers, the CPU does not make any kind of distinction about the data in its memory. It treats it as a collection of bytes. Modern CPUs (apart from those in mobile phones) can usually handle both integer and floating point arithmetic in hardware. The compiler has to generate different machine code instructions for each type, so knowing what the type of variable helps it generate optimal code. What Types of Data Can a Variable Hold?: The fundamental types are these four.
Example of Data Types:
Where are Variables Stored?: In memory but in different ways, depending on how they are used.
Conclusion:
Variables are essential to procedural programming, but it is important not to get too hung up on the underlying implementation unless you are doing systems programming or writing applications that have to run in a small amount of RAM. My own rules regarding variables are
|
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | Print this Page | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


