c语言字符串的输入输出

#include<stdio.h>
#include<string.h>
int main()
{
    char *str;
    gets(str);
    int len=strlen(str);
    for(int i=0;i<len;i++)
    puts(str);
}

为什么把第7,8行删去后不能执行puts了?

不删除后也不能执行。因为 gets 函数读取输入到 str 指向的内存,但在这之前需要为 str 分配足够的内存来存储输入的字符串。
puts 是输出 str 的内存,不需要循环。如果是循环单个字符输出,考虑使用 printf("%c", *str++);
以下为修改后的代码,有两次输出:

#include<stdio.h>
#include<string.h>
#include <malloc.h>

int main()
{
    char *str = malloc(sizeof(char)*100);
    gets(str);
    puts(str);
    int len=strlen(str);
    for(int i=0;i<len;i++)
        printf("%c", *str++);
}