SlideShare una empresa de Scribd logo
1 de 15
Descargar para leer sin conexión
Scope Rule & Storage Class
2
Local Variables
 Local variable
– Variables declared inside a function
– Accessible only from the function in which it is declared
– When a function starts, the variables in the function is
created, and when the function ends the variables are
destroyed
• If the function executes again, new memory is assigned to the
variable. It’s value is not continued
int sum( int k ) {
int a ;
a = k + 3 ;
return a ;
}
3
Local Variables
 Example
#include <stdio.h>
void function() ;
void main(void)
{
int i = 1, j = 0;
function() ;
printf(“main : i=%d, j=%d n”, i, j);
}
void function()
{
int j=0, k = 1;
printf(“in function : j=%d, k=%d n”, j, k);
}
The same variables
or
Different variables with the same name?
4
Local Variables
 Example
#include <stdio.h>
void function() ;
void main(void)
{
int i = 1, j = 0;
function() ;
printf(“main : i=%d, j=%d n”, i, j);
}
void function()
{
int j=0, k = 1;
printf(“in function : j=%d, k=%d n”, j, k);
i = 0 ;
}
i j
main
j k
function
5
Local Variables
 Parameters of a function
– The parameters of a function are local variable of the function
#include <stdio.h>
void function(int) ;
int main(void)
{
int i = 0, j = 0;
function(i, j) ;
printf(“i=%d, j=%d n”, i, j);
}
void function(int i, int j)
{
int k = 0 ;
i++ ; j++ ;
printf(“i=%d, j=%d, k=%d n”, i, j, k);
}
i j
main
i j
function
k
6
Global Variables
 Global variable
– Variables declared outside functions
• When the program starts, global variables are created.
• When the program ends, global variables are destroyed
– Accessible at any functions
– Automatically initialized with 0
int g ;
int main()
{
printf( “%dn”, g ) ;
my_func() ;
printf( “%dn”, g ) ;
}
void my_func()
{
g = 10 ;
}
7
Global Variables
 Example
#include <stdio.h>
int g = 0;
int main(void)
{
int i =0, j=0 ;
...
}
void function(int i)
{
int j = 3 ;
g++ ;
}
i j
main
i j
function
g
your program
8
Global Variables
 Example
#include <stdio.h>
int g = 0;
int main(void)
{
int i =0, j=0 ;
function(i) ;
printf(“main : %d %d %dn”, i, j, g);
}
void function(int i)
{
int j = 3 ;
i++ ;
printf(“in function : %d %d %dn”, i, j, g);
g++ ;
}
i j
main
i j
function
g
your program
9
Static Variables
 Static variable
– Variables declared with static
– Similar to global variables
• When the program starts, static variables are created
• When the program ends, static variables are destroyed
• Initialized to 0, automatically
– Static variables declared inside a function
• Accessible only from the function in which it is declared
– Static variables declared outside functions
• Accessible only from the same source file in which it is declared
(Later, refer to making program by many source files)
10
Static Variables
 Example
void func(void)
{
static int s ;
int k ;
printf( “%d %dn”, k, ++s ) ;
k = 10 ;
printf( “%d %dn”, k, ++s ) ;
}
#include <stdio.h>
void func(void)
void main(void)
{
func() ;
func() ;
}
11
Static Variables
 Example: How many times the function is
called?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void do_nothing(void) ;
void main(void)
{
int j ;
srand( time(NULL) ) ;
for( j = 0 ; j < 10 ; j++ )
if( rand() % 2 ) do_nothing() ;
}
void do_nothing(void)
{
static int cnt ;
printf( “do_nothing is called %d time(s)n”, ++cnt ) ;
}
12
Register Variables
 Register variable
– Request to use registers which are the most fastest memory
– Declarable as only integer data type
– Not always allocated to the registers
• Even though you declare register variables, it may not be
allocated into registers.
[Ex]
void main() {
register int i; /* same to register i */
for(i = 0; i < LIMIT; ++i) {
…
}
}
13
Const
 Const
– Using to declare variables that cannot be modified
– When it is declared, must initialize it
– Not a Constant !! It is a Variable.
[Ex] const int a = 1;
[Ex] const int a = 1;
a = 4; /* error!! */
a++; /* error!! */
14
Scope Rules
 Scope Rules
– Identifiers declared inside blocks
• Accessible only from the block in which it is declared
• When the block starts, the variables are available, and when
the block ends, the variables die
– Identifiers declared outside block
• Accessible from all blocks
• If the program starts, the variables are available, and when the
program ends, the variables die
15
Scope Rules
 Scope Rules
#include <stdio.h>
int g ;
void function() {
static int a ;
printf(“a=%d g=%d”, a, g);
}
int main(void) {
int a = 3, b = 4;
{
int b=5;
printf(“%d %d”, a, b, g);
}
printf(“%d %d”, a, b, g);
function() ;
}
When each variable is
initialized and destroyed?
Which variables are printed?

Más contenido relacionado

La actualidad más candente

Check the output of the following code then recode it to eliminate fu
 Check the output of the following code then recode it to eliminate fu Check the output of the following code then recode it to eliminate fu
Check the output of the following code then recode it to eliminate fulicservernoida
 
storage class
storage classstorage class
storage classstudent
 
Python functions and loops
Python functions and loopsPython functions and loops
Python functions and loopsthirumurugan133
 
What is storage class
What is storage classWhat is storage class
What is storage classIsha Aggarwal
 
Presentation on function
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageJenish Bhavsar
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming Kamal Acharya
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++Reddhi Basu
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingNeeru Mittal
 

La actualidad más candente (20)

Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
Storage classes
Storage classesStorage classes
Storage classes
 
Check the output of the following code then recode it to eliminate fu
 Check the output of the following code then recode it to eliminate fu Check the output of the following code then recode it to eliminate fu
Check the output of the following code then recode it to eliminate fu
 
Storage classes
Storage classesStorage classes
Storage classes
 
storage class
storage classstorage class
storage class
 
Composite types
Composite typesComposite types
Composite types
 
Python functions and loops
Python functions and loopsPython functions and loops
Python functions and loops
 
What is storage class
What is storage classWhat is storage class
What is storage class
 
Loops in R
Loops in RLoops in R
Loops in R
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Pointers & functions
Pointers &  functionsPointers &  functions
Pointers & functions
 
STORAGE CLASSES
STORAGE CLASSESSTORAGE CLASSES
STORAGE CLASSES
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C Language
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
 
5.program structure
5.program structure5.program structure
5.program structure
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 

Similar a 11 2. variable-scope rule,-storage_class (20)

Interesting facts on c
Interesting facts on cInteresting facts on c
Interesting facts on c
 
6. function
6. function6. function
6. function
 
functions
functionsfunctions
functions
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
Unit 4
Unit 4Unit 4
Unit 4
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
visiblity and scope.pptx
visiblity and scope.pptxvisiblity and scope.pptx
visiblity and scope.pptx
 
Function lecture
Function lectureFunction lecture
Function lecture
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Functions
FunctionsFunctions
Functions
 
Function
FunctionFunction
Function
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
C functions list
C functions listC functions list
C functions list
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Functions
FunctionsFunctions
Functions
 

Más de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)웅식 전
 

Más de 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 

Último

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Último (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

11 2. variable-scope rule,-storage_class

  • 1. Scope Rule & Storage Class
  • 2. 2 Local Variables  Local variable – Variables declared inside a function – Accessible only from the function in which it is declared – When a function starts, the variables in the function is created, and when the function ends the variables are destroyed • If the function executes again, new memory is assigned to the variable. It’s value is not continued int sum( int k ) { int a ; a = k + 3 ; return a ; }
  • 3. 3 Local Variables  Example #include <stdio.h> void function() ; void main(void) { int i = 1, j = 0; function() ; printf(“main : i=%d, j=%d n”, i, j); } void function() { int j=0, k = 1; printf(“in function : j=%d, k=%d n”, j, k); } The same variables or Different variables with the same name?
  • 4. 4 Local Variables  Example #include <stdio.h> void function() ; void main(void) { int i = 1, j = 0; function() ; printf(“main : i=%d, j=%d n”, i, j); } void function() { int j=0, k = 1; printf(“in function : j=%d, k=%d n”, j, k); i = 0 ; } i j main j k function
  • 5. 5 Local Variables  Parameters of a function – The parameters of a function are local variable of the function #include <stdio.h> void function(int) ; int main(void) { int i = 0, j = 0; function(i, j) ; printf(“i=%d, j=%d n”, i, j); } void function(int i, int j) { int k = 0 ; i++ ; j++ ; printf(“i=%d, j=%d, k=%d n”, i, j, k); } i j main i j function k
  • 6. 6 Global Variables  Global variable – Variables declared outside functions • When the program starts, global variables are created. • When the program ends, global variables are destroyed – Accessible at any functions – Automatically initialized with 0 int g ; int main() { printf( “%dn”, g ) ; my_func() ; printf( “%dn”, g ) ; } void my_func() { g = 10 ; }
  • 7. 7 Global Variables  Example #include <stdio.h> int g = 0; int main(void) { int i =0, j=0 ; ... } void function(int i) { int j = 3 ; g++ ; } i j main i j function g your program
  • 8. 8 Global Variables  Example #include <stdio.h> int g = 0; int main(void) { int i =0, j=0 ; function(i) ; printf(“main : %d %d %dn”, i, j, g); } void function(int i) { int j = 3 ; i++ ; printf(“in function : %d %d %dn”, i, j, g); g++ ; } i j main i j function g your program
  • 9. 9 Static Variables  Static variable – Variables declared with static – Similar to global variables • When the program starts, static variables are created • When the program ends, static variables are destroyed • Initialized to 0, automatically – Static variables declared inside a function • Accessible only from the function in which it is declared – Static variables declared outside functions • Accessible only from the same source file in which it is declared (Later, refer to making program by many source files)
  • 10. 10 Static Variables  Example void func(void) { static int s ; int k ; printf( “%d %dn”, k, ++s ) ; k = 10 ; printf( “%d %dn”, k, ++s ) ; } #include <stdio.h> void func(void) void main(void) { func() ; func() ; }
  • 11. 11 Static Variables  Example: How many times the function is called? #include <stdio.h> #include <stdlib.h> #include <time.h> void do_nothing(void) ; void main(void) { int j ; srand( time(NULL) ) ; for( j = 0 ; j < 10 ; j++ ) if( rand() % 2 ) do_nothing() ; } void do_nothing(void) { static int cnt ; printf( “do_nothing is called %d time(s)n”, ++cnt ) ; }
  • 12. 12 Register Variables  Register variable – Request to use registers which are the most fastest memory – Declarable as only integer data type – Not always allocated to the registers • Even though you declare register variables, it may not be allocated into registers. [Ex] void main() { register int i; /* same to register i */ for(i = 0; i < LIMIT; ++i) { … } }
  • 13. 13 Const  Const – Using to declare variables that cannot be modified – When it is declared, must initialize it – Not a Constant !! It is a Variable. [Ex] const int a = 1; [Ex] const int a = 1; a = 4; /* error!! */ a++; /* error!! */
  • 14. 14 Scope Rules  Scope Rules – Identifiers declared inside blocks • Accessible only from the block in which it is declared • When the block starts, the variables are available, and when the block ends, the variables die – Identifiers declared outside block • Accessible from all blocks • If the program starts, the variables are available, and when the program ends, the variables die
  • 15. 15 Scope Rules  Scope Rules #include <stdio.h> int g ; void function() { static int a ; printf(“a=%d g=%d”, a, g); } int main(void) { int a = 3, b = 4; { int b=5; printf(“%d %d”, a, b, g); } printf(“%d %d”, a, b, g); function() ; } When each variable is initialized and destroyed? Which variables are printed?