【发布时间】:2023-08-08 02:58:01
【问题描述】:
我的scanf 语句,在子进程中,不能正常工作:
int main(int argc, char **argv)
{
int operando, operatore;
pid2 = fork();
if (pid2 == 0) { // Figlio 2
printf("Inserisci due numeri: ");
scanf("%d%d", &operando, &operatore); //even though I " %d%d"...
printf("Operando is %d and operatore is %d\n", operando, operatore);
}
return 0;
}
这是输出: error
- 如何解决?
【问题讨论】:
-
在
return 0;之前添加wait(NULL);调用到父进程。 -
这里是需要
wait的原因:*.com/questions/21793755/… -
另外 - 总是检查输入函数的返回值,例如
scanf()。 -
根据“错误”链接,您没有输入两个预期的整数值,因此对
scanf()的调用失败
标签: c scanf fork system-calls