You are on page 1of 9

How to write and compile C++ programs

In order to run a program and see it doing wonderful things, you should first write the program. The program can be written in any text editor, such as vi and emacs in Unix environment and using command prompt in DOS. There are also several Integrated Development Environment (IDE) packages available which provide a complete programming environment for C++ in which you can write, compile, run, and debug your program. C++ programs are saved with extensions .C, .cc, .cpp, .cxx depending on the platform you are working upon. Once you have saved a program, next stage is compiling it using a compiler which converts your C++ program into the object code which the computer can understand. Compile using g++ compiler If you are using UNIX environment and using g++ compiler, then compiling your C++ program is simple. After you have saved the program file, simply type the command g++ program.cc –o program where program is the name of the program. For example the name of our first program is first, then save it using extension “first.cc”. To compile, type g++ first.cc –o first To run the program, type first or ./first at the command prompt and your program will run. Compile using DevC++ compiler If you work on Windows and use DevC++, then editing and compiling is as easy as click of a button. DevC++ is an IDE which allows you to edit, compile, run and debug your program in the IDE itself. If you want to install DevC++ then install it from here. 1. Once you have installed and configured the software, Write and save your program using DevC++ itself. Create a new program by clicking on New  Project.

1. Write the code of the program and save it. 1. Choose Empty Project from New Project dialog and choose a name for the program. in our case first and click Ok. .

then the procedure is same as in DevC++.1. However. So to come around this solution. Click on File  New Project. set a breakpoint at the end of main function and then click on Debug instead of running it. After program is compiled. 1. 2. first button) to compile your source code. After you have saved your source code. 3. . click on A Empty Project and click Finish. Compile using VC++ If you use Visual C++ toolkit for C++ programming. then a window at the bottom will specify the warnings. click on Build button to compile the source code. click on Run button (next to compile). Click on Compile button (third row. output window will show the string. DevC++ has a problem. To write the source code. output window opens momentarily and then it closes. Write your source code in the source code window. A dialog box will appear. When you run the program. 1. 2. If there are any errors in your program. As soon as you Run the program.

} Hello World! The first panel (in light blue) shows the source code for our first program. its time to learn about more features of C++ language to get some real action. The way to edit and compile a program depends on the compiler you are using. To the left. return 0. An output window will show the output of the program. compile and run programs using compilers and IDE. Once the source code is compiled. The second one (in light gray) shows the result of the program once compiled and executed. keywords and variables. It is one of the simplest programs that . Next tutorial. Now we have learnt how to edit.these are not part of the program. and its result is the printing on screen of the "Hello World!" sentence. and are shown here merely for informational purposes. here is our first program: 1 2 3 4 5 6 7 8 9 10 // my first program in C++ #include <iostream> using namespace std.3. Structure of a program Probably the best way to start learning a programming language is by writing a program. The previous program is the typical program that programmer apprentices write for the first time. Data Types and Variables tells about data types in C++. Consult the compilers section and the manual or help included with your compiler if you have doubts on how to compile a C++ console program. int main () { cout << "Hello World!". click on Execute button to run the program. Depending on whether it has a Development Interface or not and on its version. Therefore. the grey numbers represent the line numbers .

and in fact it will be included in most of the source codes included in these tutorials. using namespace std. In this case. what differentiates a function declaration from other types of expressions are these parentheses that follow its name. The main function is the point by where all C++ programs start their execution. The programmer can use them to include short explanations or observations within the source code itself. This specific file (iostream) includes the declarations of the basic standard input-output library in C++. The word main is followed in the code by a pair of parentheses (()). cout << "Hello World!". Right after these parentheses we can find the body of the main function enclosed in braces ({}). All the elements of the standard C++ library are declared within what is called a namespace. They are not regular code lines with expressions but indications for the compiler's preprocessor. Optionally. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. int main () This line corresponds to the beginning of the definition of the main function. We are going to look line by line at the code we have just written: // my first program in C++ This is a comment line. the namespace with the name std. #include <iostream> Lines beginning with a hash sign (#) are directives for the preprocessor. .the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. it is essential that all C++ programs have a main function. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. the line is a brief description of what our program is.can be written in C++. these parentheses may enclose a list of parameters within them. For that same reason. This line is very frequent in C++ programs that use the standard library. independently of its location within the source code. So in order to access its functionality we declare with this expression that we will be using these entities. but it already contains the fundamental components that every C++ program has. What is contained within these braces is what the function does when it is executed. That is because it is a function declaration: In C++. It does not matter whether there are other functions with other names defined before or after it . and it is included because its functionality is going to be used later in the program.

and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout. the main function) and. 4 return 0.). cout is the name of the standard output stream in C++. For example. return may be followed by a return code (in our example is followed by the return code with a value of zero). finally lines with statements (like the insertion into cout). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement). Then there were lines that began the declaration of a function (in this case. cout is declared in the iostream standard file within the std namespace. instead of 1 2 int main () { 3 cout << " Hello World!". return 0. In fact. 5} We could have written: . A statement is a simple or compound expression that can actually produce some effect.This line is a C++ statement. There were lines with directives for the compiler's preprocessor (those beginning by #). which were all included within the block delimited by the braces ({}) of the main function. Notice that the statement ends with a semicolon character (. The return statement causes the main function to finish. This is the most usual way to end a C++ console program. which usually corresponds to the screen). we do not have strict rules on how to separate instructions in different lines. You may have noticed that not all the lines of this program perform actions when the code is executed. The program has been structured in different lines in order to be more readable. this statement performs the only action that generates a visible effect in our first program. There were lines containing only comments (those beginning by //). so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code. but in C++. A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution.

int main () { cout << "Hello World! ". so the separation in different code lines does not matter at all for this purpose. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it. We can write many statements per line or write a single statement that takes many code lines. 6 return 0. cout << " I'm a C++ program ". return 0. the separation between statements is specified with an ending semicolon (. since main could have been perfectly valid defined this way: int main () { cout << " Hello World! ". In C++. 4 cout 5 << "I'm a C++ program". the separation in different lines of code has been done just to give greater readability to the program.int main () { cout << "Hello World!". } Hello World! I'm a C++ program In this case. cout << "I'm a C++ program". Let us add an additional instruction to our first program: 1 2 3 4 5 6 7 8 9 10 11 12 // my second program in C++ #include <iostream> using namespace std. return 0. 7} . we performed two insertions into cout in two different statements. return 0.) at the end of each one. } We were also free to divide the code into more lines if we considered it more convenient: 1 int main () 2{ cout << 3 "Hello World!". Once again. } All in just one line and this would have had exactly the same meaning as the previous code.

int main () { cout << "Hello World! ". Preprocessor directives (those that begin by #) are out of this general rule since they are not statements. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code. They simply do nothing. Preprocessor directives must be specified in their own line and do not have to end with a semicolon (. discards everything from where the pair of slash signs (//) is found up to the end of that same line. We are going to add comments to our second program: 1 2 3 4 5 6 7 8 9 10 11 12 /* my second program in C++ with more comments */ #include <iostream> using namespace std.). discards everything between the /* characters and the first appearance of the */ characters. } Hello World! I'm a C++ program . They are lines read and processed by the preprocessor and do not produce any code by themselves. with the possibility of including more than one line.8 And the result would again have been exactly the same as in the previous examples. Comments Comments are parts of the source code disregarded by the compiler. known as line comment. The second one. // prints I'm a C++ program return 0. // prints Hello World! cout << "I'm a C++ program". known as block comment. C++ supports two ways to insert comments: 1 // line comment 2 /* block comment */ The first of them.

cplusplus.com/introduction_compile_run.html http://www. http://cpp-tutorial. most likely causing one or several error messages when you compile it.com/doc/tutorial/program_structure/ . /* or */.If you include comments within the source code of your programs without using the comment characters combinations //.cpp4u. the compiler will take them as if they were C++ expressions.

nf°  °°.

++  f f n°f°¾ €° f¯ °fn¯½° °¾f  .

++½–f¯f¾  J f –°–°  ° f n  f ©¾ °  .

.

217895747.23  @¾¾fn¯¯ °° ° ¾ –°°°–¾f¾¾–°¾%.

.

2 ¾ ½ ½n ¾¾°n  ¾ f¯¾f° f €  @¾¾½ n€n€ %¾ f¯%°n ¾  nff°¾€  f¾n¾f° f °½ ½ f° .:/048970.2 ° ¾ –°°°–ff¾¾–°%%f   n ¾€ ½ ½n ¾¾ @ f ° –f n ° ¾ ½ ¾¾°¾ ° nf°¾€ n¯½  ¾½ ½n ¾¾ °¾nf¾    n 3.%f n°¾  n¯¯ °¾ f°  °f f° €€ n°  f€ ½–f¯ @ ½–f¯¯ nf°¾  ¯ °n ¾ ½f°f°¾ ¾ f°¾° ¾n n ¾ € °¾nf¾  ° ¾f  € ¾n½°€f½–f¯¾  3.:/048970.

089/    ¯ °¾€ ¾f° f ..++ f° ¾°n  nf¾ ¾€°n°f¾–°– ¾ f ° ½–f¯  :833.2085.

++ ff  nf °f¾nf f°f¯ ¾½fn   °f¯ ¾½fn  °f¯ ¾ ° fnn ¾¾¾€°n°f  nf ¾ ½ ¾¾°f  ¾°– ¾  ° ¾ @¾° ¾ €  °°.

3  @¾° n ¾½° ¾  –°°°–€  €°°€ ¯f°€°n° @ ¯f°€°n° ¾ ½°   f.++½–f¯¾f ¾  ¾f° f  f f° °€fn °n °¯¾€ ¾n n ¾°n °  ¾ f¾  392.

++½–f¯¾¾f   n° ° ½ ° °€¾nf°°  ¾n n   ¾°¯f     f  €°n°¾ °f¯ ¾ €°  € f€   °¾n°¾n°f° °¾€°n° ¾ €°°ff¾   €¾° ¾   n °f°.

++½–f¯ f¾f¯  f¾° ¾ ¾¾ °fff.

++ ½–f¯¾f f¯f°€°n°   @  2.3¾€ ° n  f½f€½f ° ¾ ¾% % @f¾ nf¾ ¾f €°n° nff° °.

4:90447/ .++ f €€  °f ¾f€°n° nff°€¯ ½ ¾€ ½ ¾¾°¾f  ¾ ½f ° ¾ ¾f€¾°f¯ ½°f  ¾ ½f ° ¾ ¾¯f °n¾ f¾€½ff¯  ¾° ¯   –f€  ¾ ½f ° ¾ ¾ nf°€°    € ¯f°€°n° °n¾ ° fn ¾%<%  Jf¾n°f° ° ¾  fn ¾¾f €°n°  ¾ °¾  n  .

@¾° ¾f.

++¾f ¯ ° ¾f ¯ °¾f¾¯½ n¯½°  ½ ¾¾°fnf°fnf ½ n ¾¯  €€ n °€fn ¾¾f ¯ °½ €¯¾ °fn°f– ° f ¾f¾   €€ n°€¾½–f¯   .4:9¾ °f¯ € ¾f° f ½¾ f¯°.

++ f°  ¯ f°°–€  °  ¾f ¯ °¾°¾ f¾  °n €nffn ¾%°¾nf¾  0447/¾  °n € nffn ¾%° ¾f° f ½¾ f¯%n n¾fn ¾½° ¾ ¾n °%   n¾ nf ° ¾ f¯¾f° f € ° ¾ °f¯ ¾½fn ¾f ¾  ° °n f¾½ n€n€ f°  nf f   –°–¾ ¾¾½ n€n °f¯ ¾½fn  f °n   -n f ¾f ¯ ° ° ¾f¾ ¯n°nffn %% @¾nffn ¾¾ ¯f   ° € ¾f ¯ °f° °€fn¯¾ °n f  ° €f ½ ¾¾°¾f ¯ °¾ °f.

++½–f¯¾%° € ¯¾n¯¯°¾°f ¾¾° €– °n ¾¯  ¾ ¯n°f€ f¾f ¯ °%  709:73  @  °¾f ¯ °nf¾ ¾ ¯f°€°n°€°¾  °¯f €  f ° n %° f¯½ ¾€    °n ff € %  °n €€  ¯f°€°n°¾– ° f° ½  f¾ ½–f¯ f¾ ½ n f° ¾ °–¾  n° @¾¾ ¯¾¾ff ° f.

++n°¾ ½–f¯   ¯ff °n f°f ° ¾€¾½–f¯½ €¯fn°¾ ° n ¾  n  @    ° ¾n°f°°–°n¯¯ °¾%¾  –°°°– .

.

% @    ° ¾  n ¾€  n¯½  ¾½ ½n ¾¾%¾  –°°°– % @ °    ° ¾f –f°  nff°€ f€°n°%°¾nf¾  ¯f°€°n°%f° €°f° ¾¾f ¯ °¾%  °¾ °°n%  n  f°n °  n ¯    fn ¾%<%€ ¯f°€°n°   @ ½–f¯f¾ °¾n ° €€  °° ¾°  ¯  f f   °.

3     .4:90447/  709:73   <   J n f  °  .++   ° f ¾n ¾°¾ ½ff °¾n°¾° €€  °° ¾  f¯½ °¾ f €  392.

 392.3 .4:90447/709:73 <   °©¾° ° f° ¾ f f  fn ¾f¯ ¯ f°°–f¾ ½ ¾n   °.

++  ¾ ½ff°  °¾f ¯ °¾¾¾½ n€ f° ° °–¾ ¯n°%%f  ° € fn ° ¾ ¾ ½ff°° €€  °n ° ¾  ¾°¯f ff€¾½½¾ J nf° ¯f° ¾f ¯ °¾½ °  f¾°– ¾f ¯ °ff ¾¯f°n ° ¾ @  ¾°€n ° €€  °° ¾¾  ¾°¯f ¯  –  f° ¾n ¯fn€ ¯f°¾f¯f f     ¾f f°f °f°¾n°€¾½–f¯    .

.

.2    :833.280.4:9  0447/  . 5747.4:9 2.3    . 709:73 < 5747.2   J   f¾€    n °¯ ° ¾€ n°¾  ¯ n° ° °   392.2085.43/5747.4:9   2. 5747.2   °¾nf¾  ½ €¯ °¾ °¾°n° €€  °¾f ¯ °¾ °n f–f°   ¾ ½ff°° €€  °° ¾€n f¾ ° ° ©¾– – f  f f  ½–f¯ ¾°n  ¯f°n f  °½ € nf  €° ¾f   392.089/  0447/ 2.4:90447/.2  709:73   < .23     3.:/048970.2 709:73    < 5747.4:90447/   .4:9 2.  392.3     .3 .

   °   ¾ f–f°f  ° fn ¾f¯ f¾° ½ ¾ f¯½ ¾   9 ½n ¾¾  n ¾%¾ f –° %f €¾– ° f ¾°n  f ° ¾f ¯ °¾ @ f ° ¾ f f° ½n ¾¾   ½ ½n ¾¾f°  °½ n f°n    ¯¾  ¾ 9 ½n ¾¾  n ¾¯¾ ¾½ n€ ° °° f°  °f  ° f ¾ ¯n°%%  4220398  .

¯¯ °¾f ½f¾€ ¾n n  ¾ –f   n¯½  @ ¾¯½ °°– @  ½½¾ ¾°f ½–f¯¯ °¾ ° ¾ ¾n½°¾ ¯ ° ¾n  n   .

++¾½½¾f¾°¾ n¯¯ °¾   .

.

30.422039  .

-4..422039 .

   @ €¾€ ¯ °°f¾° n¯¯ °  ¾nf ¾  °–€¯   ½f€¾f¾¾–°¾%.

.

%¾ €° ½  ° €f¾f¯ ° @ ¾ n° ° °°f¾ nn¯¯ °  ¾nf ¾  °–  ° .

nffn ¾f°  €¾f½½ ff°n €  .

nffn ¾  ½¾¾ € °n °–¯ f°° °  J f –°–f n¯¯ °¾¾ n° ½–f¯   .

43/5747.23   92470. 280.4220398 .

    3.089/   392.2  :833.:/048970.4:90447/..3     .2085.

.

4:9 2.5739804  47/ .2. 5747.

.

 5747.     5747.57398 2.2  709:73  < 0447/ 2.2  .

  €°n n¯¯ °¾° ¾n n €½–f¯¾¾°– n¯¯ ° nffn ¾n¯ °f°¾.

.

.

 .

 n¯½ f  ¯f¾€   .

++ ½ ¾¾°¾ ¯¾  nf¾°–° ¾  f ¯ ¾¾f– ¾ °n¯½    ½ $$n½½ f n½½ n¯$° n°%n¯½ %° ¯ ½ $$ n½¾½¾ n¯$ n$f$½–f¯%¾n $ .