Intro to datatypes

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
This is just a simple introductory tutorial I made to help someone out and I posted it on my website. Its not exactly professional, but some people should be able to learn from it.

Vocabulary
Before you start reading this tutorial you should know what 'casting' means:
Casting: Changing one datatype to another
One of the most annoying problems that beginners encounter when working with C/C++ are data types. They seem really strict and confusing at first, but I'll show you that they're not so bad in this tutorial

*the sizes of the data types listed below are not exact since they vary from system to system. These are just to give you an idea of how big they are, though generally you wouldn't have to worry too much about them.*

What are they?
A data type is just what it sounds like: a type of a data, or variable. for example, an integer would look like this:

Code:
int x = 0;
the word 'int' tells the compiler that you want x to be an integer, which can be a number from -2147483648 to 2147483647
Other data types like double and floats can also be used as numbers, and can even be interchanged since the compiler usually casts those 3 automatically!

Code:
int x = 0;
double y = 3.147;
float z = 2.55;
y = x+z;
as you can see in the example above, a double is being set to the sum of an integer and a float. Compilers usually aren't very strict about those types, but you should always try to work with similar data types.

Why not use 1 type for everything??
The reason that there aren't number and string datatypes is that memory is a very valuable resource. As a desktop developer you generally don't have to worry too much about your data usage but on mobile devices, consoles, PDA's, and anything with a microprocessor, your memory usage must be carefully monitored since there isn't that much available.
Each data type has its own size and advantages:
Integer: 4 bytes
Double: 8 bytes
Float: 4 bytes
Floats and Integers are the same size, but they are different types because a float can be a decimal, but an integer cannot. Now you might be asking: why not make integer have decimals and get rid of float? well its because an integer is able to have a bigger range since it doesn't have to carry decimal numbers. Don't stress if you don't understand this, just play along until you get more experienced :)
Doubles are the biggest datatypes because they are 8 bytes, so basically its an integer combined with a float.

Other data types
Ofcourse Integers, Floats, and Doubles aren't the only data types, there are also bools, chars, and wchar_t. Here is a summary of what each of them do:
bool:
size: 1 byte
values: true or false (1 or 0)
char:
size: 1 byte
range: -128 - 127
values: can be a number or a character like 'a'
wchar_t:
size: 4 bytes
values: 1 wide character, like unicode ? ? ? ?
suggest you don't use this until you understand it since its pretty big

booleans(bools) are true or false and shouldn't be used in any arithmetics just in conditionals and statements:

Code:
bool quit = false;
   if(quit){
      return 0;
   }
chars are usually used for strings and words:

Code:
//A single letter
char letter = 'A';
//You can make a whole word using an array
char name[4] = 'Alex';
wchar_t is used to make unicode characters. They must be prefixed with an L when being created:

Code:
wchar_t x = L'X';
I wont get too detailed into this since its rarely used and I don't know enough about it to be a credible source

Summary
Here is a list of the data types and their sizes for future reference.
int
Size: 4 bytes
Range:
float
Size: 4 bytes
Range: 1.175494351 E - 38 - 3.402823466 E + 38
Values: decimals e.g. 1.5, 2.46
double
Size: 8 bytes
Range: 2.2250738585072014 E - 308 - 1.7976931348623158 E + 308
bool
Size: 1 byte
Range: true or false(1 or 0)
char
Size: 1 byte
Range: -128 - 127
Values: small integer, or character(eg 'a')
wchar_t
Size: 4 bytes
Range: 1 wide character(unicode, like ? ? ? ?) Other: character must be defined like L'?', eg L'?'

Well thats it for this tutorial, hopefully you read everything and learned something from it. good luck on your future programming :)

Please post what you think in the comments below

any suggestions to improve it are greatly appreciated

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top