C++ Beginners – Hello World program

C++ Beginners Guide – Hello World, it has to be done, every programming guide everywhere starts with “Hello World” and so does almost every IDE available. An introduction to this guide Is available in C++ Beginners Guide Introduction.

#include <iostream>

int main(int argc, char **argv) {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Breaking down the “hello world” program above.

#include <iostream>

The first line includes the header files for the standard input and output stream objects(io = input/output).

int main(int argc, char **argv) {
    
    return 0;
}

This is the main entry point of the C++ program. The two parameters passed “int argc” and “char **argv” are parameters passed into the program at startup. The first parameter is an integer which stating how many characters were passed into the program. The second parameter is an array of chars which is of length “arcgc”. Without something, to do many C++ compilers will not compile the program while others will give warnings.

std::cout << "Hello, World!" << std::endl;
  • std:: specifies that the following instruction is part of the “std” namespace.
  • cout console output
  • << streaming operators
  • endl end of line character

The std:: is not in all “hello world examples” as part “main”. Some will start with an example that includes a “using” statement.

#include <iostream>
using namespace std;

int main (int argc, char **argv) {
   cout << "Hello World" << endl;
   return 0;
}

cout” is a function. If only one version of cout in use adding the using statement is convenient. With it, it is possible to use “cout” without specifying the namespace inline. However, more than one version from differing namespaces is in use, the using statement may be unhelpful.

Streaming operators will be covered later. At this point think of this as a transport that carries data in the direction the arrows point. Therefore the first “<<” is sending “hello world” to “cout” and the second “<<” is sending the end of line character to “cout

return 0 returns a value of 0 as the exit code. This the standard exit code for a program which completes successfully.

More Variations – C++ Beginners – Hello World

Another commonly found variation of the “hello world” program found among common IDE’s doesn’t include the parameters in “main”.

#include <iostream>

int main(int argc, char **argv) {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Related Articles

A list of articles related to C++ Beginners Guide – Hello World

Related Post

2 thoughts on “C++ Beginners – Hello World program

Leave a Reply

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