From 5d05e6c7fac0d9c8deaa4747a4f3f9828feab493 Mon Sep 17 00:00:00 2001 From: zjpedu Date: Thu, 8 Sep 2022 16:26:13 +0800 Subject: [PATCH] add gdb crash demo --- lab1/test2.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lab1/test2.c diff --git a/lab1/test2.c b/lab1/test2.c new file mode 100644 index 0000000..092df2b --- /dev/null +++ b/lab1/test2.c @@ -0,0 +1,30 @@ +#include +#include +#include + +char * buf; + +int sum_to_n(int num) +{ + int i,sum=0; + for(i=1;i<=num;i++) + sum+=i; + return sum; +} + +void printSum() +{ + char line[10]; + printf("enter a number:\n"); + fgets(line, 10, stdin); + if(line != NULL) + strtok(line, "\n"); + sprintf(buf,"sum=%d",sum_to_n(atoi(line))); + printf("%s\n",buf); +} + +int main(void) +{ + printSum(); + return 0; +} \ No newline at end of file