- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Hello World
The "Hello, World!" program is a simple program used to introduce basic syntax in most programming languages. Here's how to write and run a basic "Hello, World!" program in C++.
C++ Hello World Code Example
cpp#include // Includes the input-output stream library
int main() {
std::cout << "Hello, World!" << std::endl; // Outputs "Hello, World!" to the console
return 0; // Returns 0 to indicate the program ended successfully
}
Explanation of the Code:
#include:- This is a preprocessor directive that tells the compiler to include the iostream library, which provides functionality for input and output, such as printing to the screen.
int main():- The
mainfunction is the entry point of every C++ program. Execution starts from the main function. intindicates the return type of the function, meaning it will return an integer value (typically 0 if the program executes successfully).
- The
std::cout:std::coutis the standard output stream in C++, which is used to display information on the screen.<<is the insertion operator, used to send output to the stream (in this case, to the console).
"Hello, World!":- This is a string literal that will be printed to the console.
std::endl:- This is a manipulator that ends the current line and flushes the output buffer. It moves the cursor to the next line after printing.
return 0;:- This statement indicates that the program has ended successfully by returning 0 to the operating system.
Steps to Run the Program
1. Using a Command Line and GCC (Linux/macOS/Windows with MinGW)
Write the code: Create a new file
hello.cppand paste the code above.Compile the code:
- Open Terminal (or Command Prompt on Windows if you have GCC/MinGW installed).
- Navigate to the folder where your
hello.cppfile is saved. - Run the following command to compile the program: bash
g++ hello.cpp -o hello
Run the program:
- After compiling, run the executable: bash
./hello # On Linux/macOS hello.exe # On Windows
- After compiling, run the executable:
Output:
- You should see:
Hello, World!
- You should see:
2. Using an IDE (e.g., Visual Studio or Code::Blocks)
Create a new project:
- Open Visual Studio or Code::Blocks and create a new C++ project.
Add a new file:
- Add a new source file (typically named
main.cpporhello.cpp).
- Add a new source file (typically named
Write the code:
- Copy and paste the C++ "Hello, World!" program into the source file.
Build and run the project:
- Click the build and run button in the IDE, or press the appropriate shortcut (e.g.,
F5in Visual Studio).
- Click the build and run button in the IDE, or press the appropriate shortcut (e.g.,
Output:
- The output window should display:
Hello, World!
- The output window should display:
Summary
The "Hello, World!" program in C++ is the simplest way to introduce basic concepts such as including libraries,
using std::cout for output, and understanding the structure of a C++ program. Once compiled and
executed, the program will output the text "Hello, World!" to the console.