0% found this document useful (0 votes)
161 views5 pages

WWW W3schools Com CPP CPP - Syntax Asp

This document provides an explanation of the basic syntax of a "Hello World" program written in C++. It breaks down each line of code: 1) The #include directive and using namespace std statements import functionality from libraries for input/output. 2) The int main() function defines the starting point of the program where code between {} will execute. 3) The cout object prints the "Hello World!" string to the screen using the insertion operator. 4) The return 0 statement ends the main function, completing program execution.

Uploaded by

nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
161 views5 pages

WWW W3schools Com CPP CPP - Syntax Asp

This document provides an explanation of the basic syntax of a "Hello World" program written in C++. It breaks down each line of code: 1) The #include directive and using namespace std statements import functionality from libraries for input/output. 2) The int main() function defines the starting point of the program where code between {} will execute. 3) The cout object prints the "Hello World!" string to the screen using the insertion operator. 4) The return 0 statement ends the main function, completing program execution.

Uploaded by

nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

w3schools.

com LOG IN

  MORE  EXERCISES  

C++ Syntax
Previous Next

C++ Syntax
Let's break up the following code to understand it better:

Example
#include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
return 0;
}

Try it Yourself »

Example explained
Line 1: #include <iostream> is a header file library that lets us work with input and output objects, such as
cout (used in line 5). Header files add functionality to C++ programs.

Line 2: using namespace std means that we can use names for objects and variables from the standard library.

Don't worry if you don't understand how #include <iostream> and using namespace std works. Just think of it
as something that (almost) always appears in your program.

Line 3: A blank line. C++ ignores white space.

Line 4: Another thing that always appear in a C++ program, is int main() . This is called a function. Any code
inside its curly brackets {} will be executed.

Line 5: cout (pronounced "see-out") is an object used together with the insertion operator ( << ) to output/print
text. In our example it will output "Hello World".

Note: Every C++ statement ends with a semicolon ; .


Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }

Remember: The compiler ignores white spaces. However, multiple lines makes the code more readable.

Line 6: return 0 ends the main function.

Line 7: Do not forget to add the closing curly bracket } to actually end the main function.

Omitting Namespace
You might see some C++ programs that runs without the standard namespace library. The using namespace std
line can be omitted and replaced with the std keyword, followed by the :: operator for some objects:

Example
#include <iostream>

int main() {
std::cout << "Hello World!";
return 0;
}

Try it Yourself »

It is up to you if you want to include the standard namespace library or not.

Previous Next

COLOR PICKER

LIKE US


Get your
certification today!
View options

HOW TO
Tabs
Dropdowns
Accordions
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List

Certificates
HTML

CSS

JavaScript

Python

SQL

PHP

And more

REPORT ERROR

FORUM

ABOUT

SHOP
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W 3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W 3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W 3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples

Web Certificates
HTML Certificate
CSS Certificate
JavaScript Certificate
SQL Certificate
Python Certificate
PHP Certificate
Bootstrap Certificate
XML Certificate
jQuery Certificate

Get Certified »

You might also like