Professional Documents
Culture Documents
USP - 12 - With Storage Classes
USP - 12 - With Storage Classes
(CSE 3041)
SOA
Deemded to be University
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 1 / 32
Personal Libraries
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 2 / 32
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 3 / 32
Header Files
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 4 / 32
Implementation files
The header file describes what the functions of the library do;
The implementation file shows how a function does it.
A library’s implementation file is a .c file
I the code of all the library functions and
I any other information needed for compilation of these functions.
Elements of an implementation file
I a block comment summarizing the library’s purpose
I #include directives for this library’s header file and for other libraries
used by the functions in this library
I #define directives naming constant macros used only inside this library
I type definitions used only inside this library
I function definitions including the usual comments
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 5 / 32
Using a Personal Library
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 6 / 32
Compile and use
$ gcc -c planet equal.c -o planet equal.out
$ gcc -c scan planet.c -o scan planet.out
$ gcc -c print planet.c -o print planet.out
$ gcc -o main all.out planet main.c planet equal.out
scan planet.out print planet.out
Static library
$ gcc -c planet equal.c -o planet equal.out
$ gcc -c scan planet.c -o scan planet.out
$ gcc -c print planet.c -o print planet.out
$ ar r libPO.a planet equal.out scan planet.out print planet.out
$ gcc -o planet with lib.out planet main.c -L./ -lPO
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 7 / 32
Pointer notations
Consider the declaration:
int i = 3 ;
Storage classes
To fully define a variable one needs to mention not only its
‘type’ but also its ‘storage class’.
Why now ? variables have certain default storage classes.
Default: the compiler will assume a storage class depending on
the context in which the variable is used.
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 9 / 32
Storage classes
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 10 / 32
Identifier
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 11 / 32
Linkage class
Identifiers declared more than once may refer to the same object be-
cause of linkage.
Each identifier has a linkage class of external, internal or none.
External: Declarations in a program of a particular identifier with ex-
ternal linkage refer to the same entity.
Internal: Declarations in a file of a particular identifier with internal
linkage represent the same entity.
No linkage: Each declaration of an identifier with no linkage represents
a unique entity.
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 12 / 32
Automatic storage class
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 13 / 32
Register storage class
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 14 / 32
Static storage class
Static storage class
Figure: Program 2
Figure: Program 1
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 16 / 32
Use of Static Variables
Define two functions with name bubbleSort and onePass in another file named bubbleSort.c .
main in BSmain.c should call bubbleSort function to sort the elements in the input array.
Function bubbleSort should call the function onePass to perform one pass over the unsorted input.
Ensure that onePass cannot be called directly from the main in BSmain.c .
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 17 / 32
Static storage class
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 18 / 32
Using static: strtok() library function in C
#include <string.h>
char *strtok(char *str, const char *delim);
Given
CSE:ITER:SOADU:::::BBS:OR:IND::ASIA::::::EARTH
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 20 / 32
Library Function: strspn()
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 21 / 32
Example: strspn()
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 22 / 32
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 23 / 32
Immature use of static variables in strtok implementation
Thread-unsafe behavior
The behavior that causes the above code to fail also prevents strtok from being
used safely in programs with multiple threads.
If one thread is in the process of using strtok and a second thread calls strtok ,
subsequent calls may not behave properly.
A thread-safe function called strtok r should be used in place of strtok. The
r stands for reentrant, indicating the function can be reentered (called again)
before a previous call finishes.
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 24 / 32
C Library Function strtok r()
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 25 / 32
Using strtok r in place of strtok
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 26 / 32
A practical example: The wc utility in UNIX
Programming problem
Write a program to determine the average number of words per
line in a command-line argument.
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 27 / 32
Static: AFFECTS STORAGE OR LINKAGE DEPENDING
ON THE CONTEXT.
The static qualifier can affect either the storage class or linkage class of
an object depending on the context.
An identifier representing a function has external linkage by default.
This means that it can be referenced in any file of the program.
When static is applied to a function, it always changes its linkage
class from the default of external to internal.
Functions do not have a storage duration.
For variables declared inside a block, the linkage class is always none
and static changes the storage class from automatic to static.
For variable declared outside any block, the storage class is always static
and the static specifier changes the linkage class from external to
internal.
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 28 / 32
External storage class
Notifies the compiler that such a function/variable exists and that the
linker will know where to find it.
To reference a top-level variable in the region of its source file that
precedes its declaration or in another source file.
The names of the functions themselves are of storage class extern ,
meaning that they will be available to the linker.
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 29 / 32
External storage class
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 30 / 32
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 31 / 32
(Ashish Kumar Gupta, Ph.D.) Chapter-12, PIL, C programming December 24, 2021 32 / 32