Sie sind auf Seite 1von 25

Program

A computer program (also software, or just a program) is a


sequence of instructions written in a sequence to perform a
specified task with a computer.

Creating a C++ Program


source code

created using an IDE

Object code

Executable code: exe file

An Introduction to Programming
with C++, Fifth Edition

Processing a C++ Program


(cont'd.)

C++ Programming: From Problem Analysis to


Program Design, Fifth Edition

The Problem AnalysisCoding


Execution Cycle (contd.)

C++ Programming: From Problem Analysis to


Program Design, Fifth Edition

Structure of C++ Program


Contains built-in-functions. These are also
called pre-defined or already developed
functions

Header File

To include header
files in our program

Preprocessor
Directives

Angle or pointed
brackets
h stands for header file

1
Return type of main
function. It is used
to determines
whether the
program is
executed
successfully or not.
Void means nothing
Body
Begin

#include<iostream.h>
#include<conio.h>
void main( )

Function
Name
main function

Starting point of
program execution

Parenthesis shows function. It is used to pass


parameters/arguments

cout<<Welcome to Computer Scientists;


getch();

Program
Body
Body End

File name(standard input


output )

Cout stands for console output.. It uses insertion


operator (<<) print the output on the monitor/console.

Get character
function. It get a
character at runtime.
But we use it to stay
screen to see output

String or message to be
displayed on monitor. It must be
enclosed in double quotes( )

Each statement must end


with semicolon. A
statement without
semicolon generates
syntax error.
5

Standard input and output


streams
cin
Key Board

>>

Standard input stream

cout
<<
Monitor
<<
cerr
Standard output stream

Memory

Simple Steps to write and run a program in C++.

1.

2.

3.

Go to this path C:\TC\BIN then double click on


short to open C++
language.
Now save your program by choosing a meaningful
program file name. e.g. welcome.cpp is a file
name for a program that will display welcome
message at runtime.
Now write your program as explained in the
previous slide. (translation of algorithm to a c++
program).
7

4. Press Alt+F9 key to compile program. If your program is error


free then following screen will appear:

5. Now press Ctrl+F9 key to run the program. Following screen


shows the output of the program:

Now press enter to go back to the source code of the program

Another Sample Program


Program:
#include<stdio h>
void main()
{
int number;
cout<<Enter an integer: \n);
//message for user
cin>>number;
//get input
cout<<The number you entered is: <<number;
getch();
}
Output:
Enter an integer:
25
The number you entered is: 25

10

Programming Style
C++ is a free-format language, which means that:
Extra blanks (spaces) or tabs before or after identifiers/operators are ignored.
Blank lines are ignored by the compiler just like comments.
Code can be indented in any way.
There can be more than one statement on a single line.
A single statement can continue over several lines.
In order to improve the readability of your program, use the following conventions:
Start the program with a header that tells what the program does.
Use meaningful variable names.
Document each variable declaration with a comment telling what the variable is
used for.
Place each executable statement on a single line.
A segment of code is a sequence of executable statements that belong
together.
Use blank lines to separate different segments of code.
Document each segment of code with a comment telling what the segment
does.
11

Variable: Location in memory where value can be stored


An identifier is a name for a variable, constant,
function, etc.
Rules to Declare an
Identifier (variable)

Alphabets from A to Z or a to z
The digits from 0 to 9
Underscore(_) can be used
The first character of an identifier can not be a digit
The name of an identifier can not be a reserve word
No space allowed in the name of identifier
Valid Name:
A
Student_Name
_Fname
Pi

Inalid Name:
$Sum //special ch.
6StName // 1st letter digit
F name // no space allowed
int // reserve word

12

Identifier (variable)
Declaration
Syntax
Data-Type Space Variable-Name(Indentifier);
e.g.
int frstNumber;
char choice;
float divide;
long output;

Identifier (variable)
Initialization
Syntax
Data-Type Space Variable-Name(Indentifier) = Value;
e.g.
int frstNumber=10;
char choice=y;
float divide=0.0;

13

Memory Concepts
cin>>

first;

Assume

user entered 45

Variable

Identifier

first

45

first

45

second

72

first

45

second

72

cin>>second;
Assume

sum

user entered 72

= first + second;

sum

117

14

Assignment Operator (=)

= (assignment operator)
Assigns value to variable
Binary operator (two operands)
Example:
sum = variable1 + variable2;

15

How many bytes I am eating?


integer
data

short

2 bytes

int

2 bytes(16 bit system)


4 bytes (32 bit system)

long

4 bytes

float

4 bytes

double

8 bytes

long double

10 bytes

Character

char

1 byte

Boolean

bool

1 byte

Floating
point data

16

Memory Allocation for Arithmetic


Data Types
Size (bytes)
Name (alternate)

Description

char
short int (short)

int

Range of Values

AVR GCC

MSVC++

Character or small integer

Signed: -128 to 127 (-2(8-1) to 2(8-1)-1)


Unsigned: 0 to 255 (0 to 28-1)

Signed: -128 to 127


Unsigned: 0 to 255

Short integer

Signed: -32768 to 32767


Unsigned: 0 to 65535

Signed: -32,768 to 32,767


Unsigned: 0 to 65535

Signed: -32768 to 32767


Unsigned: 0 to 65535

Signed: -2,147,483,648 to
2,147,483,647
Unsigned: 0 to 4,294,967,295
Signed: -2,147,483,648 to
2,147,483,647
Unsigned: 0 to 4,294,967,295

Integer

AVR GCC

MSVC++

long int (long)

Long integer

Signed: -2,147,483,648 to
2,147,483,647
Unsigned: 0 to 4,294,967,295

long long int (long long)

Really long integer

Signed: -9.2E+18 to 9.2E+18


Unsigned: 0 to 1.8E+19

Signed: -9.2E+18 to 9.2E+18


Unsigned: 0 to 1.8E+19

float

'Single-precision' floating point number

1E 38 (7 decimal digits of
precision)

1E 38 (7 decimal digits of
precision)

double

'Double-precision' floating point number

1E 38 (7 decimal digits of
precision)

1E 308 (15 decimal digits of


precision)

long double

Long double-precision floating point


number

17

1E 308 (15 decimal digits of


precision)

18

Arithmetic

Rules of operator precedence

Operators in parentheses evaluated first


Nested/embedded parentheses

Operators in innermost pair first

Multiplication, division, modulus applied next


Operators applied from left to right
Addition, subtraction applied last
Operators applied from left to right

19

Comments

Non - executable statements


Comments are used for program
documentation
Two formats

Single Line Comments

// This program is used to show the Welcome


Message.

Multi Lines Comments

/*
This program is used to show the
square of even numbers from 10 to
100.
*/
20

Tokens
Tokens are individual words and punctuation marks in passage of text.
In C++, program the smallest individual units are known as C Tokens.
C++ has Six types of Tokens. The Tokens are shown in figure. C++
programs are written using these tokens and the syntax of the
language.

21

22

C++ keywords
Each

keyword has a predefined purpose in


the language.
Do not use keywords as variable and
constant names!!

Exmples: bool, break, case, char, const,


continue, do, default, double, else, extern,
false, float, for, if, int, long, namespace,
return, short, static, struct, switch,
typedef, true, unsigned, void, while
etc
etc..
23

Thank
You

New
Computer
Scientists

24

Memory Concepts
Variable

names

Correspond to actual locations in computer's


memory
Every variable has name, type, size and value
When new value placed into variable, overwrites
previous value

25

Das könnte Ihnen auch gefallen