A computer can neither think nor make any judgement on its own. Also it is impossible for any
computer to independently analyze a given data or follow its own method of selection. It needs
a program to tell what to do.
A program is a set of instructions that are arranged in a sequence that guides a computer to
solve a problem.
The process of writing a program is called programming.
1st Generation
Low Level
Languages
2nd Generation
4th Generation
It is the machine level programming language. This is a sequence of instructions written in the
form of Binary Numbers consisting of 1 and 0 to which computer responds directly.
Machine level language is also known as computers native language as this system of code
directly understood by the computer.
An instruction prepared in any machine language will have at least two parts:
The first part is the command or operations, which tells the computer what functions are
to be performed. All computers have an operation code of its functions.
The second part of the instruction is the operand which tells the computer where to find
and store the data that has to be manipulated.
ADVANTAGE : The code a user writes can run very fast and efficiently since it is directly
executed by the CPU.
DISADVANTAGES
It is difficult to develop and understand a program using 1GL.
Anybody going through the program for checking will have a difficult task understanding
what will achieve when this program is executed.
Machine language for one computer could be different for another computer.
ASSEMBLER: An assembler is a translator which takes its input in the form of assembly
language program and produces machine language code as its output.
An assembly language program cannot be executed by the machine directly as it is not in the
binary form. An assembler is needed in order to translate an assembly language program into
the object code executed by the machine.
ASSEMBLY LANGUAGE
ASSEMBLER OBJECT CODE
PROGRAM
ADVANTAGES
It is more convenient than machine language.
It gives a little more readability.
DISADVANTAGES
It is machine dependent.
It is not as fast as machine language
It is the refinement of second generation language. The third generation brought to make the
languages more programmers friendly. This includes features like improved support for
aggregate data types and expressing concepts in a way that favors the programmer, not the
computer. A 3GL improves over 2GL by having the computer take care of non-essential details
not the programmer. For example: ALGOL, COBOL, C, C++, C#, Java, Delphi etc.
COMPILER & INTERPRETER: The names compiler and interpreter primarily used for the
programs that translate source code from a high level language to low level language.
A compiler and interpreter is likely to perform many or all of the following operations: lexical
analysis, preprocessing, parsing, syntax-directed translation, code-generation and code-
optimization, e.g. Turbo C++.
COMPILER INTERPRETER
Scans the entire program first and Translate the program line by line.
then translate it into machine code. Every time the program is executed
Converts the entire program to object every line is checked its syntax error
code; when all the syntax errors are and then converted to equivalent
removed, execution takes place. machine code.
Slow for debugging. Good for fast debugging.
Execution time is less Execution time is more.
A 5GL is a programming language based around solving problems using constraints given to
the program rather than using an algorithm written by a programmer. Most constraints based
and logic programming languages and some declarative languages are 5GL.
While 4GL are designed to build specific programs, 5GL are designed to make the computer to
solve the given problem without the programmer.
This way the programmer only needs to worry about what problem need to be solved and what
conditions need to be met, without worrying about how to implement a routine or algorithm to
solve them.
5GL used mainly in artificial intelligence research e.g. Prolog, OPS5, Mercury.
C is a general purpose programming language developed between 1969 and 1973 by Dennis
Ritchie at the Bell Telephone Laboratories for use with the UNIX operating system.
Although C was designed for implementing system software, it is also widely used for
developing portable application softwares.
The initial development of C occurred at AT&T Bell Labs between 1969 and 1973; according to
Dennis Ritchie the most creative period occurred in 1972.
It was named C because its features were derived from an earlier language called B, while
according to Ken Thompson was a striped down version of BCPL programming language.
The origin of C is closely tied to the development of Unix Operating System, originally
implemented in assembly language on a PDP-7 by Ritchie and Thompson. Eventually they
decided to port the PDP-11.
Bs inability to take the advantage of some of the PDP-11 features led to the development of
any early version of C.
C++ programming language was developed at AT&T Bell Labs in the early 1980 by Bjarne
Stroustrup. C++ provides a number of features that spruce up the C language, but more
importantly, it provides capabilities for object oriented programming.
In short, C++ is the upgraded version of C.
A C++ program is a collection of one or more functions. Each function performs some particular
task and collectively they solve the entire problem.
A function is like a black box that does something for you without exposing the internals
inside.
Every C++ program must have a function named main.
One can assume main() as master and other functions as servants.
C++ begins execution at main() even if main is not the first function in the program.
The header file contains the definition of various built-in functions. e.g.
<iostream.h> contains definition input-output functions for C++
<stdio.h> contains definition input-output functions for C
Comments in a program can be useful for a variety of purpose. They provide the easiest way to
set off specific parts of codes and their purposes; as well as providing a visual split between
various parts of the code.
Comments cannot be executed by the compiler. Comments come in two forms:
// Single line comment
/* Multi line
Comments */
These are the fundamental building blocks of a program and are used as general terminology
for the names given to different parts of the program, viz. variables, functions etc.
An identifier is a series of characters consisting of letters, digits and underscore.
An identifier can be of any length, but only first 31 characters are required to be recognized
by the C++ compiler.
C++ is case sensitive- uppercase and lowercase characters are different in C++.
Note: Avoid starting identifiers with the underscore to prevent the conflict with the
compiler generated identifiers and standard library identifiers.
Keywords are the words that convey a special meaning to language compiler. These are
reserved for special purpose and must not be used as normal identifier names. e.g. if, default,
else, do, for, while, float, int, unsigned, void, short etc.
Literals (often referred to as constants) are data items that never change their value during a
program run. C++ allows several kinds of literals:
Integer Constant
Integers are the whole numbers without any fractional part. An integer constant must have at
least one digit and must not contain decimal point. It may contain either + or sign. A number
with no sign is considered to be as positive. Commas cannot appear in a integer constant. C++
allows three types of integer constant:
Decimal (base 10): An integer constant consisting of a sequence of digits is taken to be decimal
integer constant unless it is begins with zero.
Octal (base 8): A sequence of digits starting with zero is taken to be as octal integers.
Hexadecimal (base 16): A sequence of digit preceded by 0x or 0X is taken to be as
hexadecimal integers. Example:
String Literal
Multiple character constants are treated as string literals. A string literal is a sequence of
characters surrounded by double quotes.
The difference between a string literal and a multi-character constants are treated as integers
and their values are dependent. Whereas a string literal is treated as sequence of characters
(array of chars).
Each string literal is by default added with special character \0 which makes the end of string.
\0 is a single character. Thus the size of the string is the number of characters plus one for
this terminator. e.g. abc is of size 4 and \ab is of size 3.
Brackets [ ] Colon :
Opening and closing brackets indicates single It indicates a labeled statement.
and multi-dimensional array subscript. Asterisk *
Parentheses ( ) It is used for pointer declaration. The
Opening and closing parentheses indicates asterisk is also used as an operator to either
function calls and function parameters. pointer or the multiplication operator.
Parentheses also group expressions and Ellipsis
isolate conditional expressions. It is used in the formal argument lists of
Braces { } functions prototypes to indicate a variable
Opening and closing braces indicate the start number of argument (which means argument
and end of compound statement. list may contain any number of arguments
Comma , and of any data type).
It is used as separator in a function Equal to =
argument list. It is used for variable initialization and as an
Semicolon ; assignment operator in expressions.
It is used as statement terminator. Pound sign #
It is used for preprocessor directive.
FILES LINKS
GLOBAL DECLARATION
main ( )
FUNCTIONS
Data types are means to identify the type of data and associated operations of handling it.
Data types are of two types:
Fundamental: int, char, float, double, void
Derived: array, functions, pointers, references, classes, structures, union, enumerations
This data type represents character. Characters can store any number of the C++ basic
character set. If a character from this set is stored in char, its value is equivalent to the
integer code (ASCII code) of the character because it is very easy to code numbers into binary.
A number having fractional part is a floating point number. e.g. 3.14159. Decimal point
signals it is a floating point number not an integer, for example 12 is an integer but 12.0 is
This is also used for handling floating point numbers. But it occupies twice as much memory
as type float and store floating point numbers with much larger range and precision. There
are 15 digits after decimal place in a double floating point number.
The void data type specifies an empty set of values. It is used as the return data type for the
functions that do not return a value. No object of type void may be declared.
TYPE SIZE RANGE
int 2 bytes = 16 bits 2 16 -32768 32767
char 1 byte = 8 bit 2 8 -128 127
float 4 byte = 32 bits 2 32 3.410-38 - (3.410-38-1)
double 8 byte = 64 bits 264 1.710-308 - (1.710-308-1)
A variable refers to the storage location referenced by identifiers where content can varying
during processing.
There are two values associated with symbolic variables:
rvalue: it is the data value stored at variable
lvalue: it is the location value (address of variable in the memory)
This keyword creates constant variables. It means the value of variable remains same
throughout the program. e.g. const float pi=3.1428;
The operations (specific tasks) are represented by operators. The objects of the operation are
referred to as operand. e.g.
Expression
a+b
Operand
Operator
There are three types of operators:
Unary operators: Unary operators. e.g. +a, -a Operators that act on two
Operators that act on one etc. operands are referred to as
operand are referred to as Binary operators: Binary operators. e.g. a+b,
a-b etc.
Order of evaluation (precedence): ( ) ... unary ... unary ... * ... / . .. % ... ...
= is known as assignment operator. It causes the value on the right to be assigned to the
variable on the left. e.g. a 5 It stores 5 in variable a.
They are used only for numbers and characters, not for strings.
If comparison is true, then result is 1 and if comparison is false, then result is 0.
Logical operators refer the ways by which relational operators are connected.
& & Logical AND || Logical OR ! Logical NOT
If comparison is true, then result is 1 and if comparison is false, then result is 0.
, , * , / , %
Example: x x 3 & x 3 both have same meaning.
Prefix: The prefix increment and decrement Postfix: The prefix increment and decrement
operators follow: change then use rule i.e. they operators follow: use then change rule i.e. they
first change the value of their operand, then first use the value of their operand in
use the new value in evaluating the evaluating the expression, then change the
expression. operands value.
P1. Store any two numbers in integer data type from user and perform all arithmetic
operations on them.
P2. Store a number in integer type & print table. Example:
7 14 21 28 35 42 49 56 63 70
6 7 8
12 14 16
18 21 24
24 28 32
30 35 40
36 42 48
42 49 56
48 56 64
54 63 72
60 70 80
P5. Store SALE and PURCHASE in float data type from user and print: PURCHASE,
SALE, COMMISION, SALES TAX, VAT TAX and PROFIT/LOSS
COMMISION = 5% OF SALE SALES TAX = 4% OF SALE
VAT TAX = 11% OF SALE
PROFIT/LOSS = SALE PURCHASE SALES TAX VAT TAX
P6. Store a number in int data type from the users and calculate the sum of numbers from
n(n 1)
1 to the number entered by the user. Sum = .
2
P7. Store four numbers from user a and b (int) and c and d (flaot). Evaluate the following
expressions:
c cd cd a
a. 3 e.
d c. 3 5 b
b. (c d )(c d ) 4c 1
f.
1 cd
d. d
c
P8. WAP that converts a Celsius temperature to Fahrenheit. The program should print both
9
the values with identifying message. F C 32
5
P9. Store radius in float type from user and print the diameter, circumference and area of
the circle Must declare a constant PI = 3.14285
P10. Input the Principal Amount, rate of interest and the time period from the user and
P R T
calculate Interest Amount. I .
100
P1. A computer programming contest requires teams of 5 members each. WAP that asks for
the number of players, and then gives the number of teams and number of players left
over.
The process of converting one predefined data type to another is called Type Conversion.
As an example, consider the following program:
# include iostream.h
void main( )
{
int i 7;
float f 155.5;
double d i * f;
cout " d " d;
}
Here a variable of type int is multiplied by a variable of type float to yield a result of type
double.
This program compiles without errors, the compiler considers it normal that you want to
perform any arithmetic operations on different types.
Not all languages are this relaxed. Some dont permit mixed expressions and would flag
as an error the line that performs the arithmetic in MIXED. Such languages assume that when
you mix types youre making a mistake and they try to save you from yourself.
C++ and C, however, assume that you must have a good reason for doing what you are
doing and they help to carry out your intentions. This is one reason for the popularity of C++
and C. They give you more freedom.
Type conversions come in two forms:
Type Promotion
It is the process of assigning a value of smaller data to a value of larger data type. In the
evaluation of expressions of more than one operand data type conversions obey the following
rules:
Steps If eithers type is The resultant type of other operand
1 Long Double Long Double
2 Double Double
3 Float Float
4 Char or Short int Int or unsigned int
5 Unsigned Long Unsigned Long
e.g. char ch; int i; float ft; double db; long double ld;
result (ch / i) (fl * db) (fl i) (ld / fl)
int double
float long
double
int double
float
double
long
double double
long
double
Type demotion
It is the process of assigning a value of larger data type to the value of smaller data type.
o When a double value is converted to float, the value is rounded to the precision of
float, before truncation occurs. If the result is out of range the behavior is undefined.
o Converting floating point value to integer type the rule is: If the value is small enough
for it to fit into an int, the digits after its decimal points are discarded and the int
variable is assigned the integer part of the floating point number. e.g. if 7.68 is assign
to int, the value is 7 and if -4.65 is assign to int, the value is -4 or -5 depending on
machine. If the value is out of range, the result is undefined.
o Converting bigger integer type to smaller int type: if the data is out of range, the
result is undefined.
An explicit type conversion is user defined that forces an expression to be of specific type.
e.g. float ( x y) / 2
Decision box
Start / Stop
Flow of Control
Connector
This statement is similar to the conditional operator, but it allows you to expand the scope of
your decision making by providing much more flexibility.
P1. WAP that accepts two integers and print largest of two.
P2. WAP that accepts three integers and print largest of three. You can use logical
operators.
P3. WAP which accepts a number and print whether it is odd or even.
P4. WAP which inputs a year in integer and prints whether it is leap year or not.
P5. WAP which accepts month in number and print this month in words. For example: if
input is 10 it prints October.
P6. WAP which accepts month in number and check whether it has 30 days, 31 days, 28/29
days.
P7. WAP which inputs a char and print whether it is vowel or consonant.
P8. WAP which inputs a char and check whether it is a number, capital alpha, small alpha
or a symbol.
P9. Write a Temperature conversion program that gives the option of converting
Fahrenheit to Celsius or Celsius to Fahrenheit and depending upon the users choice
carries out the conversion.
P10. WAP to create equivalent of four function calculator. The program requires the user to
enter two numbers and an operator. It then carries out the specified arithmetical
operation. Finally, it displays the result.
P11. WAP to calculate commission for the salesman. The commission is calculated according
to following rates:
SALES RATE
0 5000 0%
5001 12000 3%
12001 22000 7%
22001 30000 10%
30001 onwards 15%
The program accepts the sales made by the Salesman and price of sale and displays the
calculated commission.
P18. WAP to calculate the area of circle, rectangle and a triangle depending upon users
choice.
P19. Given three numbers, WAP to write their values in descending order.
P20. WAP to convert lowercase character to uppercase character.
P21. WAP that inputs two coordinates and find the slope of line joining these coordinates.
Also, check whether the line is vertical or horizontal.
The switch statement allows a program to determine the best course of execution based on
an expression. Syntax:
switch (expression)
{
case constant - exp1 :
default :
statement;
statement;
break;
}
case constant - exp2 :
statement;
break;
Here, break keyword cause the entire Switch statements to exit. The default keyword gives the
Switch construction a way to take an action if the value of expressions does not match any of
the case statement.
Nested Switch: Like if statements, a switch statement can also be nested. There can be a
switch as a part of the statement sequence of another switch.
Some important things about Switch:
A switch statement can only work for equality comparisons.
No two case labels in the same switch can have identical values. But in case of nested
switch, the case constants of the inner and outer switch can contain common values.
If character constants are used in the switch statement, they are automatically
converted to their integers.
The switch statement is more efficient than if in a situation that supports the
nature of switch operation.
P1. Complete P36, P37, P40, P42, P43, P45, P51 by using Switch.
P2. WAP that inputs a three digit number and print it in words. e.g. 123 One Two
Three
The jump statements unconditionally transfer program control within a function. There are
four jump statements in C++: goto, return, break, continue
b c d e f f e d c b
P7.
Enter a number = 9
Enter a number = 6
Enter a number = 6
Enter a number = 9
6 7 8 9 9 8 7 6
P8. Write a calculator program that does not exit until the user wants.
Parts of a Loop
INITIALIZATION UPDATE
TEST EXPRESSION BODY OF LOOP
EXPRESSION EXPRESSION
Before entering in The test expression is an The update This contains the
a loop, its control expression whose value expression changes statement that are
variable must be decides whether the the value of loop executed repeatedly
initialized. The loop-body will be variable. from the body of loop.
initialization of the executed or not. The update In an entry-
control variable If the test expression expression is controlled loop, first
takes place under evaluates to true (1), the executed at the end the test expression is
initialization of loop body gets executed of the loop after the evaluated if it is true
expression. otherwise (0) the loop is loop body is then body of the loop
The initialization terminated. executed. is executed,
expression is In an entry-controlled otherwise loop is
executed only once, loop, the test expression terminated.
in the beginning of is evaluated before In an exit-controlled
the loop. entering into a loop loop, the body of loop
whereas in an exit- is executed first and
controlled loop, the test then the test
expression is evaluated expression is
before exiting from the evaluated. If it is
loop. false, the loop is
for & while are entry- terminated otherwise
controlled loops. repeated.
do-while is exit-
controlled loop.
FALSE
TEST EXIT
TRUE
BODY
UPDATE EXP.
x 2 5x 5
P7. WAP to tabulate the function f ( x ) . x should take the values
x 3
10,8,...,8,10 .
P8. WAP to display rectangle operations and perform according to users choice.
P9. WAP that prints 1, 2, 4, 8,16, 32, 64 ,128 with and without math.h
The loop iterates/executes over and over as long as the expression is true when it is
tested. When the expression is false the program skips the body and execution continues.
While loop is an entry controlled loop.
While loop is suitable in the conditions where it is not known when the loop is
terminated.
While is preferred when you want to execute the loop body even once in case the test
condition is false.
TRUE
BODY
The do-while loop is an exit controlled loop i.e., it evaluates the test expression at the bottom
of the loop after executing its loop body statements. This means that a do-while loop always
executes at least once.
do
{
Syntax:
------
} while(test expression);
The do-while loop is a good choice when you asking a question, whose answer will determine if
the loop is to be repeated or terminated.
Working of do-while loop
BODY
FALSE
WHILE EXIT
TRUE
The continue statement skips the rest of the loop statements and causes the next iteration of
the loop. Following examples shows the difference between continue and break:
for(int i 1; i 10; i) for(int i 1; i 10; i)
{ {
if(i%3 0) if(i%3 0)
continue; break;
else else
cout i " " ; cout i " " ;
} }
Output: Output:
1 2 4 5 6 7 8 9 10 1 2
P1. WAP that inputs 15 numbers and print them with labelling.
P2. WAP that input 7 numbers and print their tables.
P3. WAP that input 5 numbers and find the largest number.
P4. WAP that input 5 numbers and print their sum.
P5. WAP that input 10 numbers and store them in another array in reverse order.
P6. WAP that input 10 numbers and divide into two new arrays of size 5 each.
P7. WAP that reverse the order of elements in an array.
P8. WAP that calculates the mean and standard deviation of integers.
x ( x x )2
x
n n
P1. WAP that inputs a 3 3 matrix from user and convert it into 91 matrix.
P2. WAP that inputs a 91 matrix from user and convert it into 3 3 matrix.
P3. WAP that inputs two 81 matrices from user and convert it into a 4 4 matrix.
P4. WAP that inputs a 3 3 matrix and convert it into three 1 3 matrices.
P5. WAP that inputs a 3 3 matrix and convert it into three 1 3 matrices with data in
reverse order.
P6. WAP that inputs a 3 3 matrix and convert it into a new matrix as per following
example:
INPUT OUTPUT
2 4 5 5 4 2
5 8 9 9 8 5
2 1 7 7 1 2
P7. WAP that inputs a 3 3 matrix and convert it into a new matrix as per following
example:
INPUT OUTPUT
2 4 5 2 5 2
5 8 9 4 8 1
2 1 7 2 1 7
P8. WAP that inputs a 3 3 matrix and convert it into a new matrix as per following
example:
INPUT OUTPUT
2 4 5 5 9 7
5 8 9 4 8 1
2 1 7 2 5 2
P10. WAP that inputs two 3 3 matrices and perform operations as per following example:
2 4 5 5 9 7 -3 -5 -2
5 8 9 4 8 1 1 0 8
2 1 7 2 5 2 0 -4 5
P11. WAP that inputs two 3 3 matrices and perform operations as per following example:
2 4 5 5 9 7 7 14 9
5 8 9 4 8 1 8 16 2
2 1 7 2 5 2 7 14 9
P12. WAP that inputs two 3 3 matrices and perform operations as per following example:
2 4 5 5 9 7 -3 0 3
5 8 9 4 8 1 -4 0 4
2 1 7 2 5 2 -5 0 5
P13. WAP that inputs a 3 3 matrix and convert it into a new matrix as per following
example:
INPUT OUTPUT
2 4 5 2
5 8 9 8
2 1 7 7
P14. WAP that inputs a 3 3 matrix and convert it into a new matrix as per following
example:
INPUT OUTPUT
2 4 5 5
5 8 9 8
2 1 7 2
P15. WAP that inputs a 3 3 matrix and convert it into a new matrix as per following
example:
INPUT OUTPUT
2 4 5 2 5
5 8 9 8
2 1 7 2 7
P16. WAP that inputs a 3 3 matrix and convert it into a new matrix as per following
example:
INPUT OUTPUT
2 4 5 4
5 8 9 5 9
2 1 7 1
Large programs are generally avoided because it is difficult to manage a single list of
instructions. Thus, a large program is broken in to smaller units known as functions.
A function is a named unit of a group of programming statements. This unit can be invoked
from other parts of a program.
There are two types of functions:
Built-in (Library) functions: These functions are part of the compiler package.
These are parts of standard library made available by the compiler. e.g. sqrt( ), pow( ),
clrscr( ), getch( ), getche( ) etc.
In C++, you must declare every identifier before it can be used. In case of functions, a function
declaration must be preceded any physical function call.
The declaration tells the compiler that at some later point we plan to present a function.
Syntax: data - type function - name (parameter - list);
Example: float volume(int a, float b, float c); or float volume(int, float, float);
In function declaration, variable names are optional.
If the data-type of the function is omitted is assumed to be returning int values.
A function definition consists of two parts: the function heading and the function body.
data - type function - name(parameter - list) int sum(int a, int b)
{ {
----------------- int c;
Syntax: Example:
body of function; c a b;
----------------- return c;
} }
It is used as the return type for the functions that do not return a value.
void message( )
{
VOID function with NO ARGUMENTS:
cout " Hello!" ;
}
void average(int a, int b)
{
float av;
VOID function with ARGUMENTS:
av (a b)/2;
cout " Average is " av;
}
ASHISH KUMAR :: 9990781319 :: 31
char grade( )
{ char g;
if(p 60) g ' A' ;
NON-VOID function with NO ARGUMENTS:
else g ' B' ;
return g;
}
float average(int a, int b)
{
float av;
NON-VOID function with ARGUMENTS:
av (a b)/2;
return av;
}
The region of program code where it is legal to access an identifier is known as scope.
There are four kinds of scopes in C++:
Local Scope: A name declared in a block is local to that block and can be used only in it
and the other blocks contained under it
Global / File Scope: A name declared outside all blocks and functions has file scope i.e.
it can be used in all the blocks and functions written inside the file in which name
declaration appears.
Function Scope: The variables declared in the outermost block of a function have
function scope, i.e. they can be accessed only in the function that declares them.
Class Scope: Discuss Later
Example of Scope
# include.....
# include.....
int x, y;
FILE SCOPE
char ch;
void main( )
{
int a, b; FUNCTION
float amt; SCOPE
void check(int);
----------------
{
char grade; LOCAL
------------ SCOPE
}
}
void check(int i)
{ FUNCTION
long temp; SCOPE
}
If a variable declaration appears outside all the functions, it is said to be global variable. A
global variable is available to all the functions and blocks defined in a file.
A global variable comes into existence when the program execution starts and is destroyed
when the program terminates. Global variables hold their values throughout the program
execution. The scope of global variables is file scope.
When a function declares a local identifier with the same name as global identifier, the local
identifier takes precedence within the function.
# include.....
int a 20;
main( )
Example: { Output: 50
int a 50;
cout a;
}
To unhide global variables scope resolution operator (::) can be used.
# include.....
int a 20;
main( )
Example: { Output: 20 50
int a 50;
cout :: a "\t" a;
}
The rules that determine where in the program an identifier may be accessed, given the point
where that identifier declared.
These rules are: (not complete rules)
A function name has global scope. Function definitions cannot be nested within
function definitions.
The scope of formal parameter is identical to the scope of a local variable declared in
the outermost block of the function body.
The scope of global variable or constants extends from its declaration to the end of the
file.
The scope of local variable or constants extends from its declaration to the end of the
block in which it is declared.
The scope of an identifier does not include any nested block, that contains a locally
declared identifier with the same name (name precedence).
The period of time during program execution when an identifier has memory allocated to it.
Lifetime of Global Variables: During Program Run
Lifetime of Local Variables: During Function or Block Run
The storage class of variable and function determines which part of the program can access it
and how long it stays in existence.
ASHISH KUMAR :: 9990781319 :: 34
C++ provides four storage class specifiers: auto, register, static, extern
Syntax: storage - specifier data type variable;
auto register
The storage specifier auto refers to the register variables provide fast access as they
automatic variables. are stored inside the CPU registers rather
By default, all variables are automatic. than in memory. So the time required to
Not applied for global variables. read from memory is saved.
Scope Function scope A register variable has all the
Lifetime Between function creation and characteristics to the auto variable.
destruction. Scope Function scope
Lifetime Between function creation and
destruction.
Extern
C++ allows you to split large programs into different files. A file can have some of the
functions while other file can have rest of the functions. Then these files can be separately
compiled and then later linked for the program execution.
In such files, if you have declared a global variable in a file, C++ does not allow you to
declare it again in some other file. But, what if that other file is using the global variable
that is defined outside the file. The extern specifier here comes to rescue.
The extern specifier tells the compiler that the variable types and names that follow it have
been declared elsewhere, so that fresh memory is not allocated to these variables.
The extern specifier is only for global variables.
Scope file scope (they are accessible in the files where their declaration appears)
Lifetime Life of a program
static
The static variables are permanent within their own function or file.
Static Local Variables
When static modifier applies to a local variable, it is initialized only when the very first call to
the function that defines it occurs. It is not destroyed when the function terminates, rather, it
holds its value even after functions termination but it can only be accessed within its own
function.
Scope function slope
Lifetime during program run
Static Global Variables
By putting static keyword before a global variable declaration in a file makes sure that the
only functions in the same file can access it and not any other function residing in any other
file.
Scope file scope
Lifetime during program run
P1. Write a function to calculate the factorial value of any integer entered through the
keyboard.
P2. Write a function power(a, b), to calculate the value of a raised to b.
P3. Write a general purpose function to convert any given year into its roman equivalent.
Decimal Roman Decimal Roman
1 I 100 C
5 V 500 D
10 X 1000 M
50 L
P4. A positive integer entered through the keyboard. Write a function to obtain its prime
factors.
P5. According to Gregorian Calendar, it was Monday on the date 01/01/1900. If any year is
input through the keyboard, write a program to find out what is the day on 1st January
of this year.
P6. WAP to fill the entire screen with a smiling face. The smiling face has an ASCII value
1.
P7. WAP to generate all combinations of 1, 2 and 3 using for loop.
a. Factorial
b. Prime
c. Odd / Even
d. Exit
P9. Develop a program that receives the month and year from keyboard as integers and
prints the calendar.
The octal number system is very important in digital computer work. The octal number
system has a base of eight, meaning that it has eight unique symbols: 0 1 2 3 4 5 6 7
The octal systems also a positional value system.
Octal digit 472 .273 4 82 7 81 2 80 2 8 1 7 8 2 3 8 3
Sixty Fours Eights Ones
The hexadecimal system use base-16. Thus it has 16 possible digits symbols. These symbols
are: 0 1 2 3 4 5 6 7 8 9 a b c d e f
The hexadecimal system is also a positional value system.
Hexadecimal Digit 3DA. A3 3 16 2 D 161 A 16 0 A 16 1 3 16 2
Two hundred Sixteens Ones
& fifty sixes