mirror of
https://github.com/peter-tanner/CITS-3007-labs.git
synced 2024-11-30 14:20:18 +08:00
28 lines
748 B
C
28 lines
748 B
C
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#define STR_BUF_SIZE 1024
|
|
|
|
#define STR_HELPER(x) #x
|
|
#define STR(x) STR_HELPER(x)
|
|
#define CHECK_ALLOC(block) \
|
|
if (block == 0) \
|
|
{ \
|
|
printf("malloc() failed at " STR(__LINE__) ". Exited."); \
|
|
exit(-1); \
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
char *buf;
|
|
buf = malloc(STR_BUF_SIZE); // allocate a large buffer
|
|
CHECK_ALLOC(buf)
|
|
|
|
printf("type some text and hit 'return':\n");
|
|
fgets(buf, STR_BUF_SIZE, stdin); // read 1024 chars into buf
|
|
printf("\n%s\n\n", buf); // print what was entered
|
|
free(buf);
|
|
return 0;
|
|
}
|