ChEaTdEViCe
WELCOME TO CHEAT DEVICE
GTAOnline popping here



 
HomeHomeCalendarGalleryFAQSearchUsergroupsRegisterLog in

Share | 
 

 Lesson 2 - Variables and Data Types

View previous topic View next topic Go down 
AuthorMessage
DarK_DemoN
Member
avatar

Points Points : 6144
Posts : 381
Reputation : 25
Join date : 2011-01-25
Location : Hacking Kingdom

PostSubject: Lesson 2 - Variables and Data Types   Tue Jan 25, 2011 12:02 pm

Lesson 2: Variables and Data Types

In this lesson I will teach you about Variable and Data Types... as the title suggests. :P

#1 on the list: Identifiers

Identifiers are keywords, or sequences of characters, used to define a variable. This variable is then saved in the system. Each variable type has it's own maximum size.
For example a standard int can hold a maximum of 4 bytes of data.

The most common identifiers are: int, char, and void. These are used to identify variables.

Other identifiers are: asm, auto, bool, break, case, catch, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, volatile, wchar_t, and while. These identifiers are all used to do different things. For example: while and do are used in loops. True and false are simply just boolean operators.

I'm going to start with the first three: int, char, and void.


INT
int stands for integer. It declares any integer (your average number).
The correct way to declare an int is very simple. First you type in the identifier (int). Afterwards, you name the variable. You can name your variable anything (as long as it doesn't correspond with any keywords used in the C++ language; no spaces are allowed; neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid.). You put a semicolon at the end of the line.
Code:
int myInt;
The above code declares an integer called 'myInt'. This integer can hold one number in it. It could hold '4' if you wanted it to, or maybe '423'. Smile

To set the value of that variable you would do the following:
Code:
int myInt;
myInt = 3;
With this code, I stored the number '3' in myInt. Fairly simple. There is an even easier way to do that. You can declare your values when you make the variable. Like so:
Code:
int myInt = 3;
If we displayed the variable from both of those ways, they would both display '3'.

CHAR
char stands for character. It declares a variable that holds ONE character. Not three characters, not -1 characters. ONE character. If you try to enter more than one character, it will only take in the first character it sees. It will chop off the rest and throw it away.

The way to initialize a char is the same as an int.
Code:
char myChar;
In the above code, I created a char called myChar. Presetting a value to a char is a little different than ints.
Instead of putting:
Code:
int x = 10;
You would say:
Code:
char x = 'h';
The above variable is now storing the letter 'h'.

VOID
Void is not used very much in modern C++. It means that the variable/function, that it declares, does not have a identifier. You cannot declare a variable of type void.
Code:
void x = 2;
This is incorrect. You WILL get an error. You CANNOT define a void variable. Sometimes people void their main() function. This is possible, but I find that it accomplishes nothing. If you don't declare your main() function as a void function, then you cannot return any return values. I'll get into that later. Very Happy



OTHER IDENTIFIERS
There are many more identifiers. I am not going to cover ALL of them, just because you don't need them all, and it would take forever. So, I will briefly touch the most important ones. Smile

Bool
A bool is an identifier that stands for boolean. It uses a boolean operator. True or False.

Short
Short is an identifier that initializes a type of integer. A very short integer. It's range of numbers is -32768 to 32767 (signed) and 0 to 65535 (unsigned).
Code:
short x = -32768;

Long
Long is also an identifier that initializes a type of integer. As the name suggests, this is a very, very big number. It's range is -2147483648 to 2147483647 (signed) and 0 to 4294967295 (unsigned).
Code:
unsigned long y = 429467295;

Float
Initializes a floating-point number. Floating Point describes a system for representing numbers that would be too large or too small to be represented as integers. This is also known as scientific notation.

Signed
Signed means to allow negative and positive numbers in this variable. If you define a variable and don't declare it as either signed or unsigned, it will automatically set it as signed. It's cool like that.
Code:
signed int z;

Unsigned
Unsigned means that the variable will only accept positive number. This generally allows for larger numbers.
Code:
unsigned long int x;

Const
Const means that the variable it is attached to will NEVER change. It will stay the same value throughout the entire program. If you type in:
Code:
const int x = 4;
x = 5;
Then you displayed the result. You would get this:


#DEFINE

The all powerful #define is the last thing you will learn in this lesson. You can use this to set text commands. Almost like macros.
#include

using namespace std;

#define PI 3.14159

int main()
{
cout << PI << endl;
return 0;
}
If you type out this program, and run it, you will see that it prints the number 3.14159 to the screen.

There are a number of different ways you can use this. Ex:
#define NEWLINE '\n'
I haven't taught you about the escape codes yet, but that code (\n) is an escape code that is short hand for skipping to a new line. Well with this define statement you can type in 'NEWLINE' instead of having to recall the escape code.
cout << NEWLINE;
You probably won't forget that escape code, but you get what I mean.

That is the last thing for Lesson 2. Lesson 3 will be on Basic Input and Output (I/O).

Click this link for the Lesson 2 video on Youtube: [You must be registered and logged in to see this link.]
Back to top Go down
View user profile http://1337codez.co.cc
 

Lesson 2 - Variables and Data Types

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
ChEaTdEViCe ::  :: Programming :: C++ Programinng Basics-
Free forum | © phpBB | Free forum support | Contact | Report an abuse | Free forum