We provide the tools, you provide the imagination

In Association with Amazon.co.uk

Which section do you think needs the most work?
Choose one of the following-

Articles
Books
Free Software
Professional Software
Forums


Current Results

Variables


A fundamental part of programming

.

A variable stores data that can be changed over time. Before using a variable in your program you must declare it by specifying its data type and name.

You can give a variable any name you want but make sure its descriptive of what the variable contains. If the variable stores someone's age you can simply call it age.

Before you can use a variable you must declare it. Declaring a variable gives it space in memory.

 

The kind of data a variable will be able to store will depend on what data type it is assigned. So for example if you want the variable to store someone's age then you should assign the data type as int because the int data type stores numbers.

Variable types

Type Name
Full Name
Type Description Examples
int
Integer
A whole number
16, 89, -52
float
Float
A decimal point number
51.80, 6.23, -65.12
char
Character
Any keyboard key
g, $, *
bool
Boolean
Can simply be true or false
true, false


An Example

Type in, or copy & paste the code below into the text editing area of Dev C++ -

#include <iostream.h>
#include <stdlib.h>

main()
{

int age = 80;

cout<<"Your "<<age<<" years old?"<<endl;
return 0;

}

Remember C++ is case sensitive. So while c++ will accept int as the data type it won't accept INT.

Now all you have to do to create your program is click Compile and Run from the Execute menu. Enter the name for the source file and wait for the compiler to build the code. The time it takes to compile depends on the speed of your computer. If the program won't compile properly then either the code hasn't been copied correctly or the compile settings are incorrect. For some help post a message in the forum.

Let me explain the example line by line,

int age = 80;

This variable for age is declared as an int (that's a whole number if you forgot) and is given a value of 80.

cout<<"Your "<<age<<" years old?"<<endl;

Outputs the value of the age variable as part of a sentence.

 

 

 

Website Design Guide - Vioxx Lawsuits