c/c++ 01 – Hello World

This is the first tutorial in the c/c++ crash course. This being my first tutorial please bare with me. I’m running on the assumption you already have a an IDE (Integrated Development Environment) set up like Code::Blocks, Visual Studio, Xamarin or go old school with the most basic of text editors and a command line compiler. They all work just fine.

Yup you guessed it the “Hello World” project. Not very exciting but it gets your foot in the door.

Code:

#include <iostream>		// Basic In/Out Header file gives us access to cout and cin

// int is return type and main is the function name () is the non existing parameter list
int main()
{
    std::cout << "Hello World!" << std::endl;	// Hello World
    std::cin.get();				// Wait so we can read the screen
    return 0;
}

Now theres not much there but ill break each line down one at a time.

#include <iostream>

This line includes the c++ standard In/Out stream functionality, like cout and cin. The # tells the compiler that this is a preprocessor directive. Meaning this line is to be don before all other lines (More on these in a later tutorial).

int main()

The main function of our program.  This is going to be the first block of code run on any c or c++ application. The retrun type is set to int (for integer dont worry well get to types in the next tutoria). This retrun value can be used to inform the user of successful ( or not ) termination of out application.

std::cout << "Hello World!" << std::endl;

This line is where all the magic happens in our program. A few things i would like to tuch on here are “std::” and the “<<“.
std is a name space where the standard library holds the functions from <istream>. In order to access them we need to tell the compiler/linker what namespace the functions are located in.
<<  is our first operator. In this case it tells cout to print the message “Hello World!” onto the console window.

std::cin.get();

I added this line only so that the console will stay open on in a window until enter was pressed.

return 0;

The return value telling the the system that the program exited normally.

 

Leave a Reply

Your email address will not be published. Required fields are marked *