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

PDF
PPTX
Common Programming Errors
PPTX
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
PDF
General structure of c++
PPTX
C++ chapter # 1
PPTX
basic program
PPTX
C programming on Ubuntu
PDF
escape sequences and substitution markers
Common Programming Errors
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
General structure of c++
C++ chapter # 1
basic program
C programming on Ubuntu
escape sequences and substitution markers

What's hot (20)

PDF
First c program
PDF
csharp repitition structures
PPT
Fp201 unit2 1
PPTX
c++ programming Unit 2 basic structure of a c++ program
PPTX
My first program in c, hello world !
PPT
Practical basics on c++
PPTX
C++ Chapter 3
PPTX
Intro To C++ - Class 04 - An Introduction To C++ Programming, Part III
PPTX
Python component in mule
PPTX
How c program execute in c program
DOC
Week3 dq4
PPTX
C# 5.0
PDF
A simple program C# program
DOCX
#Include
PPTX
C program to write c program without using main function
PPT
C introduction
DOCX
Examples c#
PPT
Intro to c++
PPT
Unsafe
First c program
csharp repitition structures
Fp201 unit2 1
c++ programming Unit 2 basic structure of a c++ program
My first program in c, hello world !
Practical basics on c++
C++ Chapter 3
Intro To C++ - Class 04 - An Introduction To C++ Programming, Part III
Python component in mule
How c program execute in c program
Week3 dq4
C# 5.0
A simple program C# program
#Include
C program to write c program without using main function
C introduction
Examples c#
Intro to c++
Unsafe
Ad

Viewers also liked (20)

PDF
PDF
Chap 2 c++
PDF
NUMERICAL METHODS
PDF
Chapter 2 basic element of programming
PDF
CP Handout#2
PPT
Chapter 13 - Inheritance and Polymorphism
PPT
8.3 program structure (1 hour)
PPTX
Constructs (Programming Methodology)
PDF
CP Handout#5
DOC
Java programming lab assignments
PPTX
Pf cs102 programming-9 [pointers]
PPT
PPT
Apclass (2)
PPT
Chapter 12 - File Input and Output
PPTX
C++ lecture 04
PPTX
Loop c++
PPTX
C++ programming (Array)
PPT
User defined functions in C programmig
PPTX
Array in c++
PDF
C++ ARRAY WITH EXAMPLES
Chap 2 c++
NUMERICAL METHODS
Chapter 2 basic element of programming
CP Handout#2
Chapter 13 - Inheritance and Polymorphism
8.3 program structure (1 hour)
Constructs (Programming Methodology)
CP Handout#5
Java programming lab assignments
Pf cs102 programming-9 [pointers]
Apclass (2)
Chapter 12 - File Input and Output
C++ lecture 04
Loop c++
C++ programming (Array)
User defined functions in C programmig
Array in c++
C++ ARRAY WITH EXAMPLES
Ad

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

PDF
fundamental of c++ for students of b.tech iii rd year student
PPTX
Intro To C++ - Class 3 - Sample Program
PPTX
lecture1 pf.pptx
PPTX
Basics Of C++.pptx
PPTX
C++ AND CATEGORIES OF SOFTWARE
PDF
A tutorial on C++ Programming
PPTX
Programming Fundamentals IDE's Lec3.pptx
PPTX
Introduction to cpp language and all the required information relating to it
PPTX
Structure of a C++ Program in computer programming .pptx
PDF
Tutorial basic of c ++lesson 1 eng ver
PPTX
computer programming omputer programming
PPTX
Lesson 2 starting output
PPTX
AS TASKS #8
PDF
PROGRAMMING FUNDAMENTALS BASICS LECTURE 2
PPTX
POLITEKNIK MALAYSIA
PPT
intro to programming languge c++ for computer department
PDF
C++ for hackers
PDF
His162013 140529214456-phpapp01
PPTX
Introduction to programming
fundamental of c++ for students of b.tech iii rd year student
Intro To C++ - Class 3 - Sample Program
lecture1 pf.pptx
Basics Of C++.pptx
C++ AND CATEGORIES OF SOFTWARE
A tutorial on C++ Programming
Programming Fundamentals IDE's Lec3.pptx
Introduction to cpp language and all the required information relating to it
Structure of a C++ Program in computer programming .pptx
Tutorial basic of c ++lesson 1 eng ver
computer programming omputer programming
Lesson 2 starting output
AS TASKS #8
PROGRAMMING FUNDAMENTALS BASICS LECTURE 2
POLITEKNIK MALAYSIA
intro to programming languge c++ for computer department
C++ for hackers
His162013 140529214456-phpapp01
Introduction to programming

More from Deepak Singh (20)

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

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
RMMM.pdf make it easy to upload and study
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PDF
Complications of Minimal Access Surgery at WLH
PDF
01-Introduction-to-Information-Management.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Anesthesia in Laparoscopic Surgery in India
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
Weekly quiz Compilation Jan -July 25.pdf
A systematic review of self-coping strategies used by university students to ...
RMMM.pdf make it easy to upload and study
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
GDM (1) (1).pptx small presentation for students
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
O7-L3 Supply Chain Operations - ICLT Program
Cell Structure & Organelles in detailed.
Complications of Minimal Access Surgery at WLH
01-Introduction-to-Information-Management.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

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