Structure of A Program
Structure of A Program
Structure of A Program
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
The first panel shows the source code for our first program. The second one shows
the result of the program once compiled and executed.
The way to edit and compile a program depends on the compiler you are using.
Depending on whether it has a Development Interface or not and on its version. It
is one of the simplest programs that can be written in C++, but it already contains
the fundamental components that every C++ program has. We are going to look
line
#include <iostream>
Lines beginning with a hash sign (#) are directives for the
preprocessor. They are not regular code lines with
expressions but indications for the compiler's
preprocessor.
int main ()
return 0;
int main ()
{
cout << " Hello World!";
return0;
}
All in just one line and this would have had exactly the
same meaning as the previous code. In C++, the separation
between statements is specified with an
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++
program";
return0;
}
Comments
Comments are parts of the source code disregarded by the compiler. They
simply do nothing. Their purpose is only to allow the programmer to insert
notes or descriptions embedded within the source code.
// line comment
/* block comment */
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! "; //
prints Hello World!
cout << "I'm a C++ program"; //
prints I'm a C++ program
return0;
}
The first of them, known as line comment, discards everything from where
the pair of slash signs (//) is found up to the end of that same line. The
second one, known as block comment, discards everything between the /*
characters and the first appearance of the */ characters, with the possibility
of including more than one line.
Another rule that you have to consider when inventing your own
identifiers is that they cannot match any keyword of the C++ language
nor your compiler's specific ones, which are reserved keywords. The
standard reserved keywords are:
C++ keywords
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue,
default, delete,
do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for,
friend, goto,
if, inline, int, long, mutable, namespace, new, operator, private, protected,
public, register,
this, throw, true, try, typedef, typeid, typename, union, unsigned, using,
virtual, void,
and, and_eq, bitand, bitor, compl, not, not_eq, or,or_eq, xor, xor_eq
Next you have a summary of the basic fundamental data types in C++,
as well as the range of values that can be represented with each one:
* The values of the columns Size and Range depend on the system the
program is compiled for. The values shown above are those found on
most 32-bit systems. But for other systems, the general specification is
that int has the natural size suggested by the system architecture (one
"word") and the four integer types char, short, Int and long must each
one be at least as large as the one preceding it, with charbeing always
1 byte in size.
Declaration of variables
These are two valid declarations of variables. The first one declares a
variable of type intwith the identifier a. The second one declares a
variable of type floatwith the identifier mynumber. Once declared,
the variables aand my numbercan be used within the rest of their
scope in the program. If you are going to declare more than one
variable of the same type, you can declare all of them in a single
statement by separating their identifiers with commas. For example:
This declares three variables (a, band c), all of them of type int, and
has exactly the same meaning as:
The integer data types char, short, longand intcan be either signed or
unsigned depending on the range of numbers needed to be
represented. Signed types can represent both positive and negative
values, whereas unsigned types can only represent positive values
(and zero). This can be specified by using either the specifier signedor
the specifier unsignedbefore the type name. For example:
By default, if we do not specify either signedor unsignedmost
compiler settings will assume the type to be signed, therefore
with exactly the same meaning (with or without the keyword signed)
An exception to this general rule is the chartype, which exists by itself
and is considered a different fundamental data type from signed
charand unsigned char, thought to store characters. You should use
either signedor unsignedif you intend to store numerical values in a
char-sized variable. shortand longcan be used alone as type
specifiers. In this case, they refer to their respective integer
fundamental types: shortis equivalent to short intand longis
equivalent to long int. The following two variable declarations are
equivalent:
intmain ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;