Beruflich Dokumente
Kultur Dokumente
Functions
#include <stdio.h>
int fibonaci(int i)
{
if(i == 0)
{
return 0;
}
if(i == 1)
{
return 1;
}
return fibonaci(i-1) + fibonaci(i-2);
}
int main()
{
int i;
for (i = 0; i < 10; i++)
{
printf("%d\t\n", fibonaci(i));
}
return 0;
}
The Scope, Visibility and Lifetime of
variable:
• The scope of a variable is the range of
program statements that can access that
variable.
• The lifetime of a variable is the interval of
time in which storage is bound to the variable.
• A variable is visible within its scope and
invisible or hidden outside it.
Scope
• A scope in any programming is a region of the
program where a defined variable can have its
existence and beyond that variable it cannot be
accessed.
• There are three places where variables can be
declared in C programming language −
Inside a function or a block which is
called local variables.
Outside of all functions which is
called global variables.
In the definition of function parameters which are
called formal parameters.
Local and Global Variable Example
Lifetime of the Variable
The lifetime of a variable is the period of time in
which the variable is allocated a space (i.e., the
period of time for which it “lives”).
There are four lifetimes in C:
Or Storage Classes :
It is required when we declare any variable
Four Types of Classes are there:
1) Automatic
2) Register
3) Static
4) External
• Automatic : Keyword is auto, Local Variable,
RAM, Default value: garbage Value, declare in
function or main, Life time when block
execution completed.
Auto int a; incorrect
void main()
{
auto int a=10; correct
}
• Register: Keyword is Register, Local Variable, CPU
registers, Default value: garbage Value, declare in
function or main, Life time when block execution
completed. It is very less memory so we don’t
use. Its 8 to 16 kb
Register int a; incorrect
Void main()
{
Register int a=10; correct
}
Static: Fixed Memory, Cant increase or
decrease the size, Constant, Memory allocated
at run time.
int a[5];
• External or Dynamic Memory allocation: Initial
memory, at Insert and Delete of data the
memory size will increase and decrease.
• Not Constant, Memory allocated at run time.
• Char name[];
• Dynamic: In the dynamic memory allocation
user can allocate and delete the memory to
the identifiers. Some functions are available
for the dynamic memory allocation.
• Dynamic objects are stored in "the heap".
1. Malloc: for structures
2. Calloc: for arrays
3. Realloc: for increase and decrease the memory
4. Free: for deleting the memory of the variable
Visibility
• Visibility is the “accessibility” of the variable
declared. It is the result of hiding a variable in
outer scopes.
• One function local variable is invisible or not
accessible by the other function because of
not visible.
• The scope bound the visibility of variable.
Example
int main(int argc, char *argv[]) float f(float x)
{ {
... CANNOT access any variable
... CANNOT access any variable
int var1; <--------- var1 starts to exist int var3; <--------- var3 starts to exist
.. var3 accessible
.. var1 accessible
{ {
.. var3 accessible
.. var1 accessible
float var2; <--- var2 starts to exist float var4; <--- var4 starts to exist
var1 and var2 are accessible var3 and var4 are accessible
} <--- var2 ends to exist } <--- var4 ends to exist