Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我认为 usingfgets是最好的选择,但显然它需要来自标准输入的输入?如何使用 生成随机数rand()并存储它?
fgets
rand()
您可以使用sprintf():
sprintf()
char buf[11]; int r = rand(); sprintf(buf, "%d", r);
snprintf()如果您的运行时库提供它,则使用更强大的选项:
snprintf()
snprintf(buf, sizeof(buf), "%d", r);
以snprintf()这种方式使用可确保生成的字符串不会溢出buf.
buf