SlideShare a Scribd company logo
1 of 25
Storage Classes in C
• Storage Classes are used to describe the
features of a variable/function.
• These features basically include the scope,
visibility, and lifetime which help us to trace
the existence of a particular variable during
the runtime of a program.
Scope
• Scope is defined as the availability of a
variable inside a program, scope is basically
the region of code in which a variable is
available to use.
There are four types of scope:
• file scope,
• block scope,
• function scope and
• prototype scope.
File Scope
• File scope of variables in C is defined as having
the availability of the variable throughout the
file/program. It means that the variable has a
global scope, and it is available all around for
every function and every block in the
program.
#include <stdio.h>
int x = 10;
void func() {
x += 10;
printf("Value of x is %dn", x);
}
int main() {
func();
x += 30;
printf("Value of x is %d", x);
return 0;
}
2. Block Scope
• Block scope of variables in C is defined as
when the variable has a limited scope, and the
memory occupied by the variable will be
deleted once the execution of the block ends.
The variable is not accessible or available
outside the block. A block of code can be
defined in curly braces {code_block}.
#include <stdio.h>
int main()
Int y;
{
{
int a = 5;
int b = 10;
int sum = a + b;
printf("Sum of a and b: %d", sum);
}
}
3. Function Scope
• Function scope of variables in C begins with
the left curly brace { and ends with a closing
right curly brace }. A variable declared inside a
function has a function scope. It has been
allocated memory when the function is called,
and once the function returns something, the
function execution ends and with it, the
variable goes out of scope, i.e. it gets deleted
from the memory.
#include <stdio.h>
void findAge() {
int age = 18;
}
int main() {
printf("Age is %d", age);//error
return 0;
}
4. Function Prototype Scope
• Function prototype scope of variables in C are
declared in some function as its parameters.
These variables are similar to the function
scope variables where a variable's memory
gets deleted once the function execution
terminates.
#include <stdio.h>
int findSum(int a, int b)
{
return a + b;
}
int main() {
int sum=a+b; //error
int sum = findSum(3, 5);
printf("Sum of 3 and 5 is %d", sum);
return 0;
}
Scope Rules
• Variables that are declared inside the function
or a block of code are known as local
variables.
• Variables that are declared outside of any
function are known as global variables
(usually at the start of a program).
• Variables that are declared in the definition of
a function parameters as its formal
parameters.
Local Variables
#include <stdio.h>
int main() {
int sum = 0;
{
int a = 5;
int b = 10;
}
sum = a + b;//error
return 0;
}
• Output :
• prog.c: In function ‘main’:
• prog.c:12:8: error: ‘a’ undeclared (first use in this function)
• sum = a + b;
• ^
• prog.c:12:8: note: each undeclared identifier is reported
only once for each function it appears in
• prog.c:12:12: error: ‘b’ undeclared (first use in this
function)
• sum = a + b;
Global Variables
#include <stdio.h>
int side = 10;
int main() {
int squareArea = side * side;
printf("Area of Square : %d", squareArea);
return 0;
}
Output :
Area of Square : 100
Formal Parameters
#include <stdio.h>
int square(int n) {
return n * n;
}
int main() {
int number = 5;
Printf(“%d”,n);//error
printf("Square of %d is %d.", number, square(number));
return 0;
}
Visibility
• Visibility of a variable is defined as if a
variable is accessible or not inside a particular
region of code or the whole program.
#include <stdio.h>
int main() {
int scope;
scope = 10;
{
float scope;
scope = 2.98;
printf("Inner block scope : %fn", scope);
}
printf("Outer block scope : %dn", scope);
return 0;
}
Lifetime of a variable
• Lifetime of a variable is the time for which the
variable is taking up a valid space in the
system's memory, it is of three types:
• static lifetime,
• automatic lifetime and
• dynamic lifetime.
Static Lifetime
• Objects/Variables having static lifetime will
remain in the memory until the execution of
the program finishes. These types of variables
can be declared using the static keyword,
global variables also have a static lifetime:
they survive as long as the program runs.
• Example :
• static int count = 0;
• The count variable will stay in the memory
until the execution of the program finishes.
• b. Automatic Lifetime
• Objects/Variables declared inside a block have
automatic lifetime. Local variables (those
defined within a function) have an automatic
lifetime by default: they arise when the
function is invoked and are deleted (together
with their values) once the function execution
finishes.
Example :
{
// block of code
int auto_lifetime_var = 0;
}
auto_lifetime_var will be deleted from the
memory once the execution comes out of the
block.
• c. Dynamic Lifetime
• Objects/Variables which are made during the run-
time of a C program using the Dynamic Memory
Allocation concept using the malloc(), calloc()
functions in C are stored in the memory until they
are explicitly removed from the memory using
the free() function in C or delete operator in C++
and these variables are said to have a dynamic
lifetime. These variables are stored in the heap
section of our system's memory, which is also
known as the dynamic memory.
Example :
int *ptr = (int *)malloc(sizeof(int));
• Memory block (variable) pointed by ptr will
remain in the memory until it is explicitly
freed/removed from the memory or the
program execution ends.

More Related Content

Similar to visiblity and scope.pptx

Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
 
What is storage class
What is storage classWhat is storage class
What is storage classIsha Aggarwal
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptxAshwini Raut
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1karmuhtam
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfBoomBoomers
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variablesSaurav Kumar
 
Global variables, sorting static variables,function and arrays,
Global variables, sorting static variables,function and arrays,Global variables, sorting static variables,function and arrays,
Global variables, sorting static variables,function and arrays,Ahmad55ali
 

Similar to visiblity and scope.pptx (20)

unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
What is storage class
What is storage classWhat is storage class
What is storage class
 
Storage class
Storage classStorage class
Storage class
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
C language
C languageC language
C language
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Functions
FunctionsFunctions
Functions
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
08 -functions
08  -functions08  -functions
08 -functions
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
Global variables, sorting static variables,function and arrays,
Global variables, sorting static variables,function and arrays,Global variables, sorting static variables,function and arrays,
Global variables, sorting static variables,function and arrays,
 

More from VGaneshKarthikeyan

Unit III Part I_Opertaor_Overloading.pptx
Unit III Part I_Opertaor_Overloading.pptxUnit III Part I_Opertaor_Overloading.pptx
Unit III Part I_Opertaor_Overloading.pptxVGaneshKarthikeyan
 
Linear_discriminat_analysis_in_Machine_Learning.pptx
Linear_discriminat_analysis_in_Machine_Learning.pptxLinear_discriminat_analysis_in_Machine_Learning.pptx
Linear_discriminat_analysis_in_Machine_Learning.pptxVGaneshKarthikeyan
 
K-Mean clustering_Introduction_Applications.pptx
K-Mean clustering_Introduction_Applications.pptxK-Mean clustering_Introduction_Applications.pptx
K-Mean clustering_Introduction_Applications.pptxVGaneshKarthikeyan
 
Numpy_defintion_description_usage_examples.pptx
Numpy_defintion_description_usage_examples.pptxNumpy_defintion_description_usage_examples.pptx
Numpy_defintion_description_usage_examples.pptxVGaneshKarthikeyan
 
Refined_Lecture-14-Linear Algebra-Review.ppt
Refined_Lecture-14-Linear Algebra-Review.pptRefined_Lecture-14-Linear Algebra-Review.ppt
Refined_Lecture-14-Linear Algebra-Review.pptVGaneshKarthikeyan
 
randomwalks_states_figures_events_happenings.ppt
randomwalks_states_figures_events_happenings.pptrandomwalks_states_figures_events_happenings.ppt
randomwalks_states_figures_events_happenings.pptVGaneshKarthikeyan
 
stochasticmodellinganditsapplications.ppt
stochasticmodellinganditsapplications.pptstochasticmodellinganditsapplications.ppt
stochasticmodellinganditsapplications.pptVGaneshKarthikeyan
 
1.10 Tuples_sets_usage_applications_advantages.pptx
1.10 Tuples_sets_usage_applications_advantages.pptx1.10 Tuples_sets_usage_applications_advantages.pptx
1.10 Tuples_sets_usage_applications_advantages.pptxVGaneshKarthikeyan
 
Neural_Networks_scalability_consntency.ppt
Neural_Networks_scalability_consntency.pptNeural_Networks_scalability_consntency.ppt
Neural_Networks_scalability_consntency.pptVGaneshKarthikeyan
 
Lecture-4-Linear Regression-Gradient Descent Solution.ppt
Lecture-4-Linear Regression-Gradient Descent Solution.pptLecture-4-Linear Regression-Gradient Descent Solution.ppt
Lecture-4-Linear Regression-Gradient Descent Solution.pptVGaneshKarthikeyan
 
1.3 Basic coding skills_tupels_sets_controlloops.ppt
1.3 Basic coding skills_tupels_sets_controlloops.ppt1.3 Basic coding skills_tupels_sets_controlloops.ppt
1.3 Basic coding skills_tupels_sets_controlloops.pptVGaneshKarthikeyan
 
Python_basics_tuples_sets_lists_control_loops.ppt
Python_basics_tuples_sets_lists_control_loops.pptPython_basics_tuples_sets_lists_control_loops.ppt
Python_basics_tuples_sets_lists_control_loops.pptVGaneshKarthikeyan
 
1.4 Work with data types and variables, numeric data, string data.pptx
1.4 Work with data types and variables, numeric data, string data.pptx1.4 Work with data types and variables, numeric data, string data.pptx
1.4 Work with data types and variables, numeric data, string data.pptxVGaneshKarthikeyan
 
Inheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybridInheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybridVGaneshKarthikeyan
 
Refined_Lecture-8-Probability Review-2.ppt
Refined_Lecture-8-Probability Review-2.pptRefined_Lecture-8-Probability Review-2.ppt
Refined_Lecture-8-Probability Review-2.pptVGaneshKarthikeyan
 
Refined_Lecture-13-Maximum Likelihood Estimators-Part-C.ppt
Refined_Lecture-13-Maximum Likelihood Estimators-Part-C.pptRefined_Lecture-13-Maximum Likelihood Estimators-Part-C.ppt
Refined_Lecture-13-Maximum Likelihood Estimators-Part-C.pptVGaneshKarthikeyan
 
Refined_Lecture-15-Dimensionality Reduction-Uunspervised-PCA.ppt
Refined_Lecture-15-Dimensionality Reduction-Uunspervised-PCA.pptRefined_Lecture-15-Dimensionality Reduction-Uunspervised-PCA.ppt
Refined_Lecture-15-Dimensionality Reduction-Uunspervised-PCA.pptVGaneshKarthikeyan
 
Bias-Variance_relted_to_ML.pdf
Bias-Variance_relted_to_ML.pdfBias-Variance_relted_to_ML.pdf
Bias-Variance_relted_to_ML.pdfVGaneshKarthikeyan
 
Refined_Lecture-1-Motivation & Applications.ppt
Refined_Lecture-1-Motivation & Applications.pptRefined_Lecture-1-Motivation & Applications.ppt
Refined_Lecture-1-Motivation & Applications.pptVGaneshKarthikeyan
 
Lecture-4-Linear Regression-Gradient Descent Solution.PPTX
Lecture-4-Linear Regression-Gradient Descent Solution.PPTXLecture-4-Linear Regression-Gradient Descent Solution.PPTX
Lecture-4-Linear Regression-Gradient Descent Solution.PPTXVGaneshKarthikeyan
 

More from VGaneshKarthikeyan (20)

Unit III Part I_Opertaor_Overloading.pptx
Unit III Part I_Opertaor_Overloading.pptxUnit III Part I_Opertaor_Overloading.pptx
Unit III Part I_Opertaor_Overloading.pptx
 
Linear_discriminat_analysis_in_Machine_Learning.pptx
Linear_discriminat_analysis_in_Machine_Learning.pptxLinear_discriminat_analysis_in_Machine_Learning.pptx
Linear_discriminat_analysis_in_Machine_Learning.pptx
 
K-Mean clustering_Introduction_Applications.pptx
K-Mean clustering_Introduction_Applications.pptxK-Mean clustering_Introduction_Applications.pptx
K-Mean clustering_Introduction_Applications.pptx
 
Numpy_defintion_description_usage_examples.pptx
Numpy_defintion_description_usage_examples.pptxNumpy_defintion_description_usage_examples.pptx
Numpy_defintion_description_usage_examples.pptx
 
Refined_Lecture-14-Linear Algebra-Review.ppt
Refined_Lecture-14-Linear Algebra-Review.pptRefined_Lecture-14-Linear Algebra-Review.ppt
Refined_Lecture-14-Linear Algebra-Review.ppt
 
randomwalks_states_figures_events_happenings.ppt
randomwalks_states_figures_events_happenings.pptrandomwalks_states_figures_events_happenings.ppt
randomwalks_states_figures_events_happenings.ppt
 
stochasticmodellinganditsapplications.ppt
stochasticmodellinganditsapplications.pptstochasticmodellinganditsapplications.ppt
stochasticmodellinganditsapplications.ppt
 
1.10 Tuples_sets_usage_applications_advantages.pptx
1.10 Tuples_sets_usage_applications_advantages.pptx1.10 Tuples_sets_usage_applications_advantages.pptx
1.10 Tuples_sets_usage_applications_advantages.pptx
 
Neural_Networks_scalability_consntency.ppt
Neural_Networks_scalability_consntency.pptNeural_Networks_scalability_consntency.ppt
Neural_Networks_scalability_consntency.ppt
 
Lecture-4-Linear Regression-Gradient Descent Solution.ppt
Lecture-4-Linear Regression-Gradient Descent Solution.pptLecture-4-Linear Regression-Gradient Descent Solution.ppt
Lecture-4-Linear Regression-Gradient Descent Solution.ppt
 
1.3 Basic coding skills_tupels_sets_controlloops.ppt
1.3 Basic coding skills_tupels_sets_controlloops.ppt1.3 Basic coding skills_tupels_sets_controlloops.ppt
1.3 Basic coding skills_tupels_sets_controlloops.ppt
 
Python_basics_tuples_sets_lists_control_loops.ppt
Python_basics_tuples_sets_lists_control_loops.pptPython_basics_tuples_sets_lists_control_loops.ppt
Python_basics_tuples_sets_lists_control_loops.ppt
 
1.4 Work with data types and variables, numeric data, string data.pptx
1.4 Work with data types and variables, numeric data, string data.pptx1.4 Work with data types and variables, numeric data, string data.pptx
1.4 Work with data types and variables, numeric data, string data.pptx
 
Inheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybridInheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybrid
 
Refined_Lecture-8-Probability Review-2.ppt
Refined_Lecture-8-Probability Review-2.pptRefined_Lecture-8-Probability Review-2.ppt
Refined_Lecture-8-Probability Review-2.ppt
 
Refined_Lecture-13-Maximum Likelihood Estimators-Part-C.ppt
Refined_Lecture-13-Maximum Likelihood Estimators-Part-C.pptRefined_Lecture-13-Maximum Likelihood Estimators-Part-C.ppt
Refined_Lecture-13-Maximum Likelihood Estimators-Part-C.ppt
 
Refined_Lecture-15-Dimensionality Reduction-Uunspervised-PCA.ppt
Refined_Lecture-15-Dimensionality Reduction-Uunspervised-PCA.pptRefined_Lecture-15-Dimensionality Reduction-Uunspervised-PCA.ppt
Refined_Lecture-15-Dimensionality Reduction-Uunspervised-PCA.ppt
 
Bias-Variance_relted_to_ML.pdf
Bias-Variance_relted_to_ML.pdfBias-Variance_relted_to_ML.pdf
Bias-Variance_relted_to_ML.pdf
 
Refined_Lecture-1-Motivation & Applications.ppt
Refined_Lecture-1-Motivation & Applications.pptRefined_Lecture-1-Motivation & Applications.ppt
Refined_Lecture-1-Motivation & Applications.ppt
 
Lecture-4-Linear Regression-Gradient Descent Solution.PPTX
Lecture-4-Linear Regression-Gradient Descent Solution.PPTXLecture-4-Linear Regression-Gradient Descent Solution.PPTX
Lecture-4-Linear Regression-Gradient Descent Solution.PPTX
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 

visiblity and scope.pptx

  • 1. Storage Classes in C • Storage Classes are used to describe the features of a variable/function. • These features basically include the scope, visibility, and lifetime which help us to trace the existence of a particular variable during the runtime of a program.
  • 2.
  • 3. Scope • Scope is defined as the availability of a variable inside a program, scope is basically the region of code in which a variable is available to use. There are four types of scope: • file scope, • block scope, • function scope and • prototype scope.
  • 4. File Scope • File scope of variables in C is defined as having the availability of the variable throughout the file/program. It means that the variable has a global scope, and it is available all around for every function and every block in the program.
  • 5. #include <stdio.h> int x = 10; void func() { x += 10; printf("Value of x is %dn", x); } int main() { func(); x += 30; printf("Value of x is %d", x); return 0; }
  • 6. 2. Block Scope • Block scope of variables in C is defined as when the variable has a limited scope, and the memory occupied by the variable will be deleted once the execution of the block ends. The variable is not accessible or available outside the block. A block of code can be defined in curly braces {code_block}.
  • 7. #include <stdio.h> int main() Int y; { { int a = 5; int b = 10; int sum = a + b; printf("Sum of a and b: %d", sum); } }
  • 8. 3. Function Scope • Function scope of variables in C begins with the left curly brace { and ends with a closing right curly brace }. A variable declared inside a function has a function scope. It has been allocated memory when the function is called, and once the function returns something, the function execution ends and with it, the variable goes out of scope, i.e. it gets deleted from the memory.
  • 9. #include <stdio.h> void findAge() { int age = 18; } int main() { printf("Age is %d", age);//error return 0; }
  • 10. 4. Function Prototype Scope • Function prototype scope of variables in C are declared in some function as its parameters. These variables are similar to the function scope variables where a variable's memory gets deleted once the function execution terminates.
  • 11. #include <stdio.h> int findSum(int a, int b) { return a + b; } int main() { int sum=a+b; //error int sum = findSum(3, 5); printf("Sum of 3 and 5 is %d", sum); return 0; }
  • 12. Scope Rules • Variables that are declared inside the function or a block of code are known as local variables. • Variables that are declared outside of any function are known as global variables (usually at the start of a program). • Variables that are declared in the definition of a function parameters as its formal parameters.
  • 13. Local Variables #include <stdio.h> int main() { int sum = 0; { int a = 5; int b = 10; } sum = a + b;//error return 0; }
  • 14. • Output : • prog.c: In function ‘main’: • prog.c:12:8: error: ‘a’ undeclared (first use in this function) • sum = a + b; • ^ • prog.c:12:8: note: each undeclared identifier is reported only once for each function it appears in • prog.c:12:12: error: ‘b’ undeclared (first use in this function) • sum = a + b;
  • 15. Global Variables #include <stdio.h> int side = 10; int main() { int squareArea = side * side; printf("Area of Square : %d", squareArea); return 0; } Output : Area of Square : 100
  • 16. Formal Parameters #include <stdio.h> int square(int n) { return n * n; } int main() { int number = 5; Printf(“%d”,n);//error printf("Square of %d is %d.", number, square(number)); return 0; }
  • 17. Visibility • Visibility of a variable is defined as if a variable is accessible or not inside a particular region of code or the whole program.
  • 18. #include <stdio.h> int main() { int scope; scope = 10; { float scope; scope = 2.98; printf("Inner block scope : %fn", scope); } printf("Outer block scope : %dn", scope); return 0; }
  • 19. Lifetime of a variable • Lifetime of a variable is the time for which the variable is taking up a valid space in the system's memory, it is of three types: • static lifetime, • automatic lifetime and • dynamic lifetime.
  • 20. Static Lifetime • Objects/Variables having static lifetime will remain in the memory until the execution of the program finishes. These types of variables can be declared using the static keyword, global variables also have a static lifetime: they survive as long as the program runs.
  • 21. • Example : • static int count = 0; • The count variable will stay in the memory until the execution of the program finishes.
  • 22. • b. Automatic Lifetime • Objects/Variables declared inside a block have automatic lifetime. Local variables (those defined within a function) have an automatic lifetime by default: they arise when the function is invoked and are deleted (together with their values) once the function execution finishes.
  • 23. Example : { // block of code int auto_lifetime_var = 0; } auto_lifetime_var will be deleted from the memory once the execution comes out of the block.
  • 24. • c. Dynamic Lifetime • Objects/Variables which are made during the run- time of a C program using the Dynamic Memory Allocation concept using the malloc(), calloc() functions in C are stored in the memory until they are explicitly removed from the memory using the free() function in C or delete operator in C++ and these variables are said to have a dynamic lifetime. These variables are stored in the heap section of our system's memory, which is also known as the dynamic memory.
  • 25. Example : int *ptr = (int *)malloc(sizeof(int)); • Memory block (variable) pointed by ptr will remain in the memory until it is explicitly freed/removed from the memory or the program execution ends.