33
C++ Hello World
I first want to point out that some syntax in C++ programming principles and practice
is a little our of date but the underlying principles taught in it are tried and true. Also, Bjarne Stroustrup is a world class computer scientist and he invented C++, so there is literally no other person more qualified to write a book about programming in C++.
I also want to point out that if you need to set up your code editor or are unsure how to create a new file, then I would recommend following this tutorial HERE
//this program outputs the message "Hello, World" to the screen
#include <iostream>
int main()
{
std::cout << "Hello, World!";
return 0;
}
//this program outputs the message "Hello, World" to the screen
is considered a comment
. A comment is indicated by two slashes //
, comments are ignored by the compiler and are written for the benefit of the programmers who read the code. The first line of a program is typically a comment that tells the human reader what the program is supposed to do.#include
is how we instruct the compiler to include a library and in our case we are instructing the compiler to include the iostream
library. The iostream
library is part of the standard C++ library that deals with basic input and output. This library is used to get input from the keyboard and the output data to the console.#include < iostream >
we are instructing the compiler to include the iostream library that will allow us to output data to the screen. 1) Return type : specifies what type the function will return. For us we specified a return type of
int
meaning integer and it is the reason we return 0.2) A name : simply the name of the function and for us the name is
main
(more on main later).3) A parameter list : indicated by enclosed parentheses
()
, ours is empty to signify that this function takes no extra values when called. 4) A function body : a function body is everything that is enclosed within curly brackets
{}
and holds all the operations for the the function to perform.main
. It will act as a starting point of execution for our project.std
prefix is a way to indicate that we are using the std namespace. Generally in programming a namespace is used to promote encapsulation and organization within a program. With std::
we are saying that we are going to use the std namespace.cout
pronounced see-out
is the part of the iostream library that we use to send data to the console for display.<<
to display a string of characters to the console. You can think of cout
as the actual console and we use <<
to send data to the console."Hello, World!"
that is how we know Hello, World! is a string.;
. Without it our program will not compile.