1
Chapter 2: Computer Program Basics
2
Chapter 2: Computer Program Basics
Basic Program Structure
Keywords
Variables
Rules for variable names
Constant
Types of constant
Data types
Operators
Statement
Expression
3
Basic Structure of C Program
The structure of C Program is divided into six (6) parts.
In which some parts mandatory and some are optional.
Documentation Section
Link Section
Definition Section
Global Declaration Section
main() function Section
Subprogram Section
4
Document Section
This section is not compulsory section but for good programming
this part should be include.
This is suggested section
Helps anyone to get an overview of the program.
This section consists of comment lines like
The name of the program
The name of the programmer
The author
Date of writing the program
5
Comments
Two different methods to comment in C
Single line comment start from //
While Multi line comment starts with /* and end with */
6
Link Section
This section includes the header files whose functions are to be
used in the program.
This is mandatory section
Header file contain the set of predefined standard library
functions
Stdio.h Input/output functions
Math.h mathematical functions
7
Definition Section
All the symbolic constants are written in the definition section.
Symbolic constants are known as Macros
#define directive is used to define macros.
Example #define PI 3.14
This is an optional section
8
Global Declaration Section
This is an optional section
All global variables are written in this section.
The global variables can be used anywhere in the program.
This section also declares the user defined functions.
Syntax
datatype variable name;
9
main() function
Every program must contain main function from where the
execution of program starts.
This is mandatory section.
From main function other user defined functions are called when
required.
main function have two parts
Declaration part
Executable part
10
Subprogram Section
This section contain user defined functions which are called by
main function when required.
This is an optional section
11
Keywords
The words that are used by the language for special purposes
are called keywords. These are also called reserved words.
The keywords cannot be used as variable names in a program.
For example, in a C++ program the word main is used to
indicate the starting of program.
Other examples of keywords are int, float, if, for, while
12
Variables
A variable represents a storage or memory location in the
computer memory which holds some value.
The name of the variable remains fixed during execution of the
program but the value stored in that location may change from
time to time.
Each variable has name to represent the storage the location.
In C++, a variable name consists of alphabets and digits.
13
Rules for writing variables name
The first character of variable name must be an alphabetic character.
Underscore can be used as first character of a variable name.
Blank spaces are not allowed in a variable name.
Special characters such as arithmetic operators , #, & cannot be
used in a variable name.
Reserved words cannot be used as variable name
The maximum length of a variable name depends upon the compiler
of C++.
A variable name for one data type cannot be used to declare another
data type.
14
Constant
Constant is an identifier whose value can not be change
throughout the execution of program.
Example 12, -12, 12.4, ‘A’, “ABC”
15
Types of Constant
Types of constant include:
Integer Constant
Example 10,45,0.-10, -24
Floating Point Constant
Example 12.5, 16.456
Character Constant
Example ‘A’, ‘V’
String Constant
Example “ABCD”, “A”
16
Data Types
The data type specifies the type of data and set of operations
that can be applied on the data.
Every data type has a range of values
Requires different amount of memory.
Compiler allocates memory space for each variable according to its
data type.
Standard data type: A data type that is predefined in the language is
called standard data type.
Examples int, float, long, double and char
17
Integer Data Type
Integer data is the numeric value with no decimal point or
fraction.
May have positive or negative values
Examples 10, -250, 30
18
Types of integer data type
the larger the size a data type is, the greater the range of values it
can hold.
Short int: The storage capacity of short int type is two bytes. It can
store integer values from -32768 to 32767
Long int: Their storage capacity is four bytes their storage range is
from -2147483648 to 2147483647
Unsigned int: unsigned can only store positive values from 0 to
65535. their storage size two bytes.
19
Float data type
The float type variable store decimal or floating point data.
23.45,16.26, -9.6
Their storage capacity is four bytes and it can store value from 3.4xx
20
Double data type
The double is real or floating type data. Its storage capacity is twice
the capacity of float data type. It is used to store large real values.
The storage capacity for double type variable is 8 bytes and it can
store value from 1.7xx
21
Char data type
The char stands for character. It is used to declare character type
variables.
In character type variables, alphabetic characters, numeric digits
and special characters can be stored.
Example ‘A’, ‘12’, ‘@’
22
Bool data type
The word bool stands for Boolean. It is used to declare logical type
variables.
In a logical type variable, only two values true or false can be stored.
The true is equivalent to 1 and false to 0.
23
Operators
Operators are the symbols that are used to performed operations on
data.
C/C++ provides the following types of operators
Arithmetic operators +, -, /, *, %
>, <, ==, >=,
Relational operators
<=, !=
Logical operators &&, ||, !
Assignment operators =
Increment and decrement operators ++, - -
Compound Assignment operators +=, -=, /=, *=
24
Statement
A computer program statement is an instruction for the program to
perform an action.
There are many different types statements that can be given in a
computer program in order to direct the actions the program
performs.
Each statement in C++ ends with a semicolon (;).
25
Expression
A statement that evaluate to a value is called an expression.
Given a single value
Consists of operator and operand
An operator is a symbol that performs some operations on
operands
Operand is the value on which the operator performs some
operation.
Can be a constant, variable or combination of both.
Examples A+B; m/n; x+100;