Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresSelvaraj Seerangan
After go through this ppt the learners could be able to know the c programming concepts like dynamic memory allocation, pointers and functions and pointers to structures with examples.
You will learn about linear data structures like stack and Queue. and also implementation by using array and linked list. at last, we will discuss the various applications of stack and queue and other varients of queues.
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresSelvaraj Seerangan
After go through this ppt the learners could be able to know the c programming concepts like dynamic memory allocation, pointers and functions and pointers to structures with examples.
You will learn about linear data structures like stack and Queue. and also implementation by using array and linked list. at last, we will discuss the various applications of stack and queue and other varients of queues.
interfacing matlab with embedded systemsRaghav Shetty
This Book is all about Interfacing Embedded System with Matlab. This book guides the beginners for creating GUI , Modeling with SimuLink & Interfacing Arduino , Raspberry Pi , BeagleBone with Embedded System. This Book is NOT FOR SALE , Only knowledge base for Open Source Community
It covers all the basics of MATLAB required for beginners. After going through these slides, anyone can write a MATLAB program and apply it to his field of interest.
interfacing matlab with embedded systemsRaghav Shetty
This Book is all about Interfacing Embedded System with Matlab. This book guides the beginners for creating GUI , Modeling with SimuLink & Interfacing Arduino , Raspberry Pi , BeagleBone with Embedded System. This Book is NOT FOR SALE , Only knowledge base for Open Source Community
It covers all the basics of MATLAB required for beginners. After going through these slides, anyone can write a MATLAB program and apply it to his field of interest.
While doing programming in any programming language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
The PDF contains one of the six parts to learn Verilog in the simplest possible way.
It contains notes of first three chapters of the reference book Verilog HDL by Samir Palnitkar
Data Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in CData Structures in
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end. Inserting at the end takes differential time, as sometimes the array may need to be extended. Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time.
What is std::vector in C++?
std::vector in C++ is the class template that contains the vector container and its member functions. It is defined inside the <vector> header file. The member functions of std::vector class provide various functionalities to vector containers. Some commonly used member functions are written below:
Iterators
begin() – Returns an iterator pointing to the first element in the vector
end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector
rbegin() – Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element
rend() – Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)
cbegin() – Returns a constant iterator pointing to the first element in the vector.
cend() – Returns a constant iterator pointing to the theoretical element that follows the last element in the vector.
crbegin() – Returns a constant reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element
crend() – Returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)
Capacity
size() – Returns the number of elements in the vector.
max_size() – Returns the maximum number of elements that the vector can hold.
capacity() – Returns the size of the storage space currently allocated to the vector expressed as number of elements.
resize(n) – Resizes the container so that it contains ‘n’ elements.
empty() – Returns whether the container is empty.
shrink_to_fit() – Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity.
reserve() – Requests that the vector capacity be at least enough to contain n elements.
Element access
reference operator [g] – Returns a reference to the element at position ‘g’ in the vector
at(g) – Returns a reference to the element at position ‘g’ in the vector
front() – Returns a reference to the first element in the vector
back() – Returns a reference to the last element in the vector
data() – Returns a direct pointer to the memory array used internally by the vector to store its owned elements.
Modifiers
assign() – It assigns new value to the vector elements by replacing old ones
Database structure Structures Link list and trees and Recurison complete Adnan abid
Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete
This PPT is very much useful for practitioners who are all making products and services to society. Mangers think innovatively and come up with innovative ideas. It is a 5 stage processing also called a design thinking process. The stages are empathize, define, ideate, prototype and test.
Unit 8 - Information and Communication Technology (Paper I).pdfThiyagu K
This slides describes the basic concepts of ICT, basics of Email, Emerging Technology and Digital Initiatives in Education. This presentations aligns with the UGC Paper I syllabus.
Acetabularia Information For Class 9 .docxvaibhavrinwa19
Acetabularia acetabulum is a single-celled green alga that in its vegetative state is morphologically differentiated into a basal rhizoid and an axially elongated stalk, which bears whorls of branching hairs. The single diploid nucleus resides in the rhizoid.
Macroeconomics- Movie Location
This will be used as part of your Personal Professional Portfolio once graded.
Objective:
Prepare a presentation or a paper using research, basic comparative analysis, data organization and application of economic information. You will make an informed assessment of an economic climate outside of the United States to accomplish an entertainment industry objective.
Model Attribute Check Company Auto PropertyCeline George
In Odoo, the multi-company feature allows you to manage multiple companies within a single Odoo database instance. Each company can have its own configurations while still sharing common resources such as products, customers, and suppliers.
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
http://sandymillin.wordpress.com/iateflwebinar2024
Published classroom materials form the basis of syllabuses, drive teacher professional development, and have a potentially huge influence on learners, teachers and education systems. All teachers also create their own materials, whether a few sentences on a blackboard, a highly-structured fully-realised online course, or anything in between. Despite this, the knowledge and skills needed to create effective language learning materials are rarely part of teacher training, and are mostly learnt by trial and error.
Knowledge and skills frameworks, generally called competency frameworks, for ELT teachers, trainers and managers have existed for a few years now. However, until I created one for my MA dissertation, there wasn’t one drawing together what we need to know and do to be able to effectively produce language learning materials.
This webinar will introduce you to my framework, highlighting the key competencies I identified from my research. It will also show how anybody involved in language teaching (any language, not just English!), teacher training, managing schools or developing language learning materials can benefit from using the framework.
Operation “Blue Star” is the only event in the history of Independent India where the state went into war with its own people. Even after about 40 years it is not clear if it was culmination of states anger over people of the region, a political game of power or start of dictatorial chapter in the democratic setup.
The people of Punjab felt alienated from main stream due to denial of their just demands during a long democratic struggle since independence. As it happen all over the word, it led to militant struggle with great loss of lives of military, police and civilian personnel. Killing of Indira Gandhi and massacre of innocent Sikhs in Delhi and other India cities was also associated with this movement.
"Protectable subject matters, Protection in biotechnology, Protection of othe...
C Programming : Arrays and Functions
1. C Programming : Arrays and Functions
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
2. 20CST11 – Problem Solving and Programming
2/11/2021 3.1 Arrays 2
Syllabus
3. Contents
1. Arrays
– Declaring and initializing 1D array
– Two dimensional arrays
– Multidimensional arrays
2. Functions
– Basics, The anatomy of a function
– Types of functions based on arguments and return types
– Passing 1D and 2D arrays as arguments to functions
– Calling function from another function
3. Recursive functions
4. Variable scope and lifetime
5. Storage classes.
2/11/2021 3
3.1 Arrays
4. Introduction
• Variables can’t handle voluminous data because a
hundred data items need a hundred variables to store
them.
• The solution is to use an array, which is an ordered set
of data items stored contiguously in memory.
• Each data item represents an element of the array and
is accessed with an index or subscript.
• For instance, the elements of an array named signal
are accessed as signal[0], signal[1], signal[2], and so
forth.
• The first subscript is 0 and the last subscript is one less
than the size of the array.
2/11/2021 3.1 Arrays 4
5. Introduction
• The elements of an array have a common
data type (like int, char, etc.).
• An array must be declared with its type and
size to enable the compiler to allocate a
contiguous chunk of memory for all array
elements.
2/11/2021 3.1 Arrays 5
6. Introduction
• The first element is accessed as signal[0] and the last
element is signal[31] (not 32).
• The subscript is either zero, a positive integer or an
expression that evaluates to an integer value.
• An array element can be assigned to a variable
– (say, int x = signal[2];),
– which means we can use signal[2] wherever we use x.
• By the same logic, &signal[2] is also a valid scanf
argument like &x.
• It is easy to cycle through all array elements by using
the array subscript as a key variable in a loop.
2/11/2021 3.1 Arrays 6
7. Introduction
• C doesn’t offer bounds checking for arrays, i.e. it doesn’t
validate the index used to access an array element.
• For instance, the compiler will not generate an error if you
attempt to access signal[35] even though the array has 32
elements.
• A negative index (as in signal[-5]) doesn’t generate an
error either.
• C also supports multi-dimensional arrays where an
element is handled with multiple subscripts.
• Since there is virtually no limit to the number of indexes
and dimensions an array can have.
• arrays in C are very powerful data structures that can
easily gobble up a lot of memory.
2/11/2021 3.1 Arrays 7
8. DECLARING AND INITIALIZING AN ARRAY
• The declaration of an array specifies its name, type and
size, which is normally specified as a constant or
symbolic constant.
• The following statement declares a single-dimensional
array named signal having 32 elements, each of type
int.
• The sizeof operator, when used on an array, returns the
total usage of all elements, so the below definition
allocates 32 × sizeof(int) bytes of memory—typically,
128 bytes.
2/11/2021 3.1 Arrays 8
9. Layout in Memory of the Array
• months[12] and MONTHS[12] are two
separate arrays.
2/11/2021 3.1 Arrays 9
10. Initializing During Declaration
• In the first declaration, the number of values matches
the size of the array.
• The second declaration performs a partial initialization
by assigning values to the first three elements only.
When that happens, the remaining values are
automatically initialized to zeroes (NUL for an array of
type char).
• The third declaration uses an empty pair of brackets
([ ]) to implicitly specify the size of the array.
2/11/2021 3.1 Arrays 10
24. TWO-DIMENSIONAL (2D) ARRAYS
• A two-dimensional array needs two subscripts
and a three-dimensional array needs three
subscripts.
• Like a single-dimensional array, a multi-
dimensional array occupies a contiguous region
of memory.
• A two-dimensional (2D) array, arr, takes the form
arr[i][j],
– where the subscript i represents the number of rows
and
– j represents the number of columns.
2/11/2021 3.1 Arrays 24
25. • Internally though, a 2D array can be viewed as
an array of single-dimensional arrays.
• This means that table represents a set of
three single-dimensional arrays of four
elements each.
• Alternatively, it can be considered as three
rows having four columns in each row.
2/11/2021 3.1 Arrays 25
30. MULTI-DIMENSIONAL ARRAYS
• Multi-dimensional arrays in C can go beyond two
dimensions.
• Every increase in dimension increases he number of
subscripts by one, with the subscript on the right changing
faster than the one on the left.
• A three-dimensional (3D) array can be treated as an array
of arrays of arrays.
• In this case, only those elements accessed with the right-
most subscript—with the other subscripts remaining
unchanged—occupy consecutive memory cells.
• visualization of an array becomes difficult when the
number of dimensions exceeds three.
2/11/2021 3.1 Arrays 30
39. C Programming : Functions
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
40. Contents
1. Arrays
– Declaring and initializing 1D array
– Two dimensional arrays
– Multidimensional arrays
2. Functions
– Basics, The anatomy of a function
– Types of functions based on arguments and return types
– Passing 1D and 2D arrays as arguments to functions
– Calling function from another function
3. Recursive functions
4. Variable scope and lifetime
5. Storage classes.
2/11/2021 40
3.2 Functions
41. Introduction
• C programmers decompose a complex task into
independent and manageable modules called
functions.
• A function is a statement that represents a named
body of program code.
• When it is called or invoked, the code associated with
the function is executed.
• A function can optionally
– (i) accept one or more values as arguments, and
– (ii) report the outcome of its action by returning a single
value to the caller.
• A function is called mainly in one of these two ways:
2/11/2021 3.2 Functions 41
42. Introduction
• A function is easily identified from the matched
pair of parentheses that follow its name.
• A function must be declared and defined before
it is called or invoked.
• The declaration specifies how to invoke the
function correctly.
• The definition provides the implementation, i.e. ,
the body of code that will be executed on
invocation
2/11/2021 3.2 Functions 42
43. Introduction
• The functions you create can also call other
functions.
• A function can even call itself, a property that
has important applications in the programming
world (like calculating the factorial of a number).
• For all of the functions we have used so far, we
had main as the caller.
• This is a special function that every standalone C
program must have because a program
commences execution by calling main.
2/11/2021 3.2 Functions 43
46. • The return; statement in the function has the same
significance as the one we have all along used in main.
• It terminates the function and returns control to the caller.
• However, this function doesn’t return a value (return; and
not return 0;).
• Even though a return here is not necessary, it’s good
programming practice to include it in every function even
though it may return nothing.
2/11/2021 3.2 Functions 46
47. THE ANATOMY OF A FUNCTION
• Main attributes of functions:
– Declaration
– definition and
– invocation
2/11/2021 3.2 Functions 47
48. Declaration, Prototype or Signature of Function
• A declaration is an authentic statement that tells the
compiler how the function is invoked.
• The declaration is also known as prototype or signature.
• Since it is placed before main, the compiler sees it first and
then compares it to the invocation and definition.
• For this comparison, the compiler determines whether the
function
– uses any arguments, and if so, the number of such arguments
along with their data types.
– returns a value, and if so, its data type.
• This creates four possible situations—
– with or without arguments, and
– with or without return value.
2/11/2021 3.2 Functions 48
49. Declaration, Prototype or Signature of Function
• The first void indicates that the function doesn’t
return a value.
• The second void signifies that the function uses no
arguments.
• An argument can be a variable, constant or expression
2/11/2021 3.2 Functions 49
50. • The area function accepts two arguments each of type
float and returns a value of type double.
• Since the compiler simply matches the type and
number of arguments in the declaration with those
used in the definition.
• It doesn’t need to know these variable names, and C
lets you omit them.
2/11/2021 3.2 Functions 50
51. Definition or Implementation
• The function definition or implementation specifies what the
function does when invoked.
• It comprises a header and a body where all the work gets done.
• For a function that accepts arguments and returns a value.
2/11/2021 3.2 Functions 51
• The header (the first line) is virtually identical to the declaration
except that there’s no semicolon as terminator.
• The body is represented by a simple or compound statement that is
compulsorily enclosed within curly braces.
• The return statement transmits the value of expression back to the
caller.
53. Invocation or Call
• The programmer must invoke the function in
a way that enables the program to “see” the
value.
• Typically, the return value is saved in a
variable or used as an argument to another
function.
2/11/2021 3.2 Functions 53
54. Types of functions based on arguments and
return types
1. Function Without Return Values and Without Arguments.
2. Function Without Return Values and With Arguments.
3. Function With Return Values and Without Arguments.
4. Function With Return Values and With Arguments.
2/11/2021 3.2 Functions 54
59. Parameter Passing: Arguments and Parameters
• To understand the concept of parameter passing, we need to look
at function arguments from two viewpoints—the caller and called
function.
• When the caller (here, main) invokes a function, it assigns values to
the arguments or actual arguments of the function.
• The called function accepts these values into its parameters or
formal arguments.
• When we invoke c2f as shown above, the value of celsius (the
argument) is assigned to f_celsius (the parameter).
• This transfer of value from argument to parameter is known as
parameter passing.
• argument means the “actual argument” and parameter means the
“formal argument” to a function.
2/11/2021 3.2 Functions 59
61. Passing by Value
• The question is whether arguments are passed by value or
by reference, or whether there is a mix of both.
• In C, all function arguments are passed by value, i.e., they
are copied to their corresponding parameters.
• For Example,
– When c2f is called, the value of celsius is copied to f_celsius.
– Since a parameter resides in its own memory space, celsius and
f_celsius occupy separate memory locations.
– You can’t subsequently change celsius by changing f_celsius,
i.e., you can’t change an argument by changing its copy, the
parameter.
• It is thus not necessary to maintain separate names for
arguments and parameters in the declaration, definition
and invocation
2/11/2021 3.2 Functions 61
62. Passing by Reference
• Unlike C++, C doesn’t support passing by reference, where
both argument and parameter occupy the same memory
location.
• However, in C, a variable (p) can store the memory address
of another variable (x), and you can use p to indirectly
change the value of x.
• When a function is called, its arguments are copied to the
parameters.
• But if the function changes a parameter, then the change
is not seen in the caller.
• However, if an argument contains the address of a
variable, then the corresponding parameter of the
argument can be used to change that variable.
2/11/2021 3.2 Functions 62
63. Local Variables
• A function has its own set of local variables that are defined in its
implementation.
• For instance, the c2f function uses a local variable, fheit, to compute the
result it returns to its caller.
• Unlike parameters, local variables cannot be assigned by the caller.
• parameters and local variables have the following features:
– Their names don’t conflict with identical names defined in the caller or any
other function.
– They can be accessed only in the function they are defined in.
– They are neither visible in the caller nor in any other function.
– They last as long as the function is active. This means that they are created
every time the function is called.
• since main is the first function to be called and the last to terminate, its
variables have a longer lifetime compared to variables and parameters of
other functions.
• To reinforce our point, usage of identical variable names (Ex: x, y and
temp) in both calling and called function will create a problem.
2/11/2021 3.2 Functions 63
66. USING ARRAYS IN FUNCTIONS
• Functions also use arrays as arguments.
• You can pass both an individual array element
and the name of an array as argument to a
function.
• The principles of parameter passing apply to
arrays too, but they affect array elements and
entire arrays in opposite ways.
2/11/2021 3.2 Functions 66
68. PASSING A TWO-DIMENSIONAL ARRAY AS ARGUMENT
• A function can also work with the name of a 2D array as an
argument.
• Like with a 1D array, a 2D array is specified differently in the
declaration (and definition) and invocation.
• In the former case, you may drop the first subscript (rows),
but you can’t drop the second subscript (columns).
• Without knowledge of the number of columns, it would be
impossible for the compiler to know when one row ends
and another begins, considering that the rows are placed
next to one another in memory.
2/11/2021 3.2 Functions 68
69. CALLING A FUNCTION FROM ANOTHER FUNCTION
• Just as main calls a function, a function can
also call another function.
• The called function can in turn call another
function, an
• The remaining functions then return in the
reverse sequence of their invocation.
• Ultimately, the outermost function returns
control to main.
2/11/2021 3.2 Functions 69
72. Contents
1. Arrays
– Declaring and initializing 1D array
– Two dimensional arrays
– Multidimensional arrays
2. Functions
– Basics, The anatomy of a function
– Types of functions based on arguments and return types
– Passing 1D and 2D arrays as arguments to functions
– Calling function from another function
3. Recursive functions
4. Variable scope and lifetime
5. Storage classes.
2/11/2021 72
3.2 Functions
73. RECURSIVE FUNCTIONS
• Loops use the iterative technique to repeatedly update
a set of data with the same set of instructions.
• Repetition can also be achieved by recursion, a form of
cloning that uses instructions to specify themselves.
• In C, a recursive function calls itself, which on return
delivers to its caller its share of the final solution.
• A recursive function must not be allowed to run
forever because the program will eventually run out of
memory.
2/11/2021 3.2 Functions 73
75. RECURSIVE FUNCTIONS
• Because the static variable count is defined and
initialized just once, it can track the number of calls
made to main.
• You are aware that function variables and parameters
are saved in a separate region of memory called the
stack.
• With every recursive call, the stack grows in size, and if
this growth goes unchecked, the stack will eventually
overflow.
• In this program, after main has called itself 523,172
times, stack overflow occurs and terminates the
program with a “segmentation fault.”
• This is not the way recursive functions are meant to
be used.
2/11/2021 3.2 Functions 75
76. Using a Recursive Function to Compute Factorial
• This is simply the product of all integers from 1 to
the number.
• For instance, 6!, the factorial of 6, is 6 × 5 × 4 × 3
× 2 × 1 = 720.
• But 6! can also be expressed as 6 × 5! in the same
way as 5! is expressed as 5 × 4!.
• We can thus express the factorial of a number in
this manner:
2/11/2021 3.2 Functions 76
78. VARIABLE SCOPE AND LIFETIME
• Apart from having a data type, a variable has space and time
attributes.
• The space attribute signifies the scope or visibility of the variable.
This is actually the region of the program where the variable is
visible and can be accessed.
• A variable may have one of four possible scopes: block, function,
file and program.
– A variable having block scope is visible in the block in which it is
declared.
– A variable having file scope is visible in the entire file.
• The time attribute determines the lifetime of the variable, i.e., its
time of birth and death.
• A variable declared inside a function lives as long as the function is
active. One declared before main is alive for the duration of the
program.
2/11/2021 3.2 Functions 78
79. Local and Global Variables
• Variables declared inside a function are visible
only in the function. They have local scope, the
reason why they are called local variables.
• Variables declared outside a function are known
as global variables. The scope of a global variable
extends from the point of its declaration to the
rest of the file.
• When a variable is declared before main, it is
truly global across the entire program (which
may be spread across multiple files).
2/11/2021 3.2 Functions 79
80. Scope of Global and Local Variables
2/11/2021 3.2 Functions 80
82. THE STORAGE CLASSES
• The scope, lifetime and initial value of a variable
are determined by its storage class.
• So far, we have used the default storage class for
variables (automatic), but you also need to know
the following storage classes supported by C:
– automatic (auto)
– static (static)
– external (extern)
– register (register)
• The storage class also determines the way the
compiler allocates memory for a variable.
2/11/2021 3.2 Functions 82
84. Static Variables (static)
• If a local variable is assigned the static storage
class, then it will retain its value between
function calls.
• A static variable (keyword: static) is created
and initialized just once, when the function
containing its declaration is executed.
2/11/2021 3.2 Functions 84
85. External Variables (extern)
• A global or external variable is defined outside a
function and is visible everywhere—even in other files
that constitute the program.
2/11/2021 3.2 Functions 85
86. Register Variables (register)
• All program variables are stored in primary memory which is faster
than disk but slower then registers directly connected to the CPU.
• The register storage class (keyword: register) permits a variable to
be stored in one of the high-speed CPU registers.
• The compiler will attempt to store the variable x in one of the
registers, failing which it will use primary memory.
• A register normally has a size of one word, not all variables can be
assigned this storage class.
• Generally, register is restricted to char and int variables.
• However, you can’t obtain the address of a register variable using
&x because registers don’t use the addressing system used by
primary memory.
2/11/2021 3.2 Functions 86
88. C Programming : Recursive Functions,
Variable Scope and Lifetime and
Storage Classes
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
89. Contents
1. Arrays
– Declaring and initializing 1D array
– Two dimensional arrays
– Multidimensional arrays
2. Functions
– Basics, The anatomy of a function
– Types of functions based on arguments and return types
– Passing 1D and 2D arrays as arguments to functions
– Calling function from another function
3. Recursive functions
4. Variable scope and lifetime
5. Storage classes.
2/11/2021 89
3.3 Recursive Functions
90. RECURSIVE FUNCTIONS
• Loops use the iterative technique to repeatedly update
a set of data with the same set of instructions.
• Repetition can also be achieved by recursion, a form of
cloning that uses instructions to specify themselves.
• In C, a recursive function calls itself, which on return
delivers to its caller its share of the final solution.
• A recursive function must not be allowed to run
forever because the program will eventually run out of
memory.
2/11/2021 3.3 Recursive Functions 90
92. RECURSIVE FUNCTIONS
• Because the static variable count is defined and
initialized just once, it can track the number of calls
made to main.
• You are aware that function variables and parameters
are saved in a separate region of memory called the
stack.
• With every recursive call, the stack grows in size, and if
this growth goes unchecked, the stack will eventually
overflow.
• In this program, after main has called itself 523,172
times, stack overflow occurs and terminates the
program with a “segmentation fault.”
• This is not the way recursive functions are meant to
be used.
2/11/2021 3.3 Recursive Functions 92
93. Using a Recursive Function to Compute Factorial
• This is simply the product of all integers from 1 to
the number.
• For instance, 6!, the factorial of 6, is 6 × 5 × 4 × 3
× 2 × 1 = 720.
• But 6! can also be expressed as 6 × 5! in the same
way as 5! is expressed as 5 × 4!.
• We can thus express the factorial of a number in
this manner:
2/11/2021 3.3 Recursive Functions 93
95. VARIABLE SCOPE AND LIFETIME
• Apart from having a data type, a variable has space and time
attributes.
• The space attribute signifies the scope or visibility of the variable.
This is actually the region of the program where the variable is
visible and can be accessed.
• A variable may have one of four possible scopes: block, function,
file and program.
– A variable having block scope is visible in the block in which it is
declared.
– A variable having file scope is visible in the entire file.
• The time attribute determines the lifetime of the variable, i.e., its
time of birth and death.
• A variable declared inside a function lives as long as the function is
active. One declared before main is alive for the duration of the
program.
2/11/2021 3.3 Recursive Functions 95
96. Local and Global Variables
• Variables declared inside a function are visible
only in the function. They have local scope, the
reason why they are called local variables.
• Variables declared outside a function are known
as global variables. The scope of a global variable
extends from the point of its declaration to the
rest of the file.
• When a variable is declared before main, it is
truly global across the entire program (which
may be spread across multiple files).
2/11/2021 3.3 Recursive Functions 96
97. Scope of Global and Local Variables
2/11/2021 3.3 Recursive Functions 97
99. THE STORAGE CLASSES
• The scope, lifetime and initial value of a variable
are determined by its storage class.
• So far, we have used the default storage class for
variables (automatic), but you also need to know
the following storage classes supported by C:
– automatic (auto)
– static (static)
– external (extern)
– register (register)
• The storage class also determines the way the
compiler allocates memory for a variable.
2/11/2021 3.3 Recursive Functions 99
101. Static Variables (static)
• If a local variable is assigned the static storage
class, then it will retain its value between
function calls.
• A static variable (keyword: static) is created
and initialized just once, when the function
containing its declaration is executed.
2/11/2021 3.3 Recursive Functions 101
102. External Variables (extern)
• A global or external variable is defined outside a
function and is visible everywhere—even in other files
that constitute the program.
2/11/2021 3.3 Recursive Functions 102
103. Register Variables (register)
• All program variables are stored in primary memory which is faster
than disk but slower then registers directly connected to the CPU.
• The register storage class (keyword: register) permits a variable to
be stored in one of the high-speed CPU registers.
• The compiler will attempt to store the variable x in one of the
registers, failing which it will use primary memory.
• A register normally has a size of one word, not all variables can be
assigned this storage class.
• Generally, register is restricted to char and int variables.
• However, you can’t obtain the address of a register variable using
&x because registers don’t use the addressing system used by
primary memory.
2/11/2021 3.3 Recursive Functions 103