Q:

C program to input and print text using Dynamic Memory Allocation.

2

C program to input and print text using Dynamic Memory Allocation.

In this program we will create memory for text string at run time using malloc() function, text string will be inputted by the user and displayed. Using free() function we will release the occupied memory.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*C program to input and print text 
using Dynamic Memory Allocation.*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    char *text;
    
    printf("Enter limit of the text: ");
    scanf("%d",&n);
    
    /*allocate memory dynamically*/
    text=(char*)malloc(n*sizeof(char));
    
    printf("Enter text: ");
    scanf(" "); /*clear input buffer*/
    gets(text);
    
    printf("Inputted text is: %s\n",text);
    
    /*Free Memory*/
    free(text);
    
    return 0;
}
    Enter limit of the text: 100
    Enter text: I am mike from California, I am computer geek.
    Inputted text is: I am mike from California, I am computer
    geek.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to create memory for int, char and float... >>
<< C program to read a one dimensional array, print s...