Solution for Exercise 20-9
ex2009
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *age;
/* allocate memory */
age = (int *)malloc(sizeof(int)*1);
if(age==NULL)
{
puts("Out of Memory or something!");
exit(1);
}
/* use memory */
printf("How old are you in years? ");
scanf("%d",age);
*age *= 365;
printf("You're over %d days old!\n",*age);
/* free memory */
free(age);
return(0);
}
Output
How old are you in years? 39
You're over 14235 days old!
Notes
* As I wrote in the book, it's not necessary to use free() in this example as the program's memory, including all allocated storage, is freed when the program quits.
Copyright © 1997-2025 by QPBC.
All rights reserved
