Monday, 20 August 2012

Variable Datatypes in C++

Variable Names and Values 


If you're just beginning programming language you're probably lost to what exactly I am talking about. So I will try break it down for you. Varibles my words are anything that can hold a value and has a name for example
a = 20;//variable is called 'a' and the value of that variable is '20'

number = 32;//variable is called 'number' and the value of that variable is '32'

anything = 12;//variable is called 'anything' and the value of that variable is '12'

So the above example is a basic idea of what a variable is, So a variable has a name, a value and Datatype (N.B datatype in not in the above example) but we will soon get into that for now we are focusing on the name and value of the variable.

A variable can have any name that you choose as long as its not from a keyword of that programming language, for example in c++ we just learnt about the cout function we can not have a variable that looks like the below


cout = 12;//cout is a keyword so you can't use it as a variable name

If you do the above you will get a syntax error that mean something is wrong with your code basically as we go along you learn more about other errors

Something to note is that in C++ like in many programming language variable names are case sensitive or the Capital and common letters matter if a variable is called 'name' it's a totally different variable from 'Name' see example below

name = 1;
Name = 2;

So if I decide to display the value in 'name' it will show 1, but if I decide to display the value in 'Name' it will show 2. This is what I mean when i say two totally different variables some example are below to show that if any letter is change to upper case(Capital letter) or lower case(common letters) it make the variables different

name = 1;
Name = 2;
nAme = 3;
naMe = 4;
namE = 5;
NAME = 6;
NaMe = 7;

All the above variables are all different hope you finally get the concept and in C++ our variable name SHOULD be lower case, good programming practice not because it will gives you errors (which it wont if its not lower case) but just the fact that you will find out everything will move smoother and faster when everything is uniform and it only looks professional.

So you next question is probably, can I have a space in my variable's name? The answer is simply NO what we do is use camel case, for example say the name of the variable say the variable name is 'date minutes' what we do is shown below
dateMinutes = 12;//value has no specific significant purpose in this example

Variables Datatype

Datatype this is basically the different classifications of the type of values a variable can hold. for example. int  a; means that variable 'a' will and can only store integer values, what are integers? basically these are positive and negative whole numbers eg -1,0,1,2,3,4,5,6. look at the examples below for a better understanding and how datatype are setup.


#include<iostream>//iostream hearder file

using namespace std;//get all library in the header file

int main()
{
//int has a value that
//ranges -2147483648 to 2147483647
//whole numbers only
int a = 1;

//double has a value that 
//ranges +/- 1.7e +/- 308 (~15 digits)
//contains decimal
double c = 2.333;

//float has a value that 
//ranges +/- 3.4e +/- 38 (~7 digits)
//contains decimal but less whole numbers
//than double
float b = 0.333333;

//bool short for boolean can 
//only hold values true or false or 0 or 1 
bool d = true;

//char short for character can 
//hold only one character at a time
//and char values are enclosed by single quotas
char e = 'a';

//string is more of a class than a datatype
//but what use string is to do is to store text
//and string values are enclosed by double quotas
string f = "Hello World";


cout << "a = " << a <<endl; 
cout << "b = " << b <<endl;
cout << "c = " << c <<endl;
cout << "d = " << d <<endl;
cout << "e = " << e <<endl;
cout << "f = " << f <<endl;
system("pause");
}



Hope you have better understanding of datatype from the example above. we will using the a lot from now on.

Okay so I may have gone a head too far a beginner so i'm gonna try to fix it, So okay the = means your going to assign a value to a variable in the above code line 10 variable a is being declared, that mean your creating a variable in memory to hold some data in this instance that the datatype is int so it will store integer values and you will notice that a values is being assign to a  that value is 1, now at line 38  this is where the variable a is send to be displayed, by using cout. A example of display a variable is just below to help clarify things.


#include<iostream>//iostream hearder file

using namespace std;//get all library in the header file

int main()
{
//declaration of a string 
//with value "HellWorld"
string anyText = "Hello World";

//declaration of a string 
//with value "How are you?"
string camelCaseText = "How are you?";

cout << anyText <<endl;

//with no space between the two varibles
cout << anyText << camelCaseText <<endl;

//with space between the two varibles
cout << anyText << " " << camelCaseText <<endl;

//with endl between the two varibles
cout << anyText << "\n" << camelCaseText <<endl;

system("pause");
}



The code explains it self hope you learn something from my lesson.

No comments:

Post a Comment