• No results found

storageclass24-10.ppt

N/A
N/A
Protected

Academic year: 2020

Share "storageclass24-10.ppt"

Copied!
26
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)
(3)

A block of statement in C is surrounded

by curly braces i.e { and }.

We can declare variables inside the

block, such variables called as

local,can be referenced only within the

body of the block.

When the control reaches the opening

brace ,these variables come into

existence and get destroyed as soon as

the control leaves the block through

(4)

Click on to see example While(flag) { int x; if(i>x) {

int j; ………..

…….. ………. }

}

1.Scope of

x

is the while block.

2.j

is the if block.

3.The variable

x

can also be referenced to in the if

block because this block is completely enclosed in the

while block.

4.Scope of

j

is only in the if block.

(5)

The function scope pertains to the

labels declared in a function in the

sense that a label can be used

(6)

Global variables are declared outside all

blocks and functions, they are available

to all the blocks and functions.

The scope of a global variable is in the

entire program file. This type of scope

is known as file scope.

(7)

Variables have different type of behavior.

Some variables used in function

sub-program locally, but others execute

globally.

Variables are stored in memory in different

ways by using the function.

Every time execution period (i.e life time)

and boundary(i.e scope and visibility) of

the variable vary in function subprograms.

Every variable in c language has powerful

(8)

int qty,rate;

main()

{

int gross,net;

---}

fun()

{

char c;

{

int a;

}

}

Click to see scope rules

(9)
(10)

If we do not specify

storage class of a

variable compiler

will assume its

storage class as

default i.e

automatic.

Each and every

(11)

Storage place of the variable i.e memory

or CPU registers.

The initial value of a variable.

The scope or the visibility of a variable

The life time of a variable i.e how long the

(12)

1.

Automatic storage class(with specifier

auto

).

2.

Register storage class(with specifier

register

).

3.

Static storage class(with specifier

static

).

4.

External storage class(with specifier

(13)

Storage Memory

Default initial value Garbage Value i.e

unpredictable value

Scope or visibility Local or visible to the block in

which variable is declared eg.if variable is declared in a function it is only visible to that function.

Life Time It retains its value till the

control remains in the block in which it is declared.

(14)

main() {

auto int i , j=5; int k , l = 10;

printf(“\n value of i=%d \n value of j= %d”, i , j); printf(“\n value of k = %d \n value of l=%d”,k,l); }

Output:

Value of i = 2009

Value of j=5

(15)

main() {

auto int x = 5; {

auto int x = 10; {

auto int x = 15; {

printf(“\n %d”,x); }

printf(“%d”,x); }

printf(“%d”,x); }

}

output:

(16)

.

Storage CPU registers

Default initial value Garbage Value i.e

unpredictable value

Scope or visibility Local or visible to the block in

which variable is declared eg.if variable is declared in a

function it is only visible to that function.

Life Time It retains its value till the

control remains in the block in which it is declared.

(17)

main()

{

register int i;

for(i=1;i<=100;i++)

{

printf(“\n %d”,i);

}

}

(18)

.

Storage Memory

Default initial value zero

Scope or visibility Local or visible to the block in

which variable is declared e.g. if variable is declared in a

function it is only visible to that function.

Life Time It retains its value between

different function calls i.e.

when function is called for the first time, static variable is

created with initial value zero and in subsequent calls it

(19)

main() { int i,r; for(i=1;i<=5;i++) { r=tot (i);

printf(“\t %d”,r); }

getch(); }

tot (int x) {

static int s=0; s=s+x;

return (s); }

main() calls the function tot() five times, each time sending the value of i varying from 1 to 5.In the

function value is added to s and the accumulated result is returned.Once this program is executed following result is returned.

output

(20)

.

Storage Memory

Default initial value zero

Scope or visibility Scope is local to the block.

Life Time Life is,as long as the program

(21)

Extern storage class for only those variables are used

almost all functions in the program.This would avoid unnecessary passing of the variables.

If a variable is declared (with global scope) in one file but

referenced in another, the extern keyword is used to inform the compiler of the variable's existence:

In declare.c: int farVar; In use.c:

{ extern int farVar;

int a;

a = farVar * 2; }

• Note that the extern keyword is for declarations, not

definitions.

• An extern declaration does not create any storage; that

(22)
(23)
(24)

int x = 2; /* global variable x */ void f(void) {

float x; /* local variable x to f(), different from global x */ float y;

x = 3.0; /* sets local x declared in this function f() to 3.0 */ /* global x remains the same */

{

int x; /* new local variable x */

x = 4; /* outer x remains values of 3.0 */ }

y = x; /* sets y to 3.0 */ }

/* global x is never modified by anything done in f() */

 This method of hiding variables is a potential source of bugs and

(25)

int x = 1; /* global variable x */ int main() {

int x = 2; /* local variable x to main */

printf(“local x in outer scope of main is %d\n”, x);

{

int x = 3; /* new local variable x */

printf(“local x in inner scope of main is %d\ n”, x);

}

printf(“local x in outer scope of main is %d\n”, x);

a();

printf(“local x in main is %d\n”, x); }

void a( void) {

int x = 10; /* initialized each time a() is called */

++x;

printf(“local x in a() is %d\n”, x); }

(26)

int x = 1; /* global variable x */ int main() {

int x = 2; /* local variable x to main */ printf(“1. local x in main is %d\n”, x);

a(); b(); c(); a(); b(); c();

printf(“2. local x in main is %d\n”, x); }

void a( void) {

int x = 10; /* initialized each time a() is called */ ++x;

printf(“local x in a() is %d\n”, x); }

void b( void) {

static int x = 20; /* initialized on 1st time b() is called. */ ++x;

printf(“local static x in b() is %d\n”, x); } void c( void) {

++x;

printf(“global x in c() is %d\n”, x); }

References

Related documents

((BMI or Body Mass Index) or (obesity) or (obese) or (overweight) or (weight gain) or (weight loss) or (Body fat) or (Fat mass) or (Weight control*) or (Weight maintain*) or

If you’re a beer buff, take a guided tour at Deschutes Brewery to learn more about how the craft beer scene got its start in Central Oregon, then visit a few.. of the city’s

Request approval to 1) accept a grant award from, and enter into a grant agreement with, the American Psychological Association Board of Educational Affairs to pursue accreditation

The State of California, Department of Insurance (CDI) has awarded the District Attorney¶s Office (DA) $4,700,955 for the Automobile Insurance Fraud (AIF) Program, $2,121,829 for

77273 with Caban Resources, LLC (Caban), effective upon Board approval to: (i) extend the term of the Agreement for the period July 1, 2015 through June 30, 2016 with an option

Pursuant to Insurance Code Sections 1872.8(b)(1)(d) (AIF), 1874.8 (Urban Grant), 1872.83(d) (WCIF), and 1872.85(c)(2) (DHIF), CDI is authorized to award and distribute certain funds

Upon the completed review and approval of OIG’s written request to RR/CC for online access to YODA, RR/CC Public Records Division Manager shall provide OIG with the User

Combining Properties and Evidence to Support Overall Confor- mance Claims: Safety-critical system development increasingly relies on using a diverse set of verification