<< Chapter < Page Chapter >> Page >

Example

#include<iostream.h>

int funct( int); // function prototype

int main()

{

int count, value; // count is a local auto variable

for(count = 1; count<= 10; count++)

value = funct( count);

cout<<count<<‘\t’<<value<<endl;

return 0;

}

int funct( int x)

{

static int sum = 100; // sum is a local auto variable

sum += x;

return sum;

}

The output of the above program:

1 101

2 103

3 106

4 110

5 115

6 121

7 128

8 136

9 145

10 155

Since sum has a permanent memory space it retains the same value in the period of time between leaving function and again entering it later.

Note:

  1. The initialization of static variables is done only once when the program is first compiled. At compile time, the variable is created and any initialization value is placed in it. Thereafter, the value in the variable is kept without further initialization each time is called. (compile-time initialization).
  2. All static variables are set to zero when no explicit initialization is given.

Register variables

Register variables have the same time duration as automatic variables. The only difference between register and automatic variables is where the storage for the variable is located.

Register variables are stored in CPU’s internal registers rather than in memory.

Examples:

register int time;

register double difference;

Global variable storage classes

Global variables are created by definition statements external to a function. Once a global variable is created, it exists until the program in which it is declared is finished executing.

Global variables may be declared as static or extern (but not both).

The purpose of the extern storage class is to extend the scope of a global variable beyond its normal boundaries. To understand this, we must notice that the programs we have written so far have always been contained together in one file. Thus, when you have saved or retrieved programs, you have only needed to give the computer a single name for your program. Larger programs typically consist of many functions stored in multiple files and all of these files are compiled separately. Consider the following exaple.

Example:

//file1

int a;

float c;

static double d;

.

.

int main()

{

func1();

func2();

func3();

func4();

.

}

int func1();

{

.

.

}

int func2();

{

.

.

}

//end of file1

//file2

double b;

int func3();

{

.

.

}

int func4();

{

.

.

}

//end of file2

Although the variable a has been declared in file1, we want to use it in file2. Placing the statement extern int a in file2, we can extend the scope of the variable a into file2.

Now the scope of the variable a is not only in file1, but also in func3 and func4.

//file1

int a;

float c;

static double d;

.

.

int main()

{

func1();

func2();

func3();

func4();

.

}

extern double b;

int func1();

{

.

.

}

int func2();

{

.

.

}

//end of file1

//file2

double b;

extern int a;

int func3();

{

.

.

}

int func4();

{

extern float c;

.

.

}

//end of file2

Besides, placing the statement extern float c; in func4() extends the scope of this global variable, created in file1, into func4(), and the scope of the global variable b, created in file2, is extended into func1() and func2() by the declaration statement extern double b; placed before func1().

Questions & Answers

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
NANO
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
Virgil
is Bucky paper clear?
CYNTHIA
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
NANO
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask