Writing First C++ Program – Hello World Example

By | May 8, 2023

C++ is Object-Oriented Programming (OOP) language. C++ is easy to understand. You can learn C++ by following these steps:

  1. Writing your program in a text editor. Then saving it with the appropriate file extension (e.g., .cpp, .c, .cp).
  2. Compiling your program using a compiler or an online Integrated Development Environment (IDE).
  3. Understanding of the basic terminologies.

Writing First C++ Program – Hello World Example

The first step of any programming language is printing “Hello World” program. This is basic building block and is one of the simplest programs you will encounter. Lets see following program:

// C++ program to print "Hello World"

// Header file for input output functions
#include <iostream>
using namespace std;

// Driver function
int main()
{
	// prints hello world
	cout << "Hello World";

	return 0;
}

Output:

Hello World 

Time Complexity: O(1)
Space Complexity: O(1)

Analysis of “Hello World” Program

Let’s now examine each line and the terminologies used in the program above:

  1. Comment line

    The comments providis additional information about the program. Comments do not contain any programming logic and are ignored by the compiler. In C++, comments can be denoted by starting a line with ‘//’ or by enclosing text between /* and */.

  2. #include

    In C++, lines beginning with the pound (#) sign are directives processed by the preprocessor, a program invoked by the compiler. The #include directive instructs the compiler to include a file, such as #include. In this specific case, it includes the standard iostream file, which contains declarations for standard input/output library functions.

  3. using namespace std

    This statement imports the entities of the std namespace into the current namespace of the program. However, it is generally considered a bad practice since it brings in all type definitions from the std namespace, which can be quite extensive. An alternative approach is to specify the namespace for each identifier using the scope operator (::) whenever declaring a type.

  4. int main()

    It declares a function named “main” that returns an integer data type. A function is group of statements designed to perform a specific task. In C++, the execution of every program begins with the main() function, regardless of its location within the program. Therefore, every C++ program must have a main() function.

  5. { and }

    The opening brace ‘{‘ signifies the start of the main function, while the closing brace ‘}’ indicates its end. Everything between these braces constitutes the body of the main function.

  6. std::cout<<"Hello World";

    This line ask compiler to display the message “Hello World” on the screen. It is considered statement in C++. Each statement is designed to perform specific task, and a semicolon ‘;’ is used to terminate it. The std::cout is used to identify the standard character output device. Anything following the “<<" characters is displayed on the output device.

  7. return 0;

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

  8. Indentation

    cout and return statements are indented to the right. This practice is employed to enhance code readability. It makes complex program easier to read. Proper indentation and comments should always be used to improve code readability.

Note

Important Points to Note while Writing a C++ Program:

  • Always include the necessary header files:
    It is important to include the appropriate header files. For example, including the header file is necessary to utilize std::cin and std::cout for input and output operations.

  • Execution starts from the main() function:
    The main() function serves as the entry point for program execution in C++. It is where the program begins its execution, regardless of its location within the code.

  • Use indentation and comments for clarity:
    Indentation helps in visually organizing the code, making it easier to read and understand. Comments provide additional explanations and make the code more comprehensible to other programmers.

  • We use cout for printing statements and cin for taking inputs.
Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.