Tuesday, December 13, 2011

Tutorial - C++ - 1.DataTypes

ok , im going to go over some of the DataTypes and thier uses here.
the syntax is
datatype variable = value;

"int" which stands for integer, it can hold a number value ranging from -2147483647 to 2147483647

, (if you are using a different compiler then this might vary).
so if i wanted to assign the value 99 to my variable x then i would do the following.


int x;
x = 99;
 here i am declaring that is an integer , and then later assigning 99 to it

OR

you could do it all in one line.

int x = 99;

next is "char" which stands for character , and it can hold 1 character .

so if i wanted to assign the letter "D" to my char xthen i would do the following .

char x = 'D';

or

char x;
x = 'D'

you have to put single quotes( ' ) around the character you want stored , the letter can be upper or lower case .

just so you know , the letter that ive been using (x) is called an identifier , you can make this whatever you want , so int myinteger = 69; is valid aswell.

next is "float" which stands for "floating point variable" it is like int , but with the exception that it can hold a decimal value , and its range is -1.17549e-38 to 3.40282e+38
but that is just how precise it can be. so you can still assign it a value greater than that , like 999999.9 and it will still work.


float x = 99.4;


next is "double" its pretty much a much more precise float, its rang is -2.22507e-308 to 1.79769e+308 which just like float is its max precision , so assigning the value 9999999999999.999999999 will work .


double x = 89791723467.2374574;


next is "bool" which stands for "Boolean" , this variable can only hold true, or false (or 1=true , 0=false) . bool is mainly used for loops and switches, which will be covered in later tutorials.


bool answr = true;

OR


bool answr = 1;

the two lines mean the same thing , that answr is true

you must assign a data type(int , char , double ect.) to your variable, or else the compiler will give you an error saying that it wasnt defined.

EXAMPLE CODEs

int x = 8;
int y = 2;
int answr = x+y;
cout << answr;


that bit assigns 8 to x and 2 to y ,then x is added with y and their value is stored in the variable answr. So the value of answr is "couted" and its 10.

-----------


int x = 88;
double y = x;
cout <<y;



this bit assigns and int value 88 to x , then assigns the value of x to a double y . then cout's the value of y, which is 88. this is a valid thing to do , but if you tried this


double y = 50.8;
int x = y;
cout << x;


then the value of x would be 50 ,becuase int cannot hold a decimal value , so it is dropped.

-------
char x = 'dvf';
cout << x;


that will output f since char cannot hold more than 1 letter/character , so it is assigned the last character.
but you shouldnt do that anyway , if you wanted to assign a bunch of text/words to a variable you would have to use the string variable (which will be covered in another tutorial)
so remeber char is only for 1 character.

ok that is it for now , ill probably fix up this later , its messy and unfinished

No comments:

Post a Comment