Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Exercises:

int main()

char *name;

int limit;

limit=30;

//allocate memory dynamically

name=(char*)malloc(limit*sizeof(char));

printf("Enter name: ");

scanf("%[^\n]s",name);

//gets(name);

printf("Hi! %s, How are you?\n",name);

//free dynamically allocated memory

free(name); // <-- Hey, dont forget.

return 0;

=========================================================

Ex:
#include <stdio.h>

#include<stdlib.h>

void main()

int n, *arr,i;

printf("enter the size ");

scanf("%d",&n);

arr=(int*)malloc(n*sizeof(int));

if(arr==NULL)

printf("out of memory….\n");

else

printf("Enter the elements :\n");

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

scanf("%d",&*(arr+i) );

printf("the elements are:\n");

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

{
printf("%d",*(arr+i) );

=====================================================

Ex:

#include <stdio.h>

#include<stdlib.h>

struct students

int roll_no;

char name[20];

float percentage;

};

void main()

struct students* s;

s=(struct students*) calloc(2,sizeof(struct students));

if(s==NULL)

printf("out of memory….\n");

else
{

printf("Enter students details.............................\n");

printf("Enter students roll_no\n");

scanf("%d",&s->roll_no );

printf("Enter students name\n");

scanf("%s" ,s->name );

printf("Enter students percentage");

scanf("%f", &s->percentage );

printf("*************************the students details are********************");

printf("%d,%s,%f", s->roll_no,s->name,s->percentage);

You might also like