Intro To C++/First Program

From Wikibooks, open books for an open world
Jump to: navigation, search
Intro To C++
First Program Variables

Where is compiler?[edit]

C++ programs are initially created as a plain text files and saved with the file extension of "cpp". These can be done in any text editor. In order to execute these c++ program, we needs compiler.

There are several compilers in the website. In most linux system, it is already installed. We do not need to install it. You can check what kind of C++ is installed in the terminal as you type "c++ -v". In the case of Macbook,for a beginners, you would want to install Xcode from the Apple Developer site( and play around with that, which includes both a compiler and an editor. For Windows application,the GNU C++ compiler could be easy under the terms of the General Public license(GPL). Part of GNU is "Minimalist GNU for windows"( You can launch the website, download compilers and install them.

IDE and programming[edit]

IDE is an abbreviation of integrated development environment and let the programming become easier. It is composed of a source code editor, build automation tools and a debugger. For beginners, whether it is needed or not, programmers should decide.In C++, Codeblock(, netbean(, eclipse cdt( are popular as a free IDE.

codeblock netbean eclipse
IDE script
logo Example Example Example

Without IDE, after going to the commandline, through -o option, it is possible to compile and running program. But, IDE helps the programmer design the program robustly and portably and several programmers works together.

Your first program[edit]

In plain editor or IDE, you can copy the following code.

#include <iostream>
using namespace std;

//single line comment


int main() {
    cout << "Hello, enjoy fun!!" << endl;
    return 0;

According to IDE or command line, if you compile and run the program, you can get the following result:

Hello, enjoy fun!

If we analyze the above simple program consists of preprocessor directive, comment, main function and single line statement. In the above program, each part role is like the following table:

Preprocessor directive These are processed by the compiler before the program code must appear. #include instructs the compiler to use the standard C++ library and library name is specified through angled bracket. And "using directive" like using namespace std helps programming easier. Without "using directive", instead of cout and endl, std::cout and std::endl should be written as the iostream library are within std namespace.
Comment These would not be compiled and executed.
Main Function This is the important entry point of C++ program. C++ program should have one named main function. Otherwise, the compiler would not compile the program. In IDE, it could be generated through autotyping.
int main() {
Statement These are actions that the program execute when it runs. Each statement should be terminated by semicolon. In the above program, the statements are as folllows:
    cout << "Hello, enjoy fun!!" << endl;
    return 0;

In main function, return 0; statement is indispensable as return 0; means the program would return a zero integer value to the operating system. In some IDE, when you type int main(), it is generated automatically.

So congratulations on making your first program "Hello,enjoy fun!" application!