我需要在屏幕上打印一行,然后获取用户输入,但这些printf("blah")语句导致我的代码无法编译。错误消息说'char not expected'但是当我注释掉这些printf()语句时,代码就会编译。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Welcome to the shell!\n");
printf("shell:>");
char* inp = (char*)malloc(20); // error at this line
}
Run Code Online (Sandbox Code Playgroud)
我在 MINIX 3.1.0 中使用 cc 编译器
MINIX C 编译器不遵循现代标准,这意味着局部变量只能在函数开始时声明。
你需要做例如
char *inp;
printf("Welcome to the shell!\n");
printf("shell:>");
inp = malloc(20);
Run Code Online (Sandbox Code Playgroud)
当我说“现代”时,我指的是 C99 标准。MINIX 编译器似乎遵循的旧 C89 标准,以及直到最近的 Visual Studio C 编译器(在 VS2013 及更高版本之前不支持大部分 C99),只允许在块的开头进行声明。