Data and Variable Types

April 17, 2006

Hello friends,

Sorry, the posting have been light this week. I was unable to continue my studies fully because of my school. Well, anyway. This post will feature the incredible world of variables and some awesome things one can do with them. Continuing on from the previous post;

Variable Types

Before we learn how to declare variables and do cool stuff with them, we need to know their types and extent. I personally think this is a really important habit a programmer should have – declaring appropriate variables to suit your needs, that is – because although it only makes your program run 0.001 milliseconds faster, it is a really useful treat when you are maintaining your code later on. It is also useful because it helps other people, possible your colleagues, to understand what you are intending to do and what kind of data the program expects, so the possibility of a misunderstanding is diminished. Nobody would want to get a RGB value when the program is actually asking for a hex code.

So onto the types;

Variable Types

Yes, it is taken from one of my resources.

So, when you only want to deal with an integer, you should declare a variable that will hold an integer. An integer variable cannot hold anything else other than an integer. The compiler would probably give some fancy errors.

Declaration of Variables

In order to use variables in our program, we need to declare them. Plus, we need to declare them beforehand. So you cannot say “Hmm, I shall have a variable – but I’ll think of its type later on…” Nope. You must first say what kind of variable you want, and then assign it a name.

int oO;
bool hello;

Or you can declare more than one variable in one line, provided that they are all from the same type.

int a, b, w;

With this line, you have created 3 variables called a, b, w which can store integer type variables. Wow…

We have also seen that these variables can have one more additional feature. Signed, or unsigned. What this really means is that if it is a signed value, it can be either a positive, or a negative value. If it is, on the other hand, unsigned, it can only be a positive value.

When declaring variables, you do not need to tell the compiler whether the variable is signed or not. It will assume that the variable is signed. But still, if you want you can declare it like:

unsigned int a;

for your own convenience. A thing to look out for is that you can only use the signed or unsigned declaration when you are dealing with numbers. Char type would not accept this statement if you are not storing a number value in it.Now, for those of you who do know about programming, the variable scopes are more or less the same in C++. The variables that are declared outside of any apparent function are global variables; they can be accessed from every element within the code. Those variables which are declared inside a function are local variables; they are only valid for the function they are inside in. They cannot be referred from other functions, or from the main program flow. Of course, you could have skipped this, because I plan to touch on this when I have a little more knowledge about functions.

Lastly, we have, as a type of variable, the string. This type does not hold any characters, but it holds any combinations of alphanumeric values. “Hello this text can be the content of a string variable” can be the value for a string variable. It can also contain numbers and all kinds of stuff one can use in a text, but I wish to read further on this topic, because it seems a little different from other languages in C++.

Until next time,

Advertisements

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
• hello.world

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.

Well hello,

I’ve been doing some reading and have managed to learn a bit. Lemme start off:
C++, like any other programming language, has its own syntax. The good thing about C++ is that it is a very shorthand syntax; meaning that you do not have to write AND, NOT, END IF et cetera. That is why it is good for our hands; less movement, it is good for clarity and understandability (huh?); manageable among a group of people, object oriented; which I will touch upon later. But before the pros and cons of the language, lets give some factual information about it.

Firstly, C++ is a “high level” language. No, it doesn’t mean that C++ is the best. It means that it is very abstract. Low level languages (like assembly-machine language) have to be specifically coded for that apparatus, so if you are writing one code for old Intel processor, you will need to alter it before you compile it for the cutting edge processor because there probably will be some differences in the architecture of the processors. On the other hand, C++ is high level, which means it does not concern itself with what kinda processor it works on, moreover it even does not care (well, sometimes) whether it is working in a Windows platform or on a Linux. I have a good comparison to make which I believe is both insightful and understandable. Low level languages are like simple and detailed instructions you may read off from a “Do it yourself” home kit. On the contrary, a high level language will be a fantasy book full of description, and the interpretation is left to the reader. Well maybe not exactly the case, but still, you know what I mean.

I did some research and was in a dilemma about whether to use Microsoft’s Visual C++ or Dev-C++ but being the Microsoft-hater I am and for the sake of my wallet, I chose to go with Dev-C++. It can be downloaded from HERE. I will use this for all the coding I do in C++.

Since we cleared up all that stuff, we can move on. Why? Why C++ when there are tons of other programming languages which promise the same opportunities and even more? Because:

1) C++ is and Object-Oriented language

It allows objects to interact within the program. This gives a little power to avoid any exploits and bugs. More importantly, rather than writing a structured list of instructions as code, you write parts and modules of it. Since semantic markup is very popular these days, in C++ too, you can get the chance to semantically mark up objects, rather than altering functions for the program to function properly (!). Maintaining the code later on is more convenient this way, since you only have to modify the object once.

2) Popularity and Mobility

C++ is very very popular language. People use it for nearly all purposes. It infers the users with so many different techniques that you can do literally anything you want with C++ code. Another aspect of it is that it is very mobile, as I mentioned before. It can work in any system or any platform without the need to alter any code.

3) Short and Fast

The general structure of C++ is shorthand writing. No stupid abbreviations and meaningless markups. It only uses special symbols to communicate with the compiler. Therefore when you look at a foreign code, you can see the main points of the code more clearly. Since it leaves out English words such as “and” “not” etc, your attention is focused on the code itself. This shortness has one more benefit and that is the increased speed. The preprocessor (thing that readies the code for compilation) does not have to cope with words, only special symbols, so it is fast to execute too.

4) Modulation and Compatibility

Backward compatibility is supported in C++, which means that any code written in C can also be included in a C++ program. Also, the program can be composed of different files: one file for functions, others for variables and on and on. You can go and divide the code to smaller pieces, which not only helps for maintenance, but allows a group of developers to work collaboratively, without messing up the code itself for others.

Ok I gotta get some sleep. I have at least learned some cool stuff about C++. Tomorrow (as soon as I finish reading those chapters) I will talk about variables and other stuff I don’t yet know about. Keep coming back for more stuff about C++.

Hello World!

April 11, 2006

Hello world indeed!
Welcome,
I’ve always wanted to learn a programming language and my aim is to be a fairly good programmer that not only writes code, but undertands the fundamentals which lie behind these codes. I’ve always supported more flexible and smart coding.
I started out my quest to being a programming guru with learning Visual Basic. I have learned a lot from the language, and managed to write a mini shoot’em up game where you tried to prevent green aliens from destroying the world. It was a great project for me and apart from learning advanced VB, I also had very useful insight on how a program works and how it should work for convenience.
I continued on with ASP, suddenly leaping to web development. Well, I though ASP was quite a stupid language so I only learned enough so that I would understand what is going on in the code, but could not write the same code if given only the instructions. Just like that phase when you can understand but not speak a new language you are learning. So I passed onto PHP.
I think I have managed to cover quite a lot in PHP, and now I am returning to the core of my dreams: coding a standalone program.
I want to say that I do not know any C++ and will start learning starting from tomorrow. I reckoned that people would be interested in how a newbie in C++ progresses. After each of my practice sessions (can’t guarantee consistency.. :( ) I will write down what I have learned and the importance of that particular knowledge. Maybe you’ll go along and learn with me, or maybe you’ll help me out with your comments when I’m stuck, which has a 99% chance of happening.
Well, I’m really looking forward to do this, so stumble along with me and we’ll see how it turns out.
My main sources when learning the language will be:
http://www.cplusplus.comwww.cprogramming.comC++ All-in-One Desk Reference for Dummies
I have decided it is best to first scan through the language to see its capabilities and go deeper level by level (just like rpg games) to details and theory. I will use the resources in the order I have listed above.
Well, lets see..