Home » C++ Tutorial » How to Begin with Your First C++ Hello World Code

How to Begin with Your First C++ Hello World Code

No matter who you are or at what level of proficiency you may have with programming languages, you may have heard about the ubiquitous phenomena of “Hello World”, either as a C++ code example or in tutorials and instructions all across the internet. Every person who ever began learning how to code has a beginning, and most of our beginnings started with “Hello World” in C or most other programming codes for that matter. In this article, we will be learning about how you can start with your very own C++ Hello World code. Let’s begin!

What is C++ Hello World?

Traditionally, it is the very first program that any programmer begins to code with. As mentioned earlier, every person who learns to code, begins by first printing out ‘hello world’. As it is used in almost every single programming language, it has now become a tradition, time-bound, and honored by most coders. Here’s something you may not read in other tutorials; the interesting history of “Hello World”.

The origin of this phrase is the programmer and author, Brian Kernighan, who wrote the “C Programming Language” which has been read by programmers across the globe. The phrase was mentioned in this book’s predecessor, ‘A Tutorial Introduction to the Programming Language B’ as we may already know, the C programming language predates all coding scripts we may know if today. The start of the phrase began as “Hello World” in C and then replicated in all other coding scripts. Back when coding was still relatively primitive, the following is how it would be written in code when the phrase was originated:

main( ) {
extrn a, b, c;
putchar(a); putchar(b); putchar(c); putchar(’!*n’);
} 1 ’hell’;
b ’o, w’;
c ’orld’;

This is where it gets interesting. Kernighan recalls in an interview with Forbes India, “What I do remember is that I had seen a cartoon that showed an egg and a chick and the chick was saying, “Hello, world”.” Well, at least we know the answer to who said it first, the chicken or the egg.

How to use C++ Hello World:

Now that we know what the phrase is and where it came from, we can begin to learn our own Hello World program in C++.

The first step in this process would be to download and install a C++ IDE (Integrated Development Environment) through which you can execute your code. The most basic one that most programmers use is Visual Studio which is present in Windows-based computers. After creating a new project, you can begin coding. 

You will need to type in a few lines of code for every program to function. These lines of code are crucial for the IDE to work and are present in all C++ program codes.

#include <iostream>
Using namespace std;
 
int main() {
  return 0;
}

Essentially, this will function as the main container frame of any C++ code that you may work with. This program in itself will only function as a skeleton, it will do nothing as it is. The developer or programmer will have to enter their main body of code within the curly brackets ‘{ }’ in order for the IDE to read code to work upon. ‘return 0’ is an obligational line of code that will be at the very end of your body of code. It helps the IDE know that your body content has ended successfully.

This is where we get to the “Hello World” phrase. As mentioned earlier, any command entered between the curly brackets ‘{ }’ will be executed.

C++ Hello World Syntax:

#include <iostream>
 
int main() {
  std::cout << "Hello World!\n";
}

It is as simple as that. Here is something you should remember. The aforementioned code can be written in two ways. The reason we added ‘std::’ at the beginning of the main printing function ‘cout’, is because the namespace was not mentioned at the beginning of the code. Below is an example in which the namespace is clarified at the beginning of the program.

#include <iostream>
using namespace std;
 
int main() {
  cout << "Hello World!" << endl;
  return 0;
}

As you can see from the above example, there is no mention of ‘std::’ before the main function ‘cout’ as the namespace is mentioned at the beginning of the code. This also saves you the trouble of adding ‘std::’ before every single function within your program. One more thing that is different in the above code is that the keyword ‘\n’ which signifies the end of a line, can easily be replaced by adding ‘<< endl’ after the value of any function that you want to end a particular line with.

If in case your program does not run with the above-mentioned codes, you may want to add the following phrase ‘system(“pause”)’ after the line that has your main function. As follows:

#include <iostream>
using namespace std;
 
int main() {
  cout << "Hello World!" << endl;
  system("pause");
  return 0;
}

OUTPUT:

Hello World!

This error usually happens because some IDEs will run on an older configuration. Newer versions of IDEs make it unnecessary for the developers or programmers to use ‘system(“pause”)’

The output for both of these sets of code will be the same, which is as follows:

OUTPUT:

Hello World!

Conclusion:

And there you have it! Your very first program. You can easily follow this up with some other text or get as creative as you would like. It goes without saying, you can replace “Hello World” in C++ with any phrase that you would prefer. 

‘cout’ is not the only function that you can use this phrase in. You can use any function to include ‘Hello World’, be it ‘cin’ to command the IDE to read the line of code or even use it in loops; the possibilities are endless. So, now that you have learned how to write your very own code in C++, go ahead and beyond ‘Hello World’ toward more sophisticated programs.

Did you know?
1. For Loop In C++
2. Vectors In C++
3. Difference Between C and C++
4. C++ Swap
5. C++ List
6. C++ Hello World
7. C++ Map
8. Converting String To Int C++
9. Stack C++
10. Queue C++

Pin It on Pinterest