Tuesday 23 October 2012

Basic components of a C++ Program

1.4 Basic components of a C++ Program and program structure

The C++ is a superset of C. At the basic level, the programs look similar in both C and C++. Every statement in C++ ends with a semicolon (;). All the reserved words have to be written in small case and the c++ compiler is case sensitive. Data in programming languages are stored in variables. To create a variable, the variable should support an inbuilt datatype. The variable is a name given to a particular location in the memory and the value stored in a variable can be altered during program execution. The datatypes supported in C++ are listed below

Table 1.2 Basic Datatypes in c++
Data Type
Size (in bytes)
Values that can be taken
Int
2
-32768 to 32767
Bool
1
False and true / 0 and 1
Char
1
-128 to 127
Long
4
-2,147,483,648 to 2,147,483,647
Float
4
3.4 X 10-38 to 3.4 X 1038 (Precision 7)
Double
8
1.7 X 10-308 to 1.7 X 10308 (Precision 15)
Long double
10
3.4 X 10-4932 to 1.1 X 104932 (Precision 19)
Unsigned int
2
0 to 65,535

Variables can be named according to following rules
· Can comprise of 1 to 8 alphabets, digits or underscore
· First character should be an alphabet
· Names are case sensitive
· Reserve words of c++ cannot be used
Variables have to be declared before using them in the program. The declaration is done in the following way:
datatype variablename
Eg: int data ;
The above declaration declares a integer variable named data. The value stored in int data by default is a junk value. Values can also be assigned to the variable during declarations or initialized separately using the assignment operator =.
Eg: int data=0;

No comments:

Post a Comment