当我使用printf()
函数,然后scanf()
我遇到了麻烦。 我在代码的下面写道,eclipse编译器说没有错误也没有警告,但是当我编译它时,它显示:insert array dimension insert array elements
。它不会让我插入任何东西,程序结束。 我也尝试添加fflush(stdout)
但没有任何更改。 我能做什么?printf scanf函数和fflush()
int main(void) {
int *V;
int i;
int N;
printf("insert array dimension");
fflush(stdout);
scanf("%n", &N);
V=(int*)malloc(sizeof(int)*N);
if(V==NULL){
printf("\n error");
fflush(stdout);
return 0;
}
printf("\ninsert array elements");
fflush(stdout);
return EXIT_SUCCESS;
}
为什么你用'%n'指定符来表示'int'? – haccks
不要将'malloc'和朋友的结果放在C中,并且总是用'free'分配内存。 – Olaf
'scanf'被认为是邪恶的,因为它执行两项任务,并在出错时将输入流保留在未知状态。如果你想确保你知道有多少输入被读取,使用'fgets()'和'sscanf()'。 – msw