C 编程 - 程序崩溃

标签 c printf

我有一个问题

"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/

相关文章:

C printf有点奇怪,打印完全是废话

c++ - 覆盖率问题:sprintf语句上重叠内存的拷贝(OVERLAPPING_COPY)

php - 无法使用 printf 显示图像

C 读取文件后无法写入

c - 为什么这个 C 程序可以运行? (字符类型相关查询)

c - C中的主要函数概念

c - C 中的素数 : RunTime Error

r - 如何在 gtable 内部使用解析并保留尾随零

c - 之前没有用malloc分配内存的情况下进行memcpy

c - 如何在 ELF 中分配可写/可读段?