Ok, so I have a Factorial program that works fine, outside of things not related to this problem. It takes a single command line arg and prints out the Factorial of that number. If you try entering more than one command line arg it scolds you and ends.
I have another program that uses execv() to run the Factorial program (It's actually part of a bigger program, but the only part of it that won't work is the execv() part). If I use execv("./factorial", NULL) it scolds me, as it should, and ends. But if use execv("./factorial", 10) it doesn't print anything.
Any ideas why?
Here's my simple program to get exec to run something.
#include <stdio.h>
#include <stdlib.h>
main(int argc, char* argv[])
{
execv("./factorial", 10);
}
Here's my factorial program.
#include <stdio.h>
main(int argc, char *argv[])
{
int x;
if (argc == 2){
x = atoi(argv[1]);
printf("%d\n", factorial(x));
}
else printf("Please run this program with a single integer variable. ie. factorial 10 \n");
}
int factorial(int fac){
if (fac != 1)
return fac * factorial(fac - 1);
else return 1;
}
Posts
which warned me you didn't...
2) Check the return values of system calls for errors, and use perror to get the detailed error message.
and the message told me that...
3) You are using execv incorrectly. Please read the man page for the function signature.
Here's a corrected version of the main program. The factorial program has some errors also, but enabling warnings should tell you them.