The Wayback Machine - https://web.archive.org/web/20240827100835/https://www.geeksforgeeks.org/writing-first-c-program-hello-world-example/
Open In App

Writing First C++ Program – Hello World Example

Last Updated : 26 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

C++ is a widely used Object Oriented Programming language and is relatively easy to understand. The “Hello World” program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn.

The Hello World Program in C++ is the basic program that is used to demonstrate how the coding process works. All you have to do is display the message “Hello World” on the console screen.

To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

C++ Hello World Program

Below is the C++ program to print Hello World.

C++




// C++ program to display "Hello World"
 
// Header file for input output functions
#include <iostream>
using namespace std;
 
// Main() function: where the execution of
// program begins
int main()
{
    // Prints hello world
    cout << "Hello World";
 
    return 0;
}


Output

Hello World

Working of Hello World Program in C++

Let us now understand every line and the terminologies of the above program.

1. // C++ program to display “Hello World”

This line is a comment line. A comment is used to display additional information about the program. A comment does not contain any programming logic.

When a comment is encountered by a compiler, the compiler simply skips that line of code. Any line beginning with ‘//’ without quotes OR in between /*…*/ in C++ is a comment. Click to know More about Comments.

2. #include

This is a preprocessor directive. The #include directive tells the compiler to include the content of a file in the source code.

For example, #include<iostream> tells the compiler to include the standard iostream file which contains declarations of all the standard input/output library functions. Click to Know More on Preprocessors.

3. using namespace std

This is used to import the entity of the std namespace into the current namespace of the program. The statement using namespace std is generally considered a bad practice. When we import a namespace we are essentially pulling all type definitions into the current scope.

The std namespace is huge. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type. For example, std::cout. Click to know More about using namespace std.

4. int main() { }

A function is a group of statements that are designed to perform a specific task. The main() function is the entry point of every C++ program, no matter where the function is located in the program.

The opening braces ‘{‘ indicates the beginning of the main function and the closing braces ‘}’ indicates the ending of the main function. Click to know More about the main() function.

5. cout<<“Hello World”;

std::cout is an instance of the std::ostream class, that is used to display output on the screen. Everything followed by the character << in double quotes ” ” is displayed on the output device. The semi-colon character at the end of the statement is used to indicate that the statement is ending there. Click to know More on Input/Output.

6. return 0

This statement is used to return a value from a function and indicates the finishing of a function. This statement is basically used in functions to return the results of the operations performed by a function.

7. Indentation

As you can see the cout and the return statement have been indented or moved to the right side. This is done to make the code more readable. We must always use indentations and comments to make the code more readable. Must read the FAQ on the style of writing programs.

Important Points

  1. Always include the necessary header files for the smooth execution of functions. For example, <iostream> must be included to use std::cin and std::cout.
  2. The execution of code begins from the main() function.
  3. It is a good practice to use Indentation and comments in programs for easy understanding.
  4. cout is used to print statements and cin is used to take inputs.

 



Previous Article
Next Article

Similar Reads

Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
Java Hello World Program
Java is one of the most popular and widely used programming languages and platforms. Java is fast, reliable, and secure. Java is used in every nook and corner from desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet. In this article, we will learn how to write a simple Java Program. Steps to Implem
5 min read
The "Hello World" To Programming
If you have suddenly got interested to get into programming, maybe because your school finally started teaching a programming language (that too most probably during the last years of school life) and/or you surf online to find the cool stuff that people do with programming or you are fascinated by the knowledge and skill that experts have. Either
4 min read
Print "Hello World" in C/C++ without using any header file
Write a C/C++ program that prints Hello World without including any header file. Conceptually it's seems impractical to write a C/C++ program that print Hello World without using a header file of "stdio.h". Since the declaration of printf() function contains in the "stdio.h" header file. But we can easily achieve this by taking the advantage of C p
2 min read
Print Hello World without semicolon in C/C++
Every statement in C++ must end with a semicolon as per basics. However, unlike other languages, almost all statements in C++ can be treated as expressions. However there are few scenarios when we can write a running program without semicolon. If we place the statement inside an if/switch/while/macro statement with a blank pair of parentheses, we d
2 min read
Print "Hello World" with empty or blank main in C++
Write a program in C++ that prints "Hello World", it has a main function and body of main function is empty. Following are three different solutions. We can create a global variable and assign it return value of printf() that prints "Hello World" C/C++ Code // C++ program to print something with empty main() #include &amp;lt;bits/stdc++.h&amp;gt; i
2 min read
Find the first day of a given year from a base year having first day as Monday
Given two integers Y and B representing two years, the task is to find the day of the week in which 1st January of the year Y lies assuming that 1st January of the year B was a Monday. Examples: Input:Y = 2020B = 1900Output:WednesdayExplanation:01/01/2020 was a Wednesday considering that 01/01/1900 was a Monday Input:Y = 2020B = 1905Output:Thursday
6 min read
Facts and Question related to Style of writing programs in C/C++
Here are some questions related to Style of writing C programs: Question-1: Why i++ executes faster than i + 1 ? Answer-1: The expression i++ requires a single machine instruction such as INR to carry out the increment operation whereas, i + 1 requires more instructions to carry out this operation. Question-2: Is writing if(! strcmp(s1, s2) ) a goo
2 min read
Writing code faster during Competitive Programming in C++
This article focuses on how to implement your solutions and implement them fast while doing competitive programming. Setup Please refer Setting up a C++ Competitive Programming Environment Snippets Snippet is a programming term for a small region of re-usable source Code. A lot of modern text editors like Sublime provide you a functionality to auto
3 min read
Writing OS Independent Code in C/C++
A program that can interact with the operating system irrespective of the OS on which it runs. In simple words, we can say that a program, which can run on a computer without depending on the operating system. How to write a utility program that lists all contents of the directory irrespective of the Operating System?Most of the C/C++ compilers def
4 min read
Writing C/C++ code efficiently in Competitive programming
First of all you need to know about Template, Macros and Vectors before moving on the next phase! Templates are the foundation of generic programming, which involve writing code in a way that is independent of any particular type.A Macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of
6 min read
Program to calculate Variance of first N Natural Numbers
Understanding variance is crucial in statistics as it measures the spread of data points around the mean. In this article, we explore how to calculate the variance of the first N natural numbers, denoted by symbol [Tex]\sigma^2[/Tex]. Calculating Variance of First N Natural NumbersGiven an integer N, the task is to find the variance of the first N
4 min read
vector::crend() &amp; vector::crbegin() with example
These functions return useful iterators to access vector elements in reverse order (from end to beginning) Using vector::crend() It's a public member function that returns a const_reverse_iterator pointing to the element preceding the first element. Return Value A const_reverse_iterator to the reverse end of the sequence. Syntax: const_reverse_iter
2 min read
asin() and atan() functions in C/C++ with Example
In C++, asin() and atan() is a predefined function used for mathematical calculations. math.h is the header file required for various mathematical functions. All the functions available in this library take double as an argument and return double as the result. asin() Method asin() function is used to find the arc sine of a number means give a sin
3 min read
wcstok() function in C++ with example
The wcstok() function is defined in cwchar.h header file. The wcstok() function returns the next token in a null terminated wide string. The pointer delim points to the separator characters i.e. the delimiter. .Syntax: wchar_t* wcstok(wchar_t* str, const wchar_t* delim, wchar_t ** ptr); Parameters: This method takes following parameters: str: It re
2 min read
wcsstr() function in C++ with example
The wcsstr() function is defined in cwchar.h header file. The wcsstr() function finds the first occurrence of a source in a destination string. Syntax: wchar_t* wcsstr(const wchar_t* dest, const wchar_t* src); Parameter: This method accepts the following parameters: src: It represents the pointer to the null terminating wide string to be search for
2 min read
wcsncpy() function in C++ with example
The wcsncpy() function is defined in cwchar.h header file. The wcsncpy() function copies a specified number of wide characters from source to destination. Syntax: wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t n); Parameters: This method accepts the following three parameters: dest: specifies the pointer to the destination array. src: s
2 min read
forward_list::cend() in C++ STL with Example
forward_list::cend() is a function in C++ STL which returns a constant iterator pointing to the past-the-last element of the forward_list. The iterator returned by the function does not point to any element in the container, but to the position followed by the last element of the forward list container. The iterator returned by the function points
2 min read
std::is_destructible in C++ with Example
The std::is_destructible template of C++ STL is present in the &lt;type_traits&gt; header file. The std::is_destructible template of C++ STL is used to check whether the T is destructible or not. A class is called destructible whose destructor is not deleted and potentially accessible in derived classes. It return the boolean value true if T is des
2 min read
std::is_nothrow_move_constructible in C++ with Example
The std::is_nothrow_move_constructible template of C++ STL is present in the &lt;type_traits&gt; header file. The std::is_nothrow_move_constructible template of C++ STL is used to check whether the given type T T is move constructibe or not and this is known for not to throw any exception. It return the boolean value true if T is a move constructib
2 min read
std::is_trivially_default_constructible in C++ with Example
The std::is_trivially_default_constructible template of C++ STL is present in the &lt;type_traits&gt; header file. The std::is_trivially_default_constructible template of C++ STL is used to check whether T is trivially default constructible type or not. It return the boolean value true if T is trivially default constructible type, otherwise return
3 min read
std::numeric_limits&lt;T&gt;::denorm_min() in C++ with Example
The std::numeric_limits&lt;T&gt;::denorm_min() function in C++ STL is present in the &lt;limits&gt; header file. This function is used to find the smallest non-zero denormalized value. Header File: #include&lt;limits&gt; Template Class: static T denorm_min() throw(); static constexpr T denorm_min() noexcept; Syntax: std::numeric_limits&lt;T&gt;::de
1 min read
std::numeric_limits<T>::digits in C++ with Example
The std::numeric_limits::digits function in C++ STL is present in the &lt;limits&gt; header file. The std::numeric_limits::digits function is used to find the number of radix digits that the data type can represent without loss of precision. Header File: #include&lt;limits&gt; Template: static const int digits; static constexpr int digits; Syntax:
1 min read
std::remove_cv in C++ with Example
The std::remove_cv template of C++ STL is present in the &lt;type_traits&gt; header file. The std::remove_cv template of C++ STL is used to get the type T without const and volatile qualification. It return the boolean value true if T is without const and volatile qualified, otherwise return false. Header File: #include&lt;type_traits&gt; Template:
1 min read
std::underlying_type in C++ with Example
The std::underlying_type template of C++ STL is present in the &lt;type_traits&gt; header file. The std::underlying_type template of C++ STL is used to get the underlying type of enum type T. Header File: #include&lt;type_traits&gt; Template Class: template &lt;class T&gt; struct underlying_type; Syntax: std::underlying_type&lt;class T&gt;::value P
1 min read
Sine Rule with Derivation, Example and Implementation
Given angles(in degrees) A, C, and the side c, corresponding to the figure below, the task is to find the remaining two sides a and b. Examples: Input: A = 45, C = 35, c = 23Output:28.35 39.49Explanation: a is 28.35 and b is 39.49 Input: A = 45, C = 45, c = 10Output:1014.14 Approach: The idea is to use Sine rule. It states that the sides of any tri
5 min read
BigInt (BIG INTEGERS) in C++ with Example
In C/C++ the number of digits a long long int can have is a maximum of 20. And the question is to store the 22 digit number which is not easy to store in any kind of primitive type. So to deal with this type of problem let's design a new data type which is going to be called BigInt In this article, a few basic operations are being implemented on th
15+ min read
Function Interposition in C with an example of user defined malloc()
Function interposition is the concept of replacing calls to functions in dynamic libraries with calls to user-defined wrappers. What are applications? We can count number of calls to function. Store caller's information and arguments passed to function to track usage. Detect memory leak, we can override malloc() and keep track of allocated spaces.
3 min read
Buffer Overflow Attack with Example
A buffer is a temporary area for data storage. When more data (than was originally allocated to be stored) gets placed by a program or system process, the extra data overflows. It causes some of that data to leak out into other buffers, which can corrupt or overwrite whatever data they were holding.In a buffer-overflow attack, the extra data someti
3 min read
fseek() in C/C++ with example
fseek() is used to move the file pointer associated with a given file to a specific position. Syntax of fseek() The fseek() syntax is: int fseek(FILE *pointer, long int offset, int position);Parameterspointer: It is the pointer to a FILE object that identifies the stream.offset: It is the number of bytes to offset from the positionposition: It is t
2 min read