无法使用 MINGW 执行 C 程序

unable to execute a C program, using MINGW

我无法通过mingW 开发环境执行一个简单的c 代码。此代码工作正常

#include<stdio.h>
int main(){
    char ans[5];
    printf("Enter yes or no");
    scanf("%s", ans);   
    printf("You just entered", ans);
    return 0;
}

但是每当我将ans的数据类型转换为char*然后执行由命令

创建的.exe文件时
gcc basic.c -o basic.exe

我看不到输出,它只是说 basic.exe 已停止工作。 不知道是mingW里面安装有问题还是怎么的

您应该看不到输出,程序应该会崩溃,因为 把ans改成char*还不行,需要用malloc给字符串分配一个地方:

ans=malloc(sizeof(char) * 5);

而且 printf 应该是:

printf("....%s",ans);

如果内存没有问题,请考虑以下示例:

#include<stdio.h>
#include<stdlib.h>

int main(){
    char * ans = NULL;
    // memory allocation
    ans = (char*) malloc(5 * sizeof(char));
    if( ans == NULL) // check memory
        return 1; // end of program
    printf("Enter yes or no : ");
    // reading input with length limitation
    scanf("%4s", ans);
    // string output   
    printf("You just entered %s\n", ans);
    return 0;
}

如果输入超过 4 个字符,将跳过第 5 个和其他字符(留在输入缓冲区中)。