22
C++ Hello World
- This series is going to be dedicated to my reading of the book, C++ programming principles and practice by Bjarne Stroustrup. The resources I used to create this post can be found on ticketnote or HERE and in C++ programming principles and practice by Bjarne Stroustrup.
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
- I am just going to paste the code down below and then we will walk through the code line by line.
//this program outputs the message "Hello, World" to the screen
#include <iostream>
int main()
{
std::cout << "Hello, World!";
return 0;
}
- Now,
//this program outputs the message "Hello, World" to the screen
is considered acomment
. 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 theiostream
library. Theiostream
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. - so with
#include < iostream >
we are instructing the compiler to include the iostream library that will allow us to output data to the screen.
- This marks the start of our function, a function is basically an named sequence of instructions for the computer to execute. A function has 4 parts to it:
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.
- In this case the name of the function is very important. In every C++ program there must be a function called
main
. It will act as a starting point of execution for our project.
- the
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. Withstd::
we are saying that we are going to use the std namespace. -
cout
pronouncedsee-out
is the part of the iostream library that we use to send data to the console for display. - We then use the output operator
<<
to display a string of characters to the console. You can think ofcout
as the actual console and we use<<
to send data to the console. - In C++ a string is determined by double quotes
"Hello, World!"
that is how we know Hello, World! is a string. - Notice that each line ends with a semicolon
;
. Without it our program will not compile.
- lastly we have the return statement which is used to return the type that our function declared in its return type. Note, when we return something, it must match the return type of the function.
- Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
22