13_malloc.c (374B)
1 /* malloc / free + snprintf into the buffer. */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 int main(void) { 8 char* buf = malloc(64); 9 if (!buf) return 1; 10 int n = snprintf(buf, 64, "alloc %d bytes", 64); 11 if (n != 14) return 2; 12 if (memcmp(buf, "alloc 64 bytes", 15) != 0) return 3; /* incl. NUL */ 13 free(buf); 14 printf("malloc ok\n"); 15 return 0; 16 }