将int值返回到另一个funciton
我是C的完整初学者,很抱歉提出一个琐碎的问题。 问:我想将在number_generator函数中计算的Random_num变量的值返回到主函数:
int main() //didn't paste headers and prototype
{
int random_num; // <= to here
number_generator(random_num);
printf("random number: %d", random_num);
return 0;
}
int number_generator(int random_num)
{
srand(time (0));
random_num = (rand() % 100) + 0; // <= return value from here
}
当前,printf的终端输出为0,而应在0到100范围内。
I am a complete beginner in C and I'm sorry in advance for a trivial question.
Q: I would like to return the value of random_num variable that is calculated in number_generator function to the main function:
int main() //didn't paste headers and prototype
{
int random_num; // <= to here
number_generator(random_num);
printf("random number: %d", random_num);
return 0;
}
int number_generator(int random_num)
{
srand(time (0));
random_num = (rand() % 100) + 0; // <= return value from here
}
Currently, the terminal output of printf is 0 while it should be in a range from 0 to 100.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样做以下方法
是另一种方法是
要注意在此表达式中添加0的方法
是没有意义的。相当于
Do it the following way
Another approach is the following
Pay attention to that adding 0 in this expression
does not make a sense. It is equivalent to