/* gdb_test.c Jeff Ondich, 14 February 2022 Major simplifications, 17 Oct 2023 Modified by Tanya Amert for Fall 2024 A simple recursive C program to play around with in gdb. Compile like this: gcc -o gdb_test -Wall -Werror -g -Og gdb_test.c Run with one command-line argument (n to compute fact(n)): ./gdb_test 8 */ #include #include // Recursive factorial int factorial(int n) { // Base case if (n <= 1) { return 1; } // Recursive case int result = factorial(n - 1); result = n * result; return result; } // A reminder of how to use this program. void print_usage(char *command_name) { fprintf(stderr, "Usage: %s [integer]\n", command_name); } // Read in a single parameter, n, from command-line arguments // and compute and then print out fact(n) int main(int argc, char *argv[]) { // Make sure we have exactly one extra parameter if (argc != 2) { print_usage(argv[0]); return 1; } // Read in the actual value of n int n = 0; int matches = sscanf(argv[1], "%d", &n); if (matches != 1) // what's this about? { print_usage(argv[0]); return 1; } // Compute fact(n) and print the result int result = factorial(n); printf("%d! = %d\n", n, result); // Success! return 0; }