0% found this document useful (0 votes)
4 views6 pages

CSEon Basis of Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

CSEon Basis of Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

calloc()

• Allocates multiple blocks of memory (storage).


• Stores derived data types such as array and structure.
• Each block would be of same size.
• Allocates contiguous blocks of memory.
• Initially assigns zero (0) to all bytes.
• Syntax: ptr= (cast-type*)calloc (n,size);
n-> number of blocks
size->byte size
• Example:
struct student{
char name[30];
long int ID;
int level;
};
typedef struct student info;
info *ptr;
ptr= (info*)calloc (5,sizeof (info));
calloc() (contd.)

• Always make sure that requested blocks of memory has been


allocated successfully before using the pointer in the program.
if (ptr==NULL) {
printf (“Insufficient memory. Memory not allocated”);
exit (1);
}
• Example:
main(){ for(i=1;i<=n;i++)
int *ptr,i,n=5; ptr[i]=i;
ptr=(int*)calloc(n,sizeof(int)); printf("Array elements are \n");
if (ptr==NULL) { for(i=1;i<=n;i++)
printf ("Insufficient memory. Memory not allocated"); printf("%d ",ptr[i]);
exit(1); }
}
free() & realloc()
• Frees/releases the allocated block of memory for future use.
free (ptr);
• It might happen that, the previously allocated memory is not sufficient and additional
memory space is needed.
• Also, the allocated memory space is larger than required.
• realloc() changes the size of the already allocated memory space.
• This is known as reallocation of memory.
• For example,
int *ptr;
ptr= (int *)malloc(100*sizeof(int));
ptr= (int *)realloc(ptr,85*sizeof(int));

*program 13.2
The Preprocessor
• A unique feature of C is the preprocessor.
• It is a program that processes the source code before it passes through the compiler
(preprocesses).
• It operates under the preprocessor directives.
• Preprocessor directives are placed in the program before main.
• At first, the preprocessor program checks the source code if there is any preprocessor
directives. If found, appropriate actions are taken accordingly and passes the source code
to the compiler.
• Preprocessor directives start with the symbol # and require no semicolon (;) at the end.
For example, #include and #define (Table 14.1).

• The tools of the preprocessors helps to make a program easy to read and modify, portable,
and more efficient.
The Preprocessor(contd.)
• The Preprocessor directives are divided into three categories:
Macro substitution directives
File inclusion directives
Compiler control directives

 Macro substitution directives: In this process, an identifier in the program is


replaced by a predefined string. This is done under the direction of #define.
Syntax: #define identifier string

• Simple Macro Substitution: Commonly used to define constant.


Example: #define A 80
#define AMOUNT (5*4.8)
#define if(a>b)
The Preprocessor(contd.)
• Argumented Macro Substitution: Allows more complex and useful replacements.
Syntax: #define identifier(f1,f2,…..fn ) string
Example: #define A(x) (x+x)
So the statement b= A(n); would be evaluated as b=n+n;

#define MAX(a,b) ((a>b?)a:b)


So the statement result=MAX(x,y); would be evaluated as result=((a>b?)a:b);

Example program:
#define MAX(a,b) ((a>b)?a:b)
main(){
int result,x,y;
scanf("%d%d",&x,&y);
result=MAX(x,y);
printf("Maximum number is %d ",result);
}

You might also like