This program is first forked then run by execlp, it's calling program passes in two numbers, a power and a base.
int main(int argc, char *argv[])
{
int pid = getpid();
printf("Calculator Process[%d]: started\n",pid);
double base, power;
sscanf(argv[1],"%d",&base);
sscanf(argv[2],"%d",&power);
double number = pow(base,power);
printf("Calculator Process[%d]: %d ^^ %d == %d\n",pid,base,power,number);
printf("Calculator Process[%d]: exiting\n",pid);
return 1;
}
Lets say I pass into it base 3, power 5. This is what I get:
base = 4263 -- this also happens to be the PID. power = -1 raised to power: 714477568
Calling line:
execlp("./calculator","./calculator",argv[1],argv[2],(char*)0);
When I print the argvs, I get their value (as a char*, but casting fails).
Any ideas why I can't get the values to be correctly read in?
Either read double:
double base, power;
sscanf(argv[1],"%lf",&base);
sscanf(argv[2],"%lf",&power);
Or scan into integers:
int base, power;
sscanf(argv[1],"%d",&base);
sscanf(argv[2],"%d",&power);
A quick adjustment of your code should do it:
int main(int argc, char *argv[])
{
int pid = getpid();
printf("Calculator Process[%d]: started\n",pid);
double base, power;
base = atof(argv[1]);
power = atof(argv[2]);
double number = pow(base,power);
printf("Calculator Process[%d]: %f ^^ %f == %f\n",pid,base,power,number);
printf("Calculator Process[%d]: exiting\n",pid);
return 1;
}
atof()
%f
or they won't display right in your output!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.