Data and Variables

April 11, 2006

Hello once again,

I’ve read yet another chapter from my resources. It is about Variables, Data Types and Constants. First off, I want to write what variables are what good they do to our program.
Variables are pieces of data stored in the memory of a program. They hold whatever they are supposed to, that is determined by the programmer. They are also a vital part of a program with user interaction. It would not be wrong to say that any written program needs variables. They are the essence of what makes a program. Lets take the straight line equation; y=mx+b. This equation is consisted of nothing but variables. y, m, x and b are all placeholders for actual number values. You can change them, store them and do cool stuff with them. They are variables!
Variables are very much needed for a program but one needs to store many kinds of variables. You wouldn’t want a program functioning only with integers, or decimal numbers, you want variety. The data you input the program is not always the same type of data, so you need different types of data. The basic data types are:

1) char
2) int
3) short int (short)
4) long int (long)
5) bool
6) float
7) double
8) long double
9) wchar_t

Wow, 9 types of data already! I will write what these fancy words mean later on, they are not important right now. Firstly, naming of variables:
Just like the y=mx+b equation, our variables in the program need names for you to refer to them. A nameless variable is useless. You can name you variable anything. Lets say y=john and m=hello, x=mona lisa, b=dj36. So now our equation looks like: john=hello.mona lisa + dj36. So we must name our variables. The name is unimportant, since the data stored in there is not affected by the name. BUT, there is a big but here that if you don’t get used to it, it will make the erroneous code you wrote haunt your dreams. We are learning a new language and this language has its grammar and punctuation.

C++ language has some built in functions and predefined variables. So we have limitations for our variable names. These limitations are very important. They are:

• They must only contain LETTERS, NUMBERS and UNDERSCORES;
• They cannot start with NUMBERS,
• NO punctuation is allowed, meaning commas, dots, anything other than an UNDERSCORE;
• Although it is acceptable to start a variable name with an underscore, it is not recommended for normal variables, they are commonly reserved for external identifiers.

So let’s review the kinds of variable names C++ dislikes:

• 5thAvenue
• @mail
• m@il

And likes:

• hello
• h9og99wogs
• mona_lisa
• _cool

We’ve gone over the typical grammar of the language and were now moving on to exceptions. There are exceptions to everything and C++ no different. Some words needs to be spoken to the compiler so that it can understand what to do next. If, by mistake, you use one of these reserved words for a variable name, you will confuse the compiler and your program will most probably give an error without even compiling. I’ve always been more scared if there are no errors to my code when I first write it, because it means that there may be some unnoticed errors in the program and these can go past unnoticed until a user finds them out. It is not that pleasurable. So, what are these words? I will not do an unordered list because there is a whole bunch of them. Here goes:

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, and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq

One last very, very, VERY important thing is that C++ is case-sensitive. So hello and heLLo are different variables by name. I must keep that in mind since VB and ASP does not have this, although PHP and JavaScript do.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: