我有一个问题
"Write a C program that accepts as input a single integer k, then writes a pattern consisting of a single 1 on the first line, two 2s on the second line, three 3s on the third line, and so forth, until it writes k occurrences of k on the last line."
例如,如果输入是5
,输出应该是这样的:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
我写了下面的代码来实现这个。
#include <stdio.h>
#include <stdlib.h>
int main() {
int k = 5;
int i;
int j;
for(i = 1; i<=k; i++){
for(j = 1; j<=i; j++){
printf('%d',i);
}
printf('\n');
}
return 0;
}
当我运行这个程序时,Eclipse 崩溃了。我编写的代码中是否遗漏了什么?
最佳答案
printf('%d',i);
应该是
printf("%d",i);
printf()
的第一个参数需要 const char *
并且您拥有的是它的一个字符。在您继续并发生崩溃之前,编译器应该对此发出警告。不要忽视警告!!
关于C 编程 - 程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466675/