gpt4 book ai didi

c - 输入后的程序 block

转载 作者:行者123 更新时间:2023-12-01 23:42:58 25 4
gpt4 key购买 nike

这段代码紧跟在 scanf() 之后:

int main(int argc, char *argv[]) {
puts("Algorithms test kit");
long input_size;
FILE *output=fopen("output.txt","w+");
do {
printf("Enter sample size(0 goes on to next test) > ");
scanf (" %li ",&input_size);
printf ("#");
if (input_size==0) break;
int64_t *data=sorting_prepare_data(input_size);
int64_t *bsort_copy=calloc(input_size,8);
int64_t *qsort_copy=calloc(input_size,8);
memcpy(bsort_copy,data,input_size*8);
bubblesort(bsort_copy,input_size);
memcpy(qsort_copy,data,input_size*8);
quicksort(qsort_copy,input_size);
for (size_t i=0;i<input_size;i++) {
fprintf(output,"%lld\t%lld\t%lld\n",data[i],bsort_copy[i],qsort_copy[i]);
printf(".");
}
free(data); free(bsort_copy); free(qsort_copy);
} while (input_size);
return;
}

其中 bubblesort()quicksort() 是各自算法的手写实现,sorting_prepare_data() 是调用自定义的辅助函数- 在阵列上构建 PRNG。阻塞的可能原因是什么?该程序已使用 GCC 编译,没有出现错误。

最佳答案

我试过你编写代码并设法重现奇怪的行为。如果您删除 "%li" 周围的 "",它不会再阻塞。

问题是空格,因为 scanf 期望输入也匹配空格。

来自 scanf 文档:

All conversions are introduced by the % (percent sign) character. The format string may also contain other characters. White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input conversion cannot be made.

来源:http://www.manpages.info/linux/scanf.3.html

关于c - 输入后的程序 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30423095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com