< Intro To C++

Where is compiler?

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 c++ programs, we need a 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(http://developer.apple.com/xcode/) 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"(http://www.mingw.org). You can launch the website, download compilers and install them.

IDE and programming

IDE is an abbreviation for Integrated Development Environment and allows for programming in an easier fashion than a standard text-editor. An IDE is usually composed of a source code editor, build automation tools and a debugger. For beginners, they may or may not need all the features of a IDE. It is up to each developer on what they want to use and require. In C++, Code::Blocks (codeblocks.org), NetBeans (netbeans.org), Eclipse CDT (eclipse.org)[note 1] are popular as a free IDE.

codeblocknetbeaneclipse
websitehttp://www.codeblocks.org/http://netbeans.org/https://eclipse.org/cdt/
IDE script
Example
logoExampleExampleExample

Through the command-line you can use the -o flag to compile to run a program without requiring an IDE. However, using an IDE can help a programmer with automatic formatting, easier debugging, highlighting along with many other features. Some development environments also can allow multiple programmers work together more efficiently and effectively.

Your first program

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

#include <iostream>
using namespace std;

//single line comment

/*
multi
line
comment
*/

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

Through an IDE or command line, if you compile and run the program, you should get the following result:

Hello, enjoy fun!

If we analyze the simple program shown above, it consists of preprocessor directive, comment, main function and single line statement. From the above program, each part is explained in detail in the following table:

Preprocessor directiveThese 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.
CommentComments are not compiled or executed in the program as code.
Main FunctionThis is the important entry point of every C++ program. A standard C++ program should have one named main function. Otherwise, the compiler will not compile the program. In an IDE, it could be generated through autotyping.
int main() {
}
StatementThese are actions that the program execute when it runs. Each statement should be terminated by a semicolon. In the above program, the statements are as follows:
    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's, when you type int main(), it is generated automatically.

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

Footnotes and references

  1. The Eclipse CDT Project provides C and C++ Integrated Development Environment based on the Eclipse platform.
    This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.