commit ba2838b

uint  ·  2026-06-23 12:26:08 +0000 UTC
parent 4e2f506
Add variadic die function
1 files changed,  +22, -1
M tmtm.c
M tmtm.c
+22, -1
 1@@ -1,9 +1,30 @@
 2+#include <stdarg.h>
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 
 6-int main(void)
 7+/* print line number, message, exit with error code `ec` */
 8+static void die(int ln, int ec, const char* fmt, ...);
 9+
10+static void die(int ln, int ec, const char* fmt, ...)
11+{
12+	va_list args;
13+	va_start(args, fmt);
14+
15+	fprintf(stderr, "ln:%d ", ln);
16+	vfprintf(stderr, fmt, args);
17+	fprintf(stderr, "\n");
18+
19+	va_end(args);
20+	exit(ec);
21+}
22+
23+
24+int main(int argc, char* argv[])
25 {
26 	printf("Hello tmtm!\n");
27+
28+	die(__LINE__, EXIT_SUCCESS, "died!");
29+
30 	return EXIT_SUCCESS;
31 }
32