Dynamic Memory Allocation
The process of allocating memory at run time is
known as dynamic memory allocation. Although c
does not inherently have this facility there are four
library routines which allow this functions, which can
be used to allocate and free memory during the
A block mf memory may be allocated using the
function malloc. The malloc function reserves a block
of memory of specified size and returns a pointer of
type void. This means that we can assign it to any
type of pointer. It takes the following form:
ptr is a pointer of type cast-type the malloc
returns a pointer (of cast type) to an area of
memory with size byte-size
On successful execution of this statement a memory
equivalent to 100 times the area of int bytes is
reserved and the address of the first byte of
memory allocated is assigned to the pointer x of
Calloc is another memory allocation function that is
normally used to request multiple blocks of storage
each of the same size and then sets all bytes to zero.
The general form of calloc is:
calloc() allocates contiguous space for n blocks each
size of elements size bytes. All bytes are initialized to
zero and a pointer to the first byte of the allocated
region is returned. If there is not enough space a null
pointer is returned.
Compile time storage of a variable is allocated and
released by the system in accordance with its storage
class. With the dynamic runtime allocation, it is our
responsibility to release the space when it is not
ptr is a pointer that has been created by using malloc
The memory allocated by using calloc or malloc might
be insufficient or excess sometimes in both the
situations we can change the memory size already
allocated with the help of the function realloc. This
process is called reallocation of memory. The general
statement of reallocation of memory is :
Files are places where data can be stored
Some programs expect the same set of data to be
fed as input every time it is run.
Better if the data are kept in a file, and the program
reads from the file.
Programs generating large volumes of output.
Difficult to view on the screen.
Better to store them in a file for later viewing/
Basic File Operations
Opening a file
Reading data from a file
Writing data to a file
Closing a file
Opening a File
A file must be “opened” before it can be used.
fp = fopen (filename, mode);
fp is declared as a pointer to the data type FILE.
filename is a string - specifies the name of the file.
fopen returns a pointer to the file which is used in all
subsequent file operations.
mode is a string which specifies the purpose of opening
“r” :: open the file for reading only
“w” :: open the file for writing only
“a” :: open the file for appending data to it
Closing a File
After all operations on a file have been
completed, it must be closed.
Ensures that all file data stored in memory buffers are
properly written to the file.
General format: fclose (file_pointer) ;
FILE *xyz ;
xyz = fopen (“test”, “w”) ;
fclose (xyz) ;
Read/Write Operations on Files
The simplest file input-output (I/O) function are getc and
getc is used to read a character from a file and return it.
char ch; FILE *fp;
ch = getc (fp) ;
getc will return an end-of-file marker EOF, when the end of the
file has been reached.
putc is used to write a character to a file.
char ch; FILE *fp;
putc (c, fp) ;
FILE *in, *out ;
char c ;
in = fopen (“infile.dat”, “r”) ;
out = fopen (“outfile.dat”, “w”) ;
while ((c = getc (in)) != EOF)
putc (toupper (c), out);
fclose (in) ;
fclose (out) ;
Basic operations of files(Contd.)
We can also use the file versions of scanf and printf,
called fscanf and fprintf.
fscanf (file_pointer, control_string, list) ;
fprintf (file_pointer, control_string, list) ;
fscanf (fp, “%d %s %f”, &roll, dept_code, &cgpa) ;
fprintf (out, “nThe result is: %d”, xyz) ;
Command line argument
Command line arguments are parameters supplied to a
program, when the program is invoked.
How do these parameters get into the program?
Every C program has a main function.
main can take two arguments conventionally called argc and
Information regarding command line arguments are passed to
the program through argc and argv.
Six phases to execute C:
All preprocessor directives begin with #
Inclusion of other files
Definition of symbolic constants & macros
Conditional compilation of program code
Conditional compilation of preprocessor directives
#define for symbolic constants
#define identifier text
Creates symbolic constants
The “identifier” is replaced by “text” in the program
#define PI 3.14
area = PI * radius * radius;
Replaced by “area = 3.14 * radius * radius” by
preprocessor before compilation
Controls the execution of preprocessor directives
& compilation of code
Define NULL, if it hasn’t been defined yet
#define NULL 0
Use to comment out code (for comments)
code prevented from compiling