this one confused me.
Lesson #3 - Variables/Data Types
The first thing i'm going to do is take a little experiment off here:
I want you to remember:
A = 6
B = 9
C = 3
D = 3
A + D = 9
E = A + D
C = E
E - 6 = B
Now what does A + B x C + E equal? Finding it a bit hard to remember? Thats why we have Variables.
What you have just done with your memory is the same as what a computer can do with variables. The same process can be done in C++ like this:
We can define a variable as a portion of memory to store a determined value. Think about it as a small block of memory, you can write on it, erase it, make it bigger or smaller, but all the time just calling it by its identifier.Code:A = 6 B = 9 C = 3 D = 3 E = A + D C = E E - 6 = B
The identifiers in that example were all single letters, A, B, C, D and E. Spaces or punctuation marks or symbols cannot be part of an identifier. Only letters, digits and single underscore characters are valid. Variable identifiers always have to begin with a letter or an underscore.
Something else that you have to consider when inventing your own identifiers is they cannot match any keyword of the C++ language or your compiler's specific ones, which are reserved keywords.
Standard reserved keywords are:
[php]
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, 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, void, volatile, wchar_t, while[/php]
Additionally, alternative representations for some operators cannot be used as identifiers since they are reserved words under some circumstances:
[php]and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq[/php]
Remember C++ is a case sensitive language so Identifier is not the same as identifier or iDentifier.
There are many data types that can store many different things. From integers(whole numbers) to Characters or Strings. Here is a list of some.
[IMG]https://i755.photobucke*****m/albums/xx191/_-_-_JAMES_-_-_/DataTypes.jpg[/IMG]
If we want to use a variable, we must first declare it. You do this by putting a valid data type followed by your identifier. For example:
[php]int cat;
char dog;[/php]
If you are going to declare more than one variable of the same type, you can declare all of them in a single statement by separating their identifiers with commas. For example:
[php]int a, b, c;[/php]
You DON'T need to do
[php]int a;
int b;
int c;[/php]
The integer data types char, short, long and int can be either signed or unsigned depending on the need. Signed types can represent both positive and negative values, unsigned types can only represent positive values (and zero).If we do not specify either signed or unsigned most compiler settings will assume the type to be signed.
An exception to this rule is the char type, which is considered a different data type from signed char and unsigned char. You should use either signed or unsigned if you intend to store numerical values in a char-sized variable.
Short and long can be used alone as type specifiers. Short is equivalent to short int and long is equivalent to long int. The following two variable declarations are equivalent:
[php]
short cat;
short int cat;
[/php]
Signed and unsigned may also be used in the same way.
We will now look at the scope of variables.
There are two types of variables we will look at Global and Local.
Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration.
The scope of local variables is limited to the block enclosed in braces where they are declared. If they are declared at the beginning of the body of a function their scope is between its declaration point and the end of that function. This means that if another function existed in addition to main, the local variables declared in main could not be accessed from the other function and the variables from the other function could not be accessed inside main.
When declaring a regular local variable, its value is automatically undetermined. But you may want a variable to store a value at the same moment that it is declared. In order to do that, you can initialize the variable. There are two ways to do this in C++:
[php]
int a = 6;
OR
int a (6)
[/php]
We are now going to use all this in a program:
The result should look like this:Code:#include <iostream> using namespace std; //you can declare global variables here int rabbit = 1; int main() { //you can declare local variables here int dog = 3; int cat (4); cout << dog << endl; cout << cat << endl; cout << dog + cat << endl; dog = rabbit; cout << "we have now changed dog to rabbit" << endl; cout << dog; return 0; }
[php]
3
4
7
We have now changed dog to rabbit
1
[/php]
Credits:
www.cplusplus.com
www.cprogramming.com
Me
Last edited by P0SEID0N; 04-15-2010 at 02:51 AM.
I do'nt fucking understand anything
understood all, thank you !!!!
I support Thieving Eh for the Publicist competition in CA section Thieving Eh vid is the first post right below Obama and Dave on the first page! https://www.mpgh.net/forum/7-news-ann...st-needed.html
GO Thieving Eh
If it confuses you then try and do some of the practical work in it. eg. put my code in and see what it does. Then read over the theory again.
dont spam, only faggets use smileys.
[img]https://www.steliar*****m/images/star1.gif[/img][img]https://www.steliar*****m/images/star2.gif[/img][img]https://www.steliar*****m/images/star3.gif[/img][img]https://www.steliar*****m/images/star2.gif[/img][img]https://www.steliar*****m/images/star1.gif[/img]
Don't expect any more public hacks from me.
[SA Public]Voltage552 Hook! UNDETECTED - ALL OS
NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO O