Tuesday, September 23, 2014

CALLOC EXAMPLE IN CPROGRAMMING LANGUAGE

#include <stdio.h>
#include <stdlib.h>
void main()
{
    int n,i,*ptr,sum=0;
    clrscr();
    printf("Enter number of elements: ");
    scanf("%d",&n);
    ptr=(int*)calloc(n,sizeof(int));
    if(ptr==NULL)
    {
printf("Error! memory not allocated.");
exit(0);
    }
    printf("Enter elements of array: ");
    for(i=0;i<n;++i)
    {
scanf("%d",ptr+i);
sum+=*(ptr+i);
    }
    printf("Sum=%d",sum);
    free(ptr);
    getch();
}

No comments:

Post a Comment

12th January Swami Vivekanand Jayanti: National Youth Days

The day serves as a reminder to the youth of India to remain steadfast in their goals and contribute positively to society. Swami Vivekanand...