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
Little House in the Big Woods (Little House, #1)
Brighton-based crime drama Grace is returning for series 4
Devin Mansen Obituary
Avonlea Havanese
Room Background For Zepeto
Unitedhealthcare Hwp
Beautiful Scrap Wood Paper Towel Holder
What is IXL and How Does it Work?
A.e.a.o.n.m.s
Newgate Honda
Craiglist Galveston
The Banshees Of Inisherin Showtimes Near Regal Thornton Place
Les Schwab Product Code Lookup
Simplify: r^4+r^3-7r^2-r+6=0 Tiger Algebra Solver
iZurvive DayZ & ARMA Map
Apple Original Films and Skydance Animation’s highly anticipated “Luck” to premiere globally on Apple TV+ on Friday, August 5
A Biomass Pyramid Of An Ecosystem Is Shown.Tertiary ConsumersSecondary ConsumersPrimary ConsumersProducersWhich
Watch Your Lie in April English Sub/Dub online Free on HiAnime.to
Wisconsin Volleyball Team Boobs Uncensored
Cal State Fullerton Titan Online
Ultra Ball Pixelmon
Taylored Services Hardeeville Sc
Albertville Memorial Funeral Home Obituaries
Calvin Coolidge: Life in Brief | Miller Center
2487872771
Gasbuddy Lenoir Nc
P3P Orthrus With Dodge Slash
Lucky Larry's Latina's
No Hard Feelings Showtimes Near Tilton Square Theatre
Muziq Najm
Pawn Shop Open Now
Deshuesadero El Pulpo
Cygenoth
Puretalkusa.com/Amac
Lacy Soto Mechanic
manhattan cars & trucks - by owner - craigslist
Miami Vice turns 40: A look back at the iconic series
Pain Out Maxx Kratom
Coffee County Tag Office Douglas Ga
Online-Reservierungen - Booqable Vermietungssoftware
Pickwick Electric Power Outage
2294141287
Myra's Floral Princeton Wv
Grand Park Baseball Tournaments
Craigslist Charles Town West Virginia
18 Seriously Good Camping Meals (healthy, easy, minimal prep! )
Unpleasant Realities Nyt
Wild Fork Foods Login
Runelite Ground Markers
Ippa 番号
Ark Silica Pearls Gfi
Acellus Grading Scale
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 6505

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.