How a program works:
To make a program is very simple. The flow of making a program is as follows:
Cpp file -> Compiler (You have your linkers here) -> binary executable (i.e. your program)
Your cpp file can be in plaintext (i.e. using some editor such as Notepad, or on Linux, vim / gedit / Kate / etc.). Your compiler is the intermediary step between human-readable code and binary code. This is a one-way process. (i.e. you cannot turn binary exe into readable code without disassembling it).
Getting a compiler:
For Windows, try Visual C++ which is now free or Bloodshed Dev C++. Follow the instructions to compile a program. In VCC, that usually means making a project first. In Bloodshed Dev C++, you can do that by creating a cpp file, then pressing F9 to compile and F10 to run.
For Linux, your OS should have g++ pre-installed. If not, get g++ by running the command “sudo apt-get install g++” from terminal. You need administrator rights to install it. To compile a program using g++, type in the following:
g++ -o [name of output file] [name of input c++ file]
to run, type in “./[name of output file]”.
These form the basics of a compiler.
Basic C++ structure:
C++ follows C syntax. This includes the demarcation of program flow using curly braces {}. These will be elaborated in more detail later. C++ is a top-down language, i.e. it processes commands from top to bottom, and not all at once within a code block. This is very important to note.
Writing your first C++ Program:
A basic C++ hello world program goes like this:
#include
using namespace std;
int main() {
cout << "Hello world" <<>
return 0;
}
#include
#include is a preprocessor directive. I.e, it tells the compiler what to do before it compiles the program. In this case, a computer will not know how to input / handle certain things UNLESS you tell it how to. In this case, it includes a header file called “iostream” that contains definitions on how to output text to the command line, among other things. For this purpose, we will only need the “iostream” header. As noted, C++ has a top-down program flow. Hence include this statement at the beginning of every C++ file.
using namespace std;
This is like a shortcut. If you do not do this, you will need to put a “std::cout” everytime you use “cout”. So treat this like a general habit everytime you use standard C++. Basically, it tells the compiler to always use the namespace std as a shortcut to std C++.
NOTE THE SEMICOLON “;”. This is to tell the compiler you are moving on to the next line in the program. Just pressing the return key (i.e. enter) will not do the trick. Do this for every line except for function declarations and the like.
int main() { }
The main body function. This is where the compiler will look when you run the program. This is a function, and by right, functions can return something. In this case, an integer (int). Don’t worry, you will learn more about functions in the functions chapter. For now, just write it as is. You just have to know that for C++ programs, traditionally to return a value 0 means the program has no error, and to return any other number means there was some error in processing.
cout << “hello world” <<>
Pronounce cout as C-out. This tells the compiler to print the message (in this case, a string as denoted by “”) “hello world” to the command line. The “endl” means “to add a newline, i.e. move to the next line. What this does is add a “\n” to your output.
return 0;
}
End the main function. Return a 0 since there will not be any error. For windows users, you might want to add another line
system(“pause”);
before the return 0 so that you can see what is generated on the screen. This is not needed for Linux users.
There you have it, your first hello world program in a nutshell. Compile it and it should be ready to run.
Ciao!
No comments:
Post a Comment