SlideShare a Scribd company logo
www.cppforschool.com
First program is C++
// This is my first program is C++
/* this program will illustrate different components of
a simple program in C++ */
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
When the above program is compiled, linked and executed, the following
output is displayed on the VDU screen.
Hello World!
Various components of this program are discussed below:
Comments
First three lines of the above program are comments and are ignored by the
compiler. Comments are included in a program to make it more readable. If a
comment is short and can be accommodated in a single line, then it is started
with double slash sequence in the first line of the program. However, if there
are multiple lines in a comment, it is enclosed between the two symbols /* and
*/
#include <iostream>
The line in the above program that start with # symbol are called directives and
are instructions to the compiler. The word include with '#' tells the compiler to
include the file iostream into the file of the above program. File iostream is a
header file needed for input/ output requirements of the program. Therefore,
this file has been included at the top of the program.
using namespace std;
All the elements of the standard C++ library are declared within std. This line is
very frequent in C++ programs that use the standard library.
int main ( )
The word main is a function name. The brackets ( ) with main tells that main ( )
is a function. The word int before main ( ) indicates that integer value is being
returned by the function main (). When program is loaded in the memory, the
control is handed over to function main ( ) and it is the first function to be
executed.
Curly bracket and body of the function main ( )
A C++ program starts with function called main(). The body of the function is
enclosed between curly braces. The program statements are written within the
brackets. Each statement must end by a semicolon, without which an error
message in generated.
cout<<"Hello World!";
This statement prints our "Hello World!" message on the screen. cout
understands that anything sent to it via the << operator should be printed on
the screen.
return 0;
This is a new type of statement, called a return statement. When a program
finishes running, it sends a value to the operating system. This particular return
statement returns the value of 0 to the operating system, which means
“everything went okay!”.
Printing Multiple Lines of Text with a Single Statement
/* This program illustrates how to print multiple lines of text
with a single statement */
#include <iostream>
using namespace std;
int main()
{
cout << "WelcomentonC++";
return 0;
}
Output:
Welcome
to
C++
The characters print exactly as they appear between the double quotes.
However, if we type n, the characters n are not printed on the screen. The
backslash () is called an escape character. It indicates that a "special"
character is to be output. When a backslash is encountered in a string of
characters, the next character is combined with the backslash to form an
escape sequence. The escape sequence n means newline. It causes the
cursor to move to the beginning of the next line on the screen.
The following table gives a listing of common escape sequences.
Escape Sequence Description
n Newline
t Horizontal tab
a Bell (beep)
 Backslash
' Single quote
'' Double quote

More Related Content

What's hot (20)

PDF
First c program
Komal Pardeshi
 
PDF
csharp repitition structures
Micheal Ogundero
 
PPT
Fp201 unit2 1
rohassanie
 
PPTX
c++ programming Unit 2 basic structure of a c++ program
AAKASH KUMAR
 
PPTX
My first program in c, hello world !
Rumman Ansari
 
PPT
Practical basics on c++
Marco Izzotti
 
PPTX
C++ Chapter 3
SHRIRANG PINJARKAR
 
PPTX
Intro To C++ - Class 04 - An Introduction To C++ Programming, Part III
Blue Elephant Consulting
 
PPTX
Python component in mule
Ramakrishna kapa
 
PPTX
How c program execute in c program
Rumman Ansari
 
DOC
Week3 dq4
amaxwell2012
 
PPTX
C# 5.0
ali raza
 
PDF
A simple program C# program
Micheal Ogundero
 
DOCX
#Include
Saurabh Chauhan
 
PPTX
C program to write c program without using main function
Rumman Ansari
 
PPT
C introduction
Kamran
 
DOCX
Examples c#
bhaskard8
 
PPT
Intro to c++
Rafael Balderosa
 
PPT
Unsafe
abhay singh
 
First c program
Komal Pardeshi
 
csharp repitition structures
Micheal Ogundero
 
Fp201 unit2 1
rohassanie
 
c++ programming Unit 2 basic structure of a c++ program
AAKASH KUMAR
 
My first program in c, hello world !
Rumman Ansari
 
Practical basics on c++
Marco Izzotti
 
C++ Chapter 3
SHRIRANG PINJARKAR
 
Intro To C++ - Class 04 - An Introduction To C++ Programming, Part III
Blue Elephant Consulting
 
Python component in mule
Ramakrishna kapa
 
How c program execute in c program
Rumman Ansari
 
Week3 dq4
amaxwell2012
 
C# 5.0
ali raza
 
A simple program C# program
Micheal Ogundero
 
#Include
Saurabh Chauhan
 
C program to write c program without using main function
Rumman Ansari
 
C introduction
Kamran
 
Examples c#
bhaskard8
 
Intro to c++
Rafael Balderosa
 
Unsafe
abhay singh
 

Viewers also liked (20)

PDF
Notes
Abhishek Pathak
 
PDF
Chap 2 c++
Widad Jamaluddin
 
PDF
NUMERICAL METHODS
PRABHAHARAN429
 
PDF
Chapter 2 basic element of programming
Zul Aiman
 
PDF
CP Handout#2
trupti1976
 
PPT
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
PPT
8.3 program structure (1 hour)
akmalfahmi
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PDF
CP Handout#5
trupti1976
 
DOC
Java programming lab assignments
rajni kaushal
 
PPTX
Pf cs102 programming-9 [pointers]
Abdullah khawar
 
PPT
Apclass
geishaannealagos
 
PPT
Apclass (2)
geishaannealagos
 
PPT
Chapter 12 - File Input and Output
Eduardo Bergavera
 
PPTX
C++ lecture 04
HNDE Labuduwa Galle
 
PPTX
Loop c++
Mood Mood
 
PPTX
C++ programming (Array)
طارق بالحارث
 
PPT
User defined functions in C programmig
Appili Vamsi Krishna
 
PPTX
Array in c++
Mahesha Mano
 
PDF
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
Chap 2 c++
Widad Jamaluddin
 
NUMERICAL METHODS
PRABHAHARAN429
 
Chapter 2 basic element of programming
Zul Aiman
 
CP Handout#2
trupti1976
 
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
8.3 program structure (1 hour)
akmalfahmi
 
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
CP Handout#5
trupti1976
 
Java programming lab assignments
rajni kaushal
 
Pf cs102 programming-9 [pointers]
Abdullah khawar
 
Apclass (2)
geishaannealagos
 
Chapter 12 - File Input and Output
Eduardo Bergavera
 
C++ lecture 04
HNDE Labuduwa Galle
 
Loop c++
Mood Mood
 
C++ programming (Array)
طارق بالحارث
 
User defined functions in C programmig
Appili Vamsi Krishna
 
Array in c++
Mahesha Mano
 
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
Ad

Similar to Chapter 2 - Structure of C++ Program (20)

PPTX
Intro To C++ - Class 3 - Sample Program
Blue Elephant Consulting
 
PPTX
Basics Of C++.pptx
DineshDhuri4
 
PDF
fundamental of c++ for students of b.tech iii rd year student
Somesh Kumar
 
PDF
C basics programming ppt by Mayanka .pdf
mayankamandal
 
PPT
intro to programming languge c++ for computer department
MemMem25
 
PPTX
Programming Fundamentals lecture 5
REHAN IJAZ
 
PDF
A tutorial on C++ Programming
Prof. Erwin Globio
 
PDF
Tutorial basic of c ++lesson 1 eng ver
Qrembiezs Intruder
 
PPTX
lecture1 pf.pptx
MalikMFalakShairUnkn
 
PPTX
C++ AND CATEGORIES OF SOFTWARE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
PPTX
Introduction to cpp language and all the required information relating to it
PushkarNiroula1
 
PPTX
computer programming omputer programming
Jifarnecho
 
PPTX
Lecture 1
Mohammed Khan
 
DOCX
The basics of c programming
Muhammed Thanveer M
 
PPT
Intro to c programming with all basic concept with clear explanation and example
seccoordpal
 
PDF
The C++ Programming Language
Prof Ansari
 
PPT
C PLUS PLUS FOR BS ELECTRICAL 2ND SEMSTERLecture01.ppt
abdurrahimk182
 
PPT
C Introduction and bascis of high level programming
vipulkondekar
 
PPTX
C Programming Language Step by Step Part 2
Rumman Ansari
 
Intro To C++ - Class 3 - Sample Program
Blue Elephant Consulting
 
Basics Of C++.pptx
DineshDhuri4
 
fundamental of c++ for students of b.tech iii rd year student
Somesh Kumar
 
C basics programming ppt by Mayanka .pdf
mayankamandal
 
intro to programming languge c++ for computer department
MemMem25
 
Programming Fundamentals lecture 5
REHAN IJAZ
 
A tutorial on C++ Programming
Prof. Erwin Globio
 
Tutorial basic of c ++lesson 1 eng ver
Qrembiezs Intruder
 
lecture1 pf.pptx
MalikMFalakShairUnkn
 
C++ AND CATEGORIES OF SOFTWARE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
Introduction to cpp language and all the required information relating to it
PushkarNiroula1
 
computer programming omputer programming
Jifarnecho
 
Lecture 1
Mohammed Khan
 
The basics of c programming
Muhammed Thanveer M
 
Intro to c programming with all basic concept with clear explanation and example
seccoordpal
 
The C++ Programming Language
Prof Ansari
 
C PLUS PLUS FOR BS ELECTRICAL 2ND SEMSTERLecture01.ppt
abdurrahimk182
 
C Introduction and bascis of high level programming
vipulkondekar
 
C Programming Language Step by Step Part 2
Rumman Ansari
 
Ad

More from Deepak Singh (20)

PDF
Computer networks - CBSE New Syllabus (083) Class - XII
Deepak Singh
 
PDF
Chpater29 operation-on-file
Deepak Singh
 
PDF
Chapter28 data-file-handling
Deepak Singh
 
PDF
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 
PDF
Chapter26 inheritance-ii
Deepak Singh
 
PDF
Chapter25 inheritance-i
Deepak Singh
 
PDF
Chapter24 operator-overloading
Deepak Singh
 
PDF
Chapter23 friend-function-friend-class
Deepak Singh
 
PDF
Chapter22 static-class-member-example
Deepak Singh
 
PDF
Chapter21 separate-header-and-implementation-files
Deepak Singh
 
PDF
Chapter20 class-example-program
Deepak Singh
 
PDF
Chapter19 constructor-and-destructor
Deepak Singh
 
PDF
Chapter18 class-and-objects
Deepak Singh
 
PDF
Chapter17 oop
Deepak Singh
 
PDF
Chapter16 pointer
Deepak Singh
 
PDF
Chapter15 structure
Deepak Singh
 
PDF
Chapter13 two-dimensional-array
Deepak Singh
 
PDF
Chapter12 array-single-dimension
Deepak Singh
 
PDF
Chapter 11 Function
Deepak Singh
 
PDF
Chapter 10 Library Function
Deepak Singh
 
Computer networks - CBSE New Syllabus (083) Class - XII
Deepak Singh
 
Chpater29 operation-on-file
Deepak Singh
 
Chapter28 data-file-handling
Deepak Singh
 
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 
Chapter26 inheritance-ii
Deepak Singh
 
Chapter25 inheritance-i
Deepak Singh
 
Chapter24 operator-overloading
Deepak Singh
 
Chapter23 friend-function-friend-class
Deepak Singh
 
Chapter22 static-class-member-example
Deepak Singh
 
Chapter21 separate-header-and-implementation-files
Deepak Singh
 
Chapter20 class-example-program
Deepak Singh
 
Chapter19 constructor-and-destructor
Deepak Singh
 
Chapter18 class-and-objects
Deepak Singh
 
Chapter17 oop
Deepak Singh
 
Chapter16 pointer
Deepak Singh
 
Chapter15 structure
Deepak Singh
 
Chapter13 two-dimensional-array
Deepak Singh
 
Chapter12 array-single-dimension
Deepak Singh
 
Chapter 11 Function
Deepak Singh
 
Chapter 10 Library Function
Deepak Singh
 

Recently uploaded (20)

PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Horarios de distribución de agua en julio
pegazohn1978
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
epi editorial commitee meeting presentation
MIPLM
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 

Chapter 2 - Structure of C++ Program

  • 1. www.cppforschool.com First program is C++ // This is my first program is C++ /* this program will illustrate different components of a simple program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!"; return 0; } When the above program is compiled, linked and executed, the following output is displayed on the VDU screen. Hello World! Various components of this program are discussed below: Comments First three lines of the above program are comments and are ignored by the compiler. Comments are included in a program to make it more readable. If a comment is short and can be accommodated in a single line, then it is started with double slash sequence in the first line of the program. However, if there are multiple lines in a comment, it is enclosed between the two symbols /* and */ #include <iostream> The line in the above program that start with # symbol are called directives and are instructions to the compiler. The word include with '#' tells the compiler to include the file iostream into the file of the above program. File iostream is a header file needed for input/ output requirements of the program. Therefore, this file has been included at the top of the program.
  • 2. using namespace std; All the elements of the standard C++ library are declared within std. This line is very frequent in C++ programs that use the standard library. int main ( ) The word main is a function name. The brackets ( ) with main tells that main ( ) is a function. The word int before main ( ) indicates that integer value is being returned by the function main (). When program is loaded in the memory, the control is handed over to function main ( ) and it is the first function to be executed. Curly bracket and body of the function main ( ) A C++ program starts with function called main(). The body of the function is enclosed between curly braces. The program statements are written within the brackets. Each statement must end by a semicolon, without which an error message in generated. cout<<"Hello World!"; This statement prints our "Hello World!" message on the screen. cout understands that anything sent to it via the << operator should be printed on the screen. return 0; This is a new type of statement, called a return statement. When a program finishes running, it sends a value to the operating system. This particular return statement returns the value of 0 to the operating system, which means “everything went okay!”. Printing Multiple Lines of Text with a Single Statement /* This program illustrates how to print multiple lines of text with a single statement */ #include <iostream> using namespace std; int main() {
  • 3. cout << "WelcomentonC++"; return 0; } Output: Welcome to C++ The characters print exactly as they appear between the double quotes. However, if we type n, the characters n are not printed on the screen. The backslash () is called an escape character. It indicates that a "special" character is to be output. When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence. The escape sequence n means newline. It causes the cursor to move to the beginning of the next line on the screen. The following table gives a listing of common escape sequences. Escape Sequence Description n Newline t Horizontal tab a Bell (beep) Backslash ' Single quote '' Double quote