SRL Archive - C/C++ Help and Tutorials

C++ Variable Tutorial


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
int Num
and
int num
are 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

boberman

2008-07-09 18:16:50

Here are some notes.

It is often bad form to declare variables on the same line.

C++ is flexible with variables and it is good form to initialize them when you need them (not at the start of a function)

There are two types of strings, one is an array of characters that has a null character at the end, and the other was mentioned before hand. The difference is one is an object and the other an array. The object is much more flexible and easier to use, but the character array is faster.

There are several ways to call a string here are a few.

char str[] = "Hello, this is a string";
char str[255] = "Hello, this is a string";
char* str = "Hello, this is a string"; <- ok, you probably won't use this one too often. But it is handy to know that arrays are pointers and vica-versa

Dusk412

2008-07-09 21:06:09

thnx

R0b0t1

2008-07-17 17:08:03

You can also do:


register int i, j;

To have the values stored in the computers registers, if possible.

Negaal

2008-07-23 02:48:00

Wow nice. I did learn C++ but I gave up as soon as those pointers crap came in.
I can handle some basic stuff.

How many built in functions are there in C++? Anyone knows? I mean scar has maybe 500 or so… C++ is more flexible..so that number must be big.

Dusk412

2008-07-24 16:47:46

Ummm big, really big. And it has waayyyyyyyyyyyy more includes lol

R0b0t1

2008-07-28 05:30:26

It actually has very few built in to the language itself, but the standard libraries have more or less the same amount as normal language libraries. (Lots)