Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Miscellaneous Topics

Dr. Mayank Swarnkar

Department of Computer Science and Engineering


Indian Institute of Technology (BHU) Varanasi
mayank.cse@iitbhu.ac.in

Week-14

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 1 / 20


Content to Cover

Command Line Arguments


Dynamic Memory Allocation
Preprocessor Directives

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 2 / 20


Command Line Arguments

Parameter supplied to a program during invoking it


E.g.:
.\a.out > output.txt
C starts to run from main function
Main function in C can take two arguments:
argc: Argument counter
argv: Array of character pointers

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 3 / 20


Activating Main Function for CLA

main (int argc, char *argv[])


{
.....
.....
.....
}

Figure: Activating Main Function for Command Line Arguments

Exercise: Enter ‘n’ numbers using CLA and find the smallest and
largest among them.

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 4 / 20


Dynamic Memory Allocation

Prevents memory wastage


Optimized programming
Used a lot in real-time applications
Functions used for memory allocation:
malloc
calloc
free
realloc

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 5 / 20


malloc
Declaration using malloc:
ptr = (cast-type *) malloc(byte-size)
Examples:
x = (int *) malloc(100*sizeof(int));
cptr = (char *) malloc(10);

cptr

Address of first byte

10 bytes of space

Figure: Dynamic Memory Allocation Example

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 6 / 20


calloc

Difference between malloc and calloc:


malloc allocates single block of storage space
calloc allocates multiple blocks of storage space
All of same size
Set all bytes to zero
Declaration using calloc:
ptr: (cast-type *) calloc (n, elem-size)
Example:
ptr = (int *) calloc(5, sizeof(int));

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 7 / 20


free

Release the used space


Free a block of memory at that point when it has no use in program
Declaration:
free (ptr);
Use free carefully
Release the pointing value not pointer itself
Do not create dangling pointers

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 8 / 20


realloc

Changes the currently allocated space


realloc can only be called after malloc or calloc
Declaration:
ptr = malloc(size)
ptr = realloc(ptr, new-size)
Example:
buffer = (char *)malloc(10*sizeof(char));
buffer = (char *)realloc(buffer, 25*sizeof(char));

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 9 / 20


Preprocessor Directives

Preprocessor: Processed before main function


Always place before main function
Usually placed with header files
Preprocessor directives are divided into three categories:
Macro substitution directives
File inclusion directives
Compiler control directives

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 10 / 20


Popular Preprocessor Directives

Directive Function
#define Defines a macro substitution
#undef Undefines a macro
#include Specifies the files to be included
#ifdef Test for a macro definition
#endif Specifies the end of #if
#ifndef Tests whether a macro is not defined
#if Tests a compile time condition
#else Specifies alternatives when #if test fails
Table: Popular Preprocessor Directives

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 11 / 20


Macro Substitution Directives

Identifier in a program is replaced by a predefined string


String can have one or more tokens
Declaration: #define identifier-string
Macro substitution is of three types:
Simple
Argumented
Nested

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 12 / 20


Macro Substitution Directives

Simple Macro Substitution:


#define PI 3.1415926
#define CAPITAL “Delhi”
#define EQUAL ==
#define INCREMENT ++
Argumented Macro Substitution:
#define identifier(f1 , f2 ...fn ) string
#define CUBE(x) (x*x*x)
#define MAX(a,b) (((a)>(b)?(a):(b)))
Nested Macro Substitution:
#define SQUARE(x) (x*x)
#define CUBE(x) (SQUARE*x)
#define TENTH(x) (CUBE*CUBE*SQUARE*SQUARE)
Undefine a macro:
#undef identifier

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 13 / 20


File Inclusion Directives

To include external file usually containing:


Functions
Macros
Declaration:
#include <filename>
#include “filename”
Exercise: Create a file which includes macro and function to find the
square root of the quadratic equation using Shreedharacharya
formula. Call this file in another program with main function. Take
quadratic equation from user and find the roots of the equation.

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 14 / 20


Compiler Control Directives

Consider the following condition:


You don’t know the availability of a particular macro in a header file
Code behaves different in multiple operating systems
Once code to fulfill requests of multiple customers
Enabling and disabling debugging (multiple print statements)
Solution:
Single comprehensive program having multiple optional codes
Skip codes based on conditions

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 15 / 20


Condition-1: Check for the Existence of a Macro

#include <mymacros.h>
#ifndef PI
#define PI 3.14
#endif

Figure: Condition-1

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 16 / 20


Condition-2: Code Portability
.....
......
main()
{
.....
.....
#ifdef MOTOROLA
{
.....
..... Code for Motorola
}
#else
{
..... Code for Intel
.....
}
#endif
.....
.....
}

Figure: Condition-2

Use #define MOTOROLA to run code for MOTOROLA


Else it will run for INTEL by default
Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 17 / 20
Condition-3: Debugging Test Condition

.....
.....
#ifdef TEST1
{
some print code
}
#endif
.....
.....
#ifdef TEST2
{
some print code
}
#endif
.....
.....

Figure: Condition-3

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 18 / 20


Other Directives and Operators

#elif
#pragma
#error
Stringizing Operator (#)
Token Pasting Operator (##)

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 19 / 20


End of Lecture

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-14 20 / 20

You might also like