SlideShare a Scribd company logo
1 of 4
1
StaticKeywordandmeaning
Static Keyword
Static as the wordimplies are variables which exist for a certain amount of time,
much longer than that by ordinary automaticvariables.
The Many Meanings of the “Static” Keyword in C++
Private Linkage
Function Call Spanning
Per-ClassData
The two odd meanings of static:
Static is incredibly odd in the sense that it has two very different--
almost opposite-- meanings. The first meaning applies when it is used
with functions, and the second when it is used with variables. Let's take
a closer look.
The first meaning static functions:
In order totest staticfunctions you first need tobe able to compile multiple
source files-- I assumeyou havethe ability toproduce twodifferent sourcefiles
and compile them. With that addressed-- let's continue!
Static functions are much like private methods in Javaor C++. A privatemethod
is a methodwhich is only used by a class andcan't be used outsideof it. In C, we
can declare a staticfunction. A staticfunction is a function which can only be
usedwithin the sourcefile it is declared in.
First, I will give you a working example. There are twofiles-- main.cwhich is our
2
StaticKeywordandmeaning
main program. Ourmain program will call our custom mini-library called
randomnum. Wedesigned ourrandomnum library to consist of 2 functions. One
is a public function called getRandomNum which returns arandom number
between 1-1000. Thesecondfunction in the library is aprivate function called
seedRandom. Anyone that has usedrand in C knows you need toseed rand. In
this case wemake seed a private function sotheoutsidesource files can't mess
with it. This is an exampleof encapsulation-- wehide the details of our library.
1 #include <stdio.h>
2 #include "randomnum.h"
3
4 int main(void)
5 {
6 printf("Random Number - %dn", getRandomNum());
7 return 0;
8 }
The second meaning-- static variables:
The second meaning is when static is used with a variable. This has
nothing to do with static in the function sense. Static when used with a
variable means the storage will be allocated at compile time and
remain intact for the entire cycle of the program's execution. You may
be thinking, "Isn't a global variable the same thing?" and you are right.
A global variable uses static storage automatically. You probably hear
people saying, "You should avoid global variables at all cost!" and they
are also right. Global variables violate encapsulation because they
don't hide data-- they simply give it to everyone. That is generally a
3
StaticKeywordandmeaning
bad style to program with.
I won't go into using global variables because it's something that
should be avoided. I will show a snippet of how you could use static
inside a function that returns a variable.
#include <stdio.h>
02
03 char* getBob(void)
04 {
05 static char bob[3] = "bob";
06 return bob;
07
}
08
09 int main(void)
10 {
11 char *name = getBob();
12 printf("%sn", name);
13 return 0;
14 }
Why declare bob as static? Because bob was declared inside a function.
A function has auto (automatic) storage. That means when the
function returns those variables are popped off the stack and never to
be seen again (can be over wrote by new variables popped on the
stack). If you used this code without static it may or may not display
the correct name in this simple case. It's possible the memory would
still be intact and display the name, but the behaviour is actually
4
StaticKeywordandmeaning
undefined if you don't include static. Static makes sure the variable is
not automatic-- instead it is allocated for the remainder of the
program.
To conclude static variables-- for the most part they are not needed
(like in the global variable case). When declaring a global variable static
storage is automatically assigned and using the static keyword is pretty
much pointless. When using static in other contexts it may be useful if
you need to return data that must remain intact for the remainder of
the program. Think about it carefully before deciding to use it!
Static: The Multipurpose Keyword
Writing about the const keywordbrings meto asubtlebut important distinction
between const and readonly keywords in C#: const variables are implicitly static
and they need to be defined when declared. readonly variables are not implicitly
staticand can only be initialized once.
E.g.: You are writing a car racing program in which the racing track has a fixed
length of 100 Km. You can define a const variable todenote this as
private const int trackLength = 100;
Static class declarations
Also, the static keyword in C++ is used to specifythat variables
will be in memory till the time the program ends; and initialized
onlyonce. Just likeC# and Java, these variablesdon’t need an
object to be declaredto use them. Pleasesee this linkfor the
use of the static keyword in C++:

More Related Content

What's hot

Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C ProgrammingSonya Akter Rupa
 
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Philip Schwarz
 
Spf Chapter4 Variables
Spf Chapter4 VariablesSpf Chapter4 Variables
Spf Chapter4 VariablesHock Leng PUAH
 
Dotnet programming concepts difference faqs- 3
Dotnet programming concepts difference faqs- 3Dotnet programming concepts difference faqs- 3
Dotnet programming concepts difference faqs- 3Umar Ali
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to javaSadhanaParameswaran
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case StatementsDipesh Pandey
 
CSIS 138 JavaScript Class2
CSIS 138 JavaScript Class2CSIS 138 JavaScript Class2
CSIS 138 JavaScript Class2Teresa Pelkie
 
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...Philip Schwarz
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in javaanshu_atri
 
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Philip Schwarz
 
Python Training in Bangalore | Python Introduction Session | Learnbay
Python Training in Bangalore |  Python Introduction Session | LearnbayPython Training in Bangalore |  Python Introduction Session | Learnbay
Python Training in Bangalore | Python Introduction Session | LearnbayLearnbayin
 
Mark Seemann smashing together two repos using monoidal mappend
Mark Seemann smashing together two repos using monoidal mappendMark Seemann smashing together two repos using monoidal mappend
Mark Seemann smashing together two repos using monoidal mappendPhilip Schwarz
 
Learn To Code: Diving deep into java
Learn To Code: Diving deep into javaLearn To Code: Diving deep into java
Learn To Code: Diving deep into javaSadhanaParameswaran
 

What's hot (17)

Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
 
Spf Chapter4 Variables
Spf Chapter4 VariablesSpf Chapter4 Variables
Spf Chapter4 Variables
 
Dotnet programming concepts difference faqs- 3
Dotnet programming concepts difference faqs- 3Dotnet programming concepts difference faqs- 3
Dotnet programming concepts difference faqs- 3
 
Ruby Style Guide
Ruby Style GuideRuby Style Guide
Ruby Style Guide
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case Statements
 
CSIS 138 JavaScript Class2
CSIS 138 JavaScript Class2CSIS 138 JavaScript Class2
CSIS 138 JavaScript Class2
 
Switch case in C++
Switch case in C++Switch case in C++
Switch case in C++
 
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in java
 
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
 
Python Training in Bangalore | Python Introduction Session | Learnbay
Python Training in Bangalore |  Python Introduction Session | LearnbayPython Training in Bangalore |  Python Introduction Session | Learnbay
Python Training in Bangalore | Python Introduction Session | Learnbay
 
C# Generics
C# GenericsC# Generics
C# Generics
 
Mark Seemann smashing together two repos using monoidal mappend
Mark Seemann smashing together two repos using monoidal mappendMark Seemann smashing together two repos using monoidal mappend
Mark Seemann smashing together two repos using monoidal mappend
 
Learn To Code: Diving deep into java
Learn To Code: Diving deep into javaLearn To Code: Diving deep into java
Learn To Code: Diving deep into java
 

Similar to Static keyword a.z

Static Keyword Static is a keyword in C++ used to give special chara.pdf
  Static Keyword Static is a keyword in C++ used to give special chara.pdf  Static Keyword Static is a keyword in C++ used to give special chara.pdf
Static Keyword Static is a keyword in C++ used to give special chara.pdfKUNALHARCHANDANI1
 
EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl Lyndon White
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++Mohamed Essam
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010Rich Helton
 
If I Had a Hammer...
If I Had a Hammer...If I Had a Hammer...
If I Had a Hammer...Kevlin Henney
 
solidity programming solidity programming
solidity programming solidity programmingsolidity programming solidity programming
solidity programming solidity programmingMohan Kumar Ch
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
The Rest of the Best
The Rest of the BestThe Rest of the Best
The Rest of the BestKevlin Henney
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with GroovyDhaval Dalal
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practicesAnilkumar Patil
 
Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1LK394
 
why c++11?
why c++11?why c++11?
why c++11?idrajeev
 
Library management system
Library management systemLibrary management system
Library management systemSHARDA SHARAN
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1ReKruiTIn.com
 
Javascript design patterns
Javascript design patternsJavascript design patterns
Javascript design patternsGomathiNayagam S
 

Similar to Static keyword a.z (20)

Static Keyword Static is a keyword in C++ used to give special chara.pdf
  Static Keyword Static is a keyword in C++ used to give special chara.pdf  Static Keyword Static is a keyword in C++ used to give special chara.pdf
Static Keyword Static is a keyword in C++ used to give special chara.pdf
 
EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010
 
If I Had a Hammer...
If I Had a Hammer...If I Had a Hammer...
If I Had a Hammer...
 
Making an Exception
Making an ExceptionMaking an Exception
Making an Exception
 
solidity programming solidity programming
solidity programming solidity programmingsolidity programming solidity programming
solidity programming solidity programming
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
The Rest of the Best
The Rest of the BestThe Rest of the Best
The Rest of the Best
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practices
 
What is c language
What is c languageWhat is c language
What is c language
 
Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1
 
Java performance
Java performanceJava performance
Java performance
 
why c++11?
why c++11?why c++11?
why c++11?
 
Library management system
Library management systemLibrary management system
Library management system
 
java tr.docx
java tr.docxjava tr.docx
java tr.docx
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
C# features
C# featuresC# features
C# features
 
Javascript design patterns
Javascript design patternsJavascript design patterns
Javascript design patterns
 

More from Syed Umair

Assignement code
Assignement codeAssignement code
Assignement codeSyed Umair
 
New microsoft office word document
New microsoft office word documentNew microsoft office word document
New microsoft office word documentSyed Umair
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)Syed Umair
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solvingSyed Umair
 
Assignement of discrete mathematics
Assignement of discrete mathematicsAssignement of discrete mathematics
Assignement of discrete mathematicsSyed Umair
 
Assignment c++12
Assignment c++12Assignment c++12
Assignment c++12Syed Umair
 
Assignement of discrete mathematics
Assignement of discrete mathematicsAssignement of discrete mathematics
Assignement of discrete mathematicsSyed Umair
 
Assignement c++
Assignement c++Assignement c++
Assignement c++Syed Umair
 

More from Syed Umair (20)

Assignement code
Assignement codeAssignement code
Assignement code
 
Tree 4
Tree 4Tree 4
Tree 4
 
Title page
Title pageTitle page
Title page
 
S.k
S.kS.k
S.k
 
Q.a
Q.aQ.a
Q.a
 
Prog
ProgProg
Prog
 
Perception
PerceptionPerception
Perception
 
New microsoft office word document
New microsoft office word documentNew microsoft office word document
New microsoft office word document
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
M.b
M.bM.b
M.b
 
C++ 4
C++ 4C++ 4
C++ 4
 
B.g
B.gB.g
B.g
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solving
 
Assignement of discrete mathematics
Assignement of discrete mathematicsAssignement of discrete mathematics
Assignement of discrete mathematics
 
A.i
A.iA.i
A.i
 
H m
H mH m
H m
 
Prog
ProgProg
Prog
 
Assignment c++12
Assignment c++12Assignment c++12
Assignment c++12
 
Assignement of discrete mathematics
Assignement of discrete mathematicsAssignement of discrete mathematics
Assignement of discrete mathematics
 
Assignement c++
Assignement c++Assignement c++
Assignement c++
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
_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
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
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
 
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
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
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
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
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
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
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
 

Recently uploaded (20)

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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
_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
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
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
 
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
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
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
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).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
 

Static keyword a.z

  • 1. 1 StaticKeywordandmeaning Static Keyword Static as the wordimplies are variables which exist for a certain amount of time, much longer than that by ordinary automaticvariables. The Many Meanings of the “Static” Keyword in C++ Private Linkage Function Call Spanning Per-ClassData The two odd meanings of static: Static is incredibly odd in the sense that it has two very different-- almost opposite-- meanings. The first meaning applies when it is used with functions, and the second when it is used with variables. Let's take a closer look. The first meaning static functions: In order totest staticfunctions you first need tobe able to compile multiple source files-- I assumeyou havethe ability toproduce twodifferent sourcefiles and compile them. With that addressed-- let's continue! Static functions are much like private methods in Javaor C++. A privatemethod is a methodwhich is only used by a class andcan't be used outsideof it. In C, we can declare a staticfunction. A staticfunction is a function which can only be usedwithin the sourcefile it is declared in. First, I will give you a working example. There are twofiles-- main.cwhich is our
  • 2. 2 StaticKeywordandmeaning main program. Ourmain program will call our custom mini-library called randomnum. Wedesigned ourrandomnum library to consist of 2 functions. One is a public function called getRandomNum which returns arandom number between 1-1000. Thesecondfunction in the library is aprivate function called seedRandom. Anyone that has usedrand in C knows you need toseed rand. In this case wemake seed a private function sotheoutsidesource files can't mess with it. This is an exampleof encapsulation-- wehide the details of our library. 1 #include <stdio.h> 2 #include "randomnum.h" 3 4 int main(void) 5 { 6 printf("Random Number - %dn", getRandomNum()); 7 return 0; 8 } The second meaning-- static variables: The second meaning is when static is used with a variable. This has nothing to do with static in the function sense. Static when used with a variable means the storage will be allocated at compile time and remain intact for the entire cycle of the program's execution. You may be thinking, "Isn't a global variable the same thing?" and you are right. A global variable uses static storage automatically. You probably hear people saying, "You should avoid global variables at all cost!" and they are also right. Global variables violate encapsulation because they don't hide data-- they simply give it to everyone. That is generally a
  • 3. 3 StaticKeywordandmeaning bad style to program with. I won't go into using global variables because it's something that should be avoided. I will show a snippet of how you could use static inside a function that returns a variable. #include <stdio.h> 02 03 char* getBob(void) 04 { 05 static char bob[3] = "bob"; 06 return bob; 07 } 08 09 int main(void) 10 { 11 char *name = getBob(); 12 printf("%sn", name); 13 return 0; 14 } Why declare bob as static? Because bob was declared inside a function. A function has auto (automatic) storage. That means when the function returns those variables are popped off the stack and never to be seen again (can be over wrote by new variables popped on the stack). If you used this code without static it may or may not display the correct name in this simple case. It's possible the memory would still be intact and display the name, but the behaviour is actually
  • 4. 4 StaticKeywordandmeaning undefined if you don't include static. Static makes sure the variable is not automatic-- instead it is allocated for the remainder of the program. To conclude static variables-- for the most part they are not needed (like in the global variable case). When declaring a global variable static storage is automatically assigned and using the static keyword is pretty much pointless. When using static in other contexts it may be useful if you need to return data that must remain intact for the remainder of the program. Think about it carefully before deciding to use it! Static: The Multipurpose Keyword Writing about the const keywordbrings meto asubtlebut important distinction between const and readonly keywords in C#: const variables are implicitly static and they need to be defined when declared. readonly variables are not implicitly staticand can only be initialized once. E.g.: You are writing a car racing program in which the racing track has a fixed length of 100 Km. You can define a const variable todenote this as private const int trackLength = 100; Static class declarations Also, the static keyword in C++ is used to specifythat variables will be in memory till the time the program ends; and initialized onlyonce. Just likeC# and Java, these variablesdon’t need an object to be declaredto use them. Pleasesee this linkfor the use of the static keyword in C++: