lesson_03_c++

3
Every language has its own language processor (or translator), therefore language processor is defined. 1. Compilers 2. Interpreters 3. Assemblers Compiler: the language processor that translates the complete source program as a whole in machine code before execution is called compiler. Examples C or C++ compilers. Interpreter: the language processor that translates (converts) one statement of source program into machine code at a time and executes it immediately before translation the next statement is called interpreter. If there is an error in the statement, the interpreter terminates its translating process at that statement and displays an error message. Example: GW-Basic is an example of interpreter. Assembler: it is used to translate the program wr itten in Assembly language into machine code. Basic Structure of C++ Program: the format or way according to which a computer program is written is called the Structure of the program. The program structure of different programming language is different. The basic structure of C++ program is very simple. The basic structure of the C++ program is given as under: a) Preprocessor Directives b) The main() Function c) C++ statements Example Program: #include <iostream.h> main() { cout << “My First Program is C++”; } Preprocessor Directives: the instructions that are given to the C++ compiler at the beginning of the source code are called preprocessor directives. The preprocessor directives are known as compiler directives. Note: Preprocessor directive does not end with semicolon (;). Most important preprocessor directives are: 1. include directive 2. define directive Detail of include and define directive will be discussed in later lessons. main() Function: C++ consists of one or more functions. Every C++ program must have the main() function. The main function indicates the beginning of the actual C++ program. It is the point at which execution of program is started. C++ Statements: the program statement is the fundamental unit of any programming language. The set of statements of C++ program are written under the main() function (or any user defined function) between curly brackets i.e {}. Every Statement of C++ must be terminated with semicolon (;). It is called statement terminator. If ; is missing, an error message is re ported. (Statement missing)

Upload: muhammad-rehan-baig

Post on 02-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson_03_c++

7/27/2019 Lesson_03_c++

http://slidepdf.com/reader/full/lesson03c 1/3

Every language has its own language processor (or translator), therefore language processor is defined.

1.  Compilers

2.  Interpreters

3.  Assemblers

Compiler: the language processor that translates the complete source program as a whole in machine

code before execution is called compiler. Examples C or C++ compilers.Interpreter: the language processor that translates (converts) one statement of source program into

machine code at a time and executes it immediately before translation the next statement is called

interpreter. If there is an error in the statement, the interpreter terminates its translating process at

that statement and displays an error message. Example: GW-Basic is an example of interpreter.

Assembler: it is used to translate the program written in Assembly language into machine code.

Basic Structure of C++ Program: the format or way according to which a computer program is written is

called the Structure of the program. The program structure of different programming language is

different. The basic structure of C++ program is very simple. The basic structure of the C++ program is

given as under:

a)  Preprocessor Directives

b)  The main() Function

c)  C++ statements

Example Program:

#include <iostream.h>

main()

{

cout << “My First Program is C++”; 

}Preprocessor Directives:  the instructions that are given to the C++ compiler at the beginning of the

source code are called preprocessor directives. The preprocessor directives are known as compiler

directives.

Note: Preprocessor directive does not end with semicolon (;).

Most important preprocessor directives are:

1.  include directive

2.  define directive

Detail of include and define directive will be discussed in later lessons.

main() Function: C++ consists of one or more functions. Every C++ program must have the main()

function. The main function indicates the beginning of the actual C++ program. It is the point at which

execution of program is started.

C++ Statements: the program statement is the fundamental unit of any programming language. The set

of statements of C++ program are written under the main() function (or any user defined function)

between curly brackets i.e {}.

Every Statement of C++ must be terminated with semicolon (;). It is called statement terminator. If ; is

missing, an error message is reported. (Statement missing)

Page 2: Lesson_03_c++

7/27/2019 Lesson_03_c++

http://slidepdf.com/reader/full/lesson03c 2/3

We can write single statement in multiple lines but each statement must have (;) at the end.

We can write multiple statements in single line but each statement end with (;).

Each statement is C++ written in lower case letters because C++ case sensitive language.

There are three types of error that can occur in C++ program. These are:

1.  Syntax errors

2.  Logical errors

3.  Runtime errors

The detail will be discussed later on.

Keywords: the predefined words of the C++ that are used for special purpose in the source program

called keywords. The keywords are also known as reserved words.

For example keyword ‘int’ can’t be used as variable name because it is used to define integer type

variable.

Some keywords are given below

false if for while

int float double char

void new true dodelete sizeof struct class

Data types in C++: A computer program manipulates various types of data. The data is given to the

program as input. The data is processed according to the program instructions and output is returned.

In C++ each variable is associated with a specific data type.

Some basic data types of C++ are:

1.  int

2.  float

3.  double

4.  char

5.  void

Variables: in any programming language, variables are used to store values during program execution.

A variable represents a location in the computer’s memory where a value is stored for use of a program. 

The name of memory location or variable name remains fixed during execution of program but the data

stored in that location may change from time to time.

0000

0001

0002 25 i

0003

0004

0005

0002 25

Memory location

Variable is: i

Value

Page 3: Lesson_03_c++

7/27/2019 Lesson_03_c++

http://slidepdf.com/reader/full/lesson03c 3/3

Rules for Naming the Variables:

1.  First character must be letter or underscore (a to z or _) like number1, total etc.

2.  Special characters, such as arithmetic operators, ^, # ,~ etc.

3.  No blank spaces between the variable name like ( number 1, total marks …….not valid)

4.  Keywords can’t be used as variable name like (float, true, int etc) 

5.  Length of variable name can be from 1 to 31 characters.

6.  A variable name declared for one data type, the same can’t be declared for another data type. 

7.  Meaningful name should be given to variables.