Writing First C++ Program - Hello World Example - GeeksforGeeks (2024)

Last Updated : 26 Dec, 2023

Comments

Improve

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 filewhich 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 theoutput 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.

Elevate your coding journey with a Premium subscription. Benefit from ad-free learning, unlimited article summaries, an AI bot, access to 35+ courses, and more-available only with GeeksforGeeks Premium! Explore now!


`; tags.map((tag)=>{ let tag_url = `videos/${getTermType(tag['term_id__term_type'])}/${tag['term_id__slug']}/`; tagContent+=``+ tag['term_id__term_name'] +``; }); tagContent+=`

`; return tagContent; } //function to create related videos cards function articlePagevideoCard(poster_src="", title="", description="", video_link, index, tags=[], duration=0){ let card = `

${secondsToHms(duration)}

${title}
${showLessRelatedVideoDes(htmlToText(description))} ... Read More

${getTagsString(tags)}

`; return card; } //function to set related videos content function getvideosContent(limit=3){ videos_content = ""; var total_videos = Math.min(videos.length, limit); for(let i=0;i

Writing First C++ Program - Hello World Example - GeeksforGeeks (2024)
Top Articles
26 Best Carry-On Essentials for Travel
30+ Healthy Dessert Recipes (Low-Sugar | 21 Day Fix | Weight Watchers) - The Foodie and The Fix
Foxy Roxxie Coomer
Canary im Test: Ein All-in-One Überwachungssystem? - HouseControllers
Faint Citrine Lost Ark
Wmu Course Offerings
Women's Beauty Parlour Near Me
Teamexpress Login
2013 Chevy Cruze Coolant Hose Diagram
Catsweb Tx State
Pollen Count Central Islip
Edible Arrangements Keller
Slope Unblocked Minecraft Game
Conscious Cloud Dispensary Photos
Cpt 90677 Reimbursem*nt 2023
Copart Atlanta South Ga
Stardew Expanded Wiki
Royal Cuts Kentlands
Ppm Claims Amynta
Violent Night Showtimes Near Century 14 Vallejo
Tips and Walkthrough: Candy Crush Level 9795
Valic Eremit
Low Tide In Twilight Ch 52
Hefkervelt Blog
Victory for Belron® company Carglass® Germany and ATU as European Court of Justice defends a fair and level playing field in the automotive aftermarket
Roanoke Skipthegames Com
Gen 50 Kjv
Uncovering the Enigmatic Trish Stratus: From Net Worth to Personal Life
Www.1Tamilmv.con
Kltv Com Big Red Box
Adecco Check Stubs
Tamilrockers Movies 2023 Download
RUB MASSAGE AUSTIN
Rocketpult Infinite Fuel
Panchitos Harlingen Tx
4083519708
Bay Focus
World History Kazwire
Ktbs Payroll Login
My Locker Ausd
Danielle Ranslow Obituary
Craigs List Hartford
Mitchell Kronish Obituary
Best Haircut Shop Near Me
War Room Pandemic Rumble
Zipformsonline Plus Login
Aloha Kitchen Florence Menu
Take Me To The Closest Ups
How to Find Mugshots: 11 Steps (with Pictures) - wikiHow
Mkvcinemas Movies Free Download
Where To Find Mega Ring In Pokemon Radical Red
Guidance | GreenStar™ 3 2630 Display
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 6078

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.