0% found this document useful (0 votes)
2 views

2- Structure of C++ Programe

Uploaded by

saeedafghan01234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2- Structure of C++ Programe

Uploaded by

saeedafghan01234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Structure of a C++ Program

Instructor: Samiullah Ehsas

1
C++ Program Structure:
Probably the best way to start learning a programming language is by writing
a program. Therefore, here is our first program:

Let us look at a simple code that would print the words Hello World.

#include <iostream>
using namespace std;
// main() is where program execution begins.
int main()
{
cout << "Hello World"; // prints Hello World
return 0; 2
}
Let us look various parts of the above program:

• The C++ language defines several headers, which contain information that is
either necessary or useful to your program. For this program, the header
<iostream> is needed.
• The line using namespace std; tells the compiler to use the std namespace.
Namespaces are a relatively recent addition to C++.
• The next line // main() is where program execution begins. is a single-line
comment available in C++. Single-line comments begin with // and stop at the end
of the line.
• The line int main() is the main function where program execution begins.
• The next line cout << "This is my first C++ program."; causes the message "This is
my first C++ program" to be displayed on the screen.
• The next line return 0; terminates main( )function and causes it to return the 3
value 0 to the calling process.
Compile & Execute C++ Program:
Let's look at how to save the file, compile and run the program. Please follow the steps
given below:
• Open a text editor and add the code as above.
• Save the file as: hello.cpp
• Open a command prompt and go to the directory where you saved the file.
• Type 'g++ hello.cpp ' and press enter to compile your code. If there are no errors in
your code the command prompt will take you to the next line and would generate
a.out executable file.
• Now, type ' a.out' to run your program.
• You will be able to see ' Hello World ' printed on the window.

g++ hello.cpp
a.exe 4
Hello World
Make sure that g++ is in your path and that you are running it in the directory containing file hello.cpp.
Semicolons & Blocks in C++:
In C++, the semicolon is a statement terminator. That is, each individual
statement must be ended with a semicolon. It indicates the end of one
logical entity.
For example, following are three different statements:
x = y;
y = y+1;
add(x, y);
A block is a set of logically connected statements that are surrounded by
opening and closing braces. For example:
{
cout << "Hello World"; // prints Hello World
5
return 0;
}
C++ does not recognize the end of the line as a terminator. For this reason, it
does not matter where on a line you put a statement. For example:
x = y;
y = y+1;
add(x, y);
is the same as
x = y; y = y+1; add(x, y);

6
Whitespace in C++:
A line containing only whitespace, possibly with a comment, is known as a
blank line, and C++ compiler totally ignores it.
Whitespace is the term used in C++ to describe blanks, tabs, newline
characters and comments. Whitespace separates one part of a statement
from another and enables the compiler to identify where one element in a
statement, such as int, ends and the next element begins. Therefore, in the
statement,
int age;
there must be at least one whitespace character (usually a space) between int and age
for the compiler to be able to distinguish them. On the other hand, in the statement
fruit = apples + oranges; // Get the total fruit
no whitespace characters are necessary between fruit and =, or between = and apples, 7
although you are free to include some if you wish for readability purpose.
Comments in C++
Program comments are explanatory statements that you can include in the
C++ code that you write and helps anyone reading it's source code. All
programming languages allow for some form of comments.
C++ supports single-line and multi-line comments. All characters available
inside any comment are ignored by C++ compiler.
C++ comments start with /* and end with */. For example:
/* This is a multi-line-comment */

/* C++ comments can also


* span multiple lines
*/

8
A comment can also start with //, extending to the end of the line. For
example:
// This is single line comment
#include <iostream>
using namespace std;
main()
{
cout << "Hello World"; // prints Hello World
return 0;
}

When the above code is compiled, it will ignore // prints Hello World and final
executable will produce the following result: 9
Hello World
C++ Identifiers:
A C++ identifier is a name used to identify a variable, function, class,
module, or any other user-defined item.

Rules for Constructing Identifiers in C++


• Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore character
• First character must be a letter or underscore
• There can be no embedded blanks
• Keywords cannot be used as identifiers
• Identifiers are case sensitive 10
Here are some examples of acceptable identifier
mohd zara abc move_name a_123 myname50 _temp

Identifiers refer to the names of data types, constants, variables, and functions

11
C++ Keywords:
are words that meaning is already defined to the
language.

The following list shows the reserved words in C++. These reserved words
may not be used as constant or variable or any other identifier names.

12
13
Variable:
an entity that may change its value during program
execution is called variable.

Constant:
an entity that can not be changed during program
execution.

14
Thanks

15

You might also like