Wednesday, March 16, 2011

C++: Hello World!

So, you want to start programming in C++? Well, lemme tell you this: it ain't easy. But with all neat things, it's not going to be. Luckily, if you understand how a computer operates, then any programming language should be easy. Programming is logical: it follows rules and restrictions that, if you do not follow, will either end in a compiling error or a run-time error, depending on the situation. These will be explained later.

In order to write a program, you need a compiler. You don't need an Integrated Development Environment (IDE), but I suggest you download one. An IDE is essentially a program to help you write your programs. It's a good idea to install one because it'll help to auto-complete your code, auto-format, and set up Makefiles to make compiling a breeze. Without an IDE, you have to create the Makefile yourself, which can be hell. To quote someone, "Makefiles are for masochists." I'm going to have to agree with them. Anyway, a good IDE is Code::Blocks. Make sure you download it with the compiler; just read the webpage.

I'm not going to show you how to use the program. I'd rather show you how to write a program than to set up an IDE; check your IDE's website for details on how to set it up. Like I said above, programming isn't easy.

If you've managed to survive this far, then you should do fine. Seriously, I was just scaring you up there. In any case, let's begin:

Let's write a simple program that will write a string to the console. A console is essentially one of those windows with a black background and white text, and you have to type in commands in order for it to work. That's not always true, depending on the device, but 90% of the time, it is.

#include <iostream>

using namespace std;

int main() {
  cout << "Hello World!" << endl;
  return 0;
Okay, before you go off on a tangent, let me explain what's going on here: on the first line, we see we're trying to include something. When we include something, we add it to the program. What we're adding is a file that'll make programming loads easier. "iostream" stands for "input-output stream." "input-output" means that you can ask the user to input data, and then you can output the result of that data back to the user. A stream is like a river: data goes out the same order it goes in. There's a much more complex definition for streams, but one step at a time.

Now, on the second line (technically the third, but let's not overcomplicate it) we see something that has English text, but in a strange format. The only time we use using is to sstate a namespace. A namespace is like an include file (also known as a directive), but it has a different format than a directive, mainly due to them not being files. std does not mean "sexually transmitted diseases," you perverts. Granted, I fell for that trap too, but shut up. "std" stands for "standard."

Following this utter nonsense, we get the main function. Literally, this is the first function to run in a program. A function can be equivalent to a math formula, but a function doesn't always return something. int is the type of variable that will be returned. In this instance, we'll get an integer type back. If you don't remember, an integer is a whole positive of negative number. The brackets are necessary, since they can be used to make the function do multiple statements (or actions). Neat, huh?

cout means "console output." This will output the following data to a console for the user to see. This feature can use any variable. << is called the insertion operator. This will insert the variable into the stream. An operator can be a mathematical symbol like the plus sign or the minus sign, or it can be another kind of symbol that does something else, like the before mentioned. endl means to end the line or to perform a line break. A line break is like pressing the enter key on the keyboard, except the program does it for you on the console. At the end of each statement (remember the multiple statements bit?), you must put a semicolon. This is so the compiler will know when to start on a new statement.

return 0 means exactly as it says: to return 0. When a function returns something, it will automatically stop running and the function that called that function will process the result. Or should, at least. In this case, the function is returning the number 0, which can mean different things. But in this instance, it means, "all went well." Now that the main function has returned a value, what does that mean? It means the function that called the main function will end. But no function called the main function (though I doubt that's possible). So that means the program has ended!

No comments:

Post a Comment