Conversions    
 
  Constantes 
 
  Kepler's laws  
 
  Unités  
 
  ask us  
 


Coding &
programming


Installing CodeBlocs compiler

C language


C++ language




© The scientific sentence. 1998-2016 
 

Programming with C/C++ Language
Introduction to the C++ Language
The first C++ program





Introduction to the C++ Language



1. The first C++ program

Let's consider this program:

#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!\n";
}


Let's look at the elements of the program.

The #include is a preprocessor directive that tells the compiler to put code from the header called iostream into our program before actually creating the executable.

By including header files, you gain access to many different functions. For example, the cout function requires iostream.

Following the include is the statement, using namespace std;. This line tells the compiler to use a group of functions that are part of the standard library (std).

By including this line at the top of a file, you allow the program to use functions such as cout.

The semicolon ; is part of the syntax of C++. It tells the compiler that you're at the end of a command. It is used to end most commands in C++.

The next important line is int main(). This line tells the compiler that there is a function named main, and that the function returns an integer, hence the type int.

The "curly braces" ({ and }) signal the beginning and end of functions and other code blocks. You can think of them as meaning BEGIN and END.

In other lamguages print, or echo are the function used to display text. In C++, it's the cout object that is used to display text. It is pronounced "C out". It uses the << symbols, known as insertion operators, to indicate what to output.

cout<< is a function call that take a text as an argument to the function. The quotes tell the compiler that you want to output the literal string as-is.

The \n sequence is actually treated as a single character that stands for a newline. It moves the cursor on your screen to the next line. Again, notice the semicolon: it is added onto the end of the line, such as function calls .

Upon reaching the end of main, the closing brace, our program will return the value of 0 (and integer, hence why we told main to return an int) to the operating system.

This return value is important as it can be used to tell the OS whether our program succeeded or not. A return value of 0 means success and is returned automatically (but only for main, other functions require you to manually return a value).

If we wanted to return something else, such as 1, we would have to do it with a return statement:

#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!\n";
return 1;
}


The final brace closes off the function.

Now, let's try compiling this program and running it. Let's cut and paste the code into a file, save it as a helloWorld.cpp file.

• The code:



• The executable:








  

Google
  Web ScientificSentence
 


chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2009. All rights reserved.