Total Pageviews

Tuesday, November 25, 2025

Calloc

 //calloc


#include <stdio.h>

#include <stdlib.h> 

int main() {

    int *ptr;

    int n = 5;

    printf("Enter number of values:\n");

    scanf("%d",&n);

    

    ptr = (int *)calloc(n, sizeof(int));


    if (ptr == NULL) {

        printf("Memory allocation failed!\n");

        return 1; 

    }


   

    printf("\n");


   

    for (int i = 0; i < n; i++) {

        ptr[i] = i *10+10;

    }


    printf("After assigning values:\n");

    for (int i = 0; i < n; i++) {

        printf("%d ", ptr[i]); 

    }

    printf("\n");


   

    free(ptr);

    ptr = NULL; 


    return 0;

}


Malloc

 #include <stdio.h>

#include <stdlib.h> 

int main() {

    int *ptr; 

   ptr = (int*) malloc(sizeof(int));

    if (ptr == NULL) {

        printf("Memory allocation failed!\n");

        return 1; 

    }

    printf("Memory successfully allocated using malloc.\n");

     printf("enter a value.\n");

    scanf("%d",ptr);   

        printf("value = %d ", *ptr);    

    printf("\n");

           return 0; 

}

Malloc and free

  #include <stdio.h>

#include <stdlib.h> 

int main() {

    int *ptr; 

   ptr = (int*) malloc(sizeof(int));

    if (ptr == NULL) {

        printf("Memory allocation failed!\n");

        return 1; 

    }

    printf("Memory successfully allocated using malloc.\n");

     printf("enter a value.\n");

    scanf("%d",ptr);   

        printf("value = %d ", *ptr);    

    printf("\n");

free(ptr);

 printf("after free ptr new value = %d ", *ptr);    

           return 0; 

}

Dynamic memory allocation

1. Malloc - CLICK HERE

2.Free  - CLICK HERE 

3.Calloc  - 1. CLICK HERE   2. CLICK HERE

4.Calloc AND Free - CLICK HERE

5.Realloc - CLICK HERE