Dusk412
2008-07-09 14:43:03
C++ Variable Tutorial
Description:
This tutorial will teach you all you need to know about C++ variables.
Table of Contents:
I: Introduction
II. Identifiers
III. Types of Variables
IV. Declaring Variables
V. Initializing Variables
I. Introduction:
Variables are⦠very difficult to explain. A variable is a word, its identifier, that stores a value. It can store a different kind of value based on its type. Read on for more.
II. Identifiers:
An identifier is what you call your variable and what you will refer to it as throughout your script. An identifier may include letters, numbers, and underscores ( _ ), but the first character cannot be a number and it is good practice not to begin it with an underscore. It is also good practice not to end in an underscore or have two underscores in a row. An identifier may not include symbols, punctuation marks, or spaces. Identifiers are case sensitive. This means that
Also, beware that your identifiers are not the same as any identifiers or keywords built into c++ or the libraries you are using (learn more about those later). Here is a list of most of the known keywords:
III. Types of Variables:
char A single character, or a single letter, symbol, or number
int An integer, or a whole number value. It can be positive, negative, or zero
short int (short) An integer that is used for smaller values, really rather obscure
long int (long) An integer that is more widely known. It is used to hold large values, generally in the millions or something.
bool A boolean, or a value that is either true or false
float A floating point number. Basically a short decimal number, also not widely used.
double A decimal dumber, very widely used.
long double A longer decimal dumber, holds more places. Again, rather obscure.
wchar_t A wide char, or a character that takes up two or four bytes, instead of one. Very obscure.
You may be wondering where strings, or words, or series of characters are at this point, but these are actually included in a library of their own, something I will discuss farther down in the tutorial.
IV. Declaring Variables:
Declaring a variable is when you let the program know that you have a variable and you tell it what type it is so that you may use it later in your program. You can declare a variable anywhere in the script that you like, something that is missing in some languages, SCAR for example. The only real restriction is that if you declare inside the body of a function, even your main function, it can only be used within that function.
The way to declare a variable is type identifier. For example:
There are two main ways to declare multiple variables of the same type. These are both the same thing.
V. Initializing Variables:
To initialize a variable is to give it a value. There are a few ways to do this.
Method One:
Method Two (slightly obscure and unused):
V. Strings:
The C++ language library provides support for strings through the standard string class. To learn more about libraries, visit another tutorial. You can include it in your program using this line.
Sometimes, it is actually in this library, not really sure why, but this is the fix:
A string is actually an array of characters. We will learn more about arrays later. You declare a string like so:
Technically string does not need to be capitalized, it is just a habit of mine built from several programming languages.
Other C++ Tutorials:
C++ Compiler Tutorial:
http://www.villavu.com/forum/showthread.php?t=32498?p=428885#post428885
C++ Variable Tutorial:
http://www.villavu.com/forum/showthread.php?t=32499?p=428888#post428888
C++ Constant Tutorial:
http://www.villavu.com/forum/showthread.php?t=32500?p=428889#post428889
C++ Operator Tutorial:
http://www.villavu.com/forum/showthread.php?t=32501?p=428891#post428891
Description:
This tutorial will teach you all you need to know about C++ variables.
Table of Contents:
I: Introduction
II. Identifiers
III. Types of Variables
IV. Declaring Variables
V. Initializing Variables
I. Introduction:
Variables are⦠very difficult to explain. A variable is a word, its identifier, that stores a value. It can store a different kind of value based on its type. Read on for more.
II. Identifiers:
An identifier is what you call your variable and what you will refer to it as throughout your script. An identifier may include letters, numbers, and underscores ( _ ), but the first character cannot be a number and it is good practice not to begin it with an underscore. It is also good practice not to end in an underscore or have two underscores in a row. An identifier may not include symbols, punctuation marks, or spaces. Identifiers are case sensitive. This means that
int Numand
int numare different variables.
Also, beware that your identifiers are not the same as any identifiers or keywords built into c++ or the libraries you are using (learn more about those later). Here is a list of most of the known keywords:
and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, class, compl, 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, not, not_eq, operator, or, or_eq, 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, xor, xor_eq
III. Types of Variables:
char A single character, or a single letter, symbol, or number
int An integer, or a whole number value. It can be positive, negative, or zero
short int (short) An integer that is used for smaller values, really rather obscure
long int (long) An integer that is more widely known. It is used to hold large values, generally in the millions or something.
bool A boolean, or a value that is either true or false
float A floating point number. Basically a short decimal number, also not widely used.
double A decimal dumber, very widely used.
long double A longer decimal dumber, holds more places. Again, rather obscure.
wchar_t A wide char, or a character that takes up two or four bytes, instead of one. Very obscure.
You may be wondering where strings, or words, or series of characters are at this point, but these are actually included in a library of their own, something I will discuss farther down in the tutorial.
IV. Declaring Variables:
Declaring a variable is when you let the program know that you have a variable and you tell it what type it is so that you may use it later in your program. You can declare a variable anywhere in the script that you like, something that is missing in some languages, SCAR for example. The only real restriction is that if you declare inside the body of a function, even your main function, it can only be used within that function.
The way to declare a variable is type identifier. For example:
int Number;declares the integer called Number.
There are two main ways to declare multiple variables of the same type. These are both the same thing.
int Number;
int Number2;
int Number, Number2;
V. Initializing Variables:
To initialize a variable is to give it a value. There are a few ways to do this.
Method One:
int Num = 5;
Method Two (slightly obscure and unused):
int Num (5);
V. Strings:
The C++ language library provides support for strings through the standard string class. To learn more about libraries, visit another tutorial. You can include it in your program using this line.
#include <string>
Sometimes, it is actually in this library, not really sure why, but this is the fix:
#include <lvp\string>
A string is actually an array of characters. We will learn more about arrays later. You declare a string like so:
String MyString
Technically string does not need to be capitalized, it is just a habit of mine built from several programming languages.
Other C++ Tutorials:
C++ Compiler Tutorial:
http://www.villavu.com/forum/showthread.php?t=32498?p=428885#post428885
C++ Variable Tutorial:
http://www.villavu.com/forum/showthread.php?t=32499?p=428888#post428888
C++ Constant Tutorial:
http://www.villavu.com/forum/showthread.php?t=32500?p=428889#post428889
C++ Operator Tutorial:
http://www.villavu.com/forum/showthread.php?t=32501?p=428891#post428891