write a c program to allocate dynamic memory for one string scan it print it

16 Dec 2022 Balmiki Mandal 0 C Programming

"Write a C program to allocate dynamic memory for one string, scan it, and print it" means to write a program in the C programming language that performs the following tasks:

  1. Dynamically allocate memory for a string: This means that the program should request memory space from the operating system at runtime to store a string.

  2. Scan the string: This means that the program should prompt the user to input a string and store it in the memory space allocated in step 1.

  3. Print the string: This means that the program should retrieve the string stored in the memory space allocated in step 1 and print it to the console.

Overall, this program should demonstrate how to allocate dynamic memory in C, how to scan user input for a string, and how to print the string to the console.

C program to allocate dynamic memory for one string scan & print it

#include<stdio.h>
#include<stdlib.h>
void main()
{
    char *p;
    p=malloc(sizeof(char)*10);
    printf("eneter the string:\n");
    scanf("%s",p);
    printf("p=%s\n",p);
}

Output: eneter the string: Balmiki

p=Balmiki

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.