244
edits
No edit summary |
No edit summary |
||
| Line 33: | Line 33: | ||
#include <iostream> | #include <iostream> | ||
iostream (or, archaically, iostream.h) is a C++ header file, which is a file containing C++ code designed to be imported by multiple programs. | '''iostream''' (or, archaically, iostream.h) is a C++ header file, which is a file containing C++ code designed to be imported by multiple programs. | ||
Now we want to create our main() function. main() is the first part of code that will be executed upon running your program. | Now we want to create our '''main()''' function. main() is the first part of code that will be executed upon running your program. main() will typically look something like this | ||
int main() | |||
{ | |||
// insert some code here | |||
return(0); | |||
} | |||
There are a couple of things that have to be explained here. | |||
* The function begins with '''int''' - an int in C++ means integer, which is simply a type of variable - in this case a number. Having the function declaration as int main() means that the function main() will return an int at the end of its execution. main() will '''always''' return an int. | |||
* Chain brackets '''{ }''' are used to denote the code which main() contains. When you call main(), the code between these brackets will be executed. | |||
* '''//''' is used to make a comment. A comment is a line in your source code which will be completely ignored by the compiler. This is handy for explaining what your code does and you should quickly learn to use them frivilously. | |||
* '''return(0);''' is placed at the end of your main() function. The 0 is the int which is returned by the function - in this case, 0 means the program executed without any problems. Yay!!! The ; is used to mark the end of a line where a command is issued. If you omit this semicolon, the compiler will whinge at you. | |||
So, at this point your program should look like this | |||
#include <iostream> | |||
int main() | |||
{ | |||
// insert some code here | |||
return(0); | |||
} | |||
The [[Programming On Redbrick]] page explains how to compile C++ programs. Just to recap, we'll go over it again (oh joy!) Save your program in nano by pressing CTRL+O (to bring up the save option) and then CTRL+X to exit. | |||
g++ -o first first.cpp | |||
Your program ''should'' compile. If it does, run it by entering | |||
./first | |||
Nothing will happen. That's because we haven't told the program to do anything! | |||
Open up your source file again and | |||
stands for Gnu C Compiler. It is a fairly popular UNIX C compiler and is based on the standard C Compiler, known as CC :o) | stands for Gnu C Compiler. It is a fairly popular UNIX C compiler and is based on the standard C Compiler, known as CC :o) | ||
edits