Curriculum
Course: Complete C++ Programming Course
Login

Curriculum

Complete C++ Programming Course

Video lesson

Writing a First HelloWorld Program in C++

In this lesson, you will learn.

  • First Hello World Program in C++
  • Examples

 

Program

  • C++ is a compiled language.
  • For a program to run, its source text must be processed by a compiler, producing object files, which are then combined by a linker to yield an executable program.
  • A C++ program typically consists of many source code files (usually called source files).

 

Hello World Program in C++

Here’s the first program in C++: “Printing a line of text,” followed by a detailed explanation of its components.

Example:  Hello. cpp

#include<iostream>   
// This is a using directive that allows us to use names from the std namespace
using namespace std; 

// This is a multi-line comment:
/*
   This program prints "Hello, World!" to the console.
   It demonstrates the basic structure and syntax of a C++ program.
*/

int main() {  // The main function: Where the execution starts.
    cout << "Hello, World!" << endl; 
      
    return 0;  
}

Now, let’s break down each element of this program:

 

1. Comments:

  • Comments in C++ start with // or /* ..... */.
  • The compiler ignores them; they are used to annotate the code and do not generate any machine language object code.

The comments in C++ can be of the following two types.

Comments in C++

2. #include<iostream>

  • This line is a preprocessor directive that begins with a ‘#’. It tells the compiler to include the iostream standard library in the program.
  • The <iostream> is one of the modern-style headers defined by the Standard C++ library. Modern C++ headers do not use the .h extension.
  • iostream is a header file and contains definitions for input/output (I/O) stream objects like cin (for input stream), cout (for output stream), cerr (for output stream of errors), and clog (for the output stream of logs).

iostram objects

Note:

  1. >>: Stream extraction operator used for input.
  2. <<: Stream insertion operator used for output.
  3. The predefined object ‘cin’ is an ‘istream’ class object.
  4. The predefined object ‘cout’ is an ‘ostream’ class object and is said to be “connected to” the standard output device.
  5. The predefined objects ‘cerr’ and ‘clog’ are also objects of the ‘ostream’ class.

 

3. The using Directive

  • A C++ program can be divided into different namespaces.
  • A namespace creates a declarative region in which various program elements, such as variables, functions, and classes, can be placed.
  • Namespaces help organize large programs.

The directive

using namespace std;

The using statement informs the compiler that you want to use the std namespace. This is the namespace in which the entire Standard C++ library is declared.

Various program components, such as cout, are declared within this namespace. If we didn’t use the ‘using directive’, we would need to add the std name to many program elements.

For example, in the FIRST program, we’d need to say

std::cout << "Hello, World!" << endl;

Note: To avoid adding std:: dozens of times in programs, we use the using directive instead.

 

4. int main()

  • This line defines the main function, which is the entry point for every C++ program.
  • The int before main indicates that this function returns an integer. In the context of the main function, returning zero typically signifies that the program executed successfully.

 

5. Opening and Closing Braces { }:

  • These define the body of the main function. Everything between the opening ({) and closing (}) braces are part of the main function.

 

6. cout << “Hello, World!” <<endl;

  • cout is the standard character output stream in C++. It is used to write data to the console.
  • The << operator is the stream insertion operator and is used to send data to the cout object.
  • "Hello, World!" is a string literal that gets sent to the standard output (typically the console).
  • endl is a manipulator that inserts a new line character and flushes the stream. This results in moving the cursor to the next line on the console.

cout output

Important Note:

If you do not use the following directive

using namespace std;

Then the std:: before cout is required. For example, std::cout<<“Hello, World”;

cout description

 

Additional Notes:

The std namespace is defined in the <iostream> header file. The following code shows the definition of the std namespace in <iostream>.

std namespace

7. return 0;

  • This line ends the main function and returns the value 0.
  • The return value indicates whether the program ran successfully. Returning zero usually means that the program finished without errors.
  •  

 


End of the lesson….enjoy learning.

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 1 review)
Excellent
Very good
Average
Poor
Terrible

 

 

25 September 2024

Good

 

 

Submit a Review