Posted in C++, Home, TUTORIALS

C++ tutorials: 3 – Introduction to iostream, Hello World!

Here we go with episode 3, this time we’ll start coding something, in particular we’ll analyse most common iostream commands and how to use them. Let’s see!
Open your IDE (how we saw previously, I’m going to use CodeLite), and create a new project.

3proj       3newproj
Let’s start

1. #include

2. using namespace std;

3. int main(){

}

This is the main structure of a c++ program.

  1. #include is a preprocessor that tells to include some libraries, in our case iostream that we’ll use later on.
  2. using namespace std tells to use implicitely the standard library all over the program, avoiding to use std:: before every operation.
  3. int main is the function called at first when your program runs.

Why have I just included the iostream library?

It is one of the most important libraries, it is needed to do basic stream operation just like get input or output text onto the screen.

Let’s build our HelloWorld program!

  1. #include
  2. using namespace std;
  3. int main() {
  4. cout << “Hello World!” << endl;
  5. }

Let’s analyse the 4th line.

cout

  • Meaning: prints some text on your screen.
  • Syntax: cout << "text";

More information

  • You can concatenate more words using << operator, for example: cout << "Hello " << "World !";
  • You can add a break line at the end, in two ways: cout << "Hello " << "World !" << endl; or using special character “\n”: cout << "Hello " << "World ! \n";
  • You can print both text or numbers.

helloworld

Stay tuned for the second part!

MASC

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s