我很好奇我是否可以在 Mac 终端中编写 C 程序。似乎是的,但是当我开始尝试使用字符串时,编译时出现错误。
#include <stdio.h>
#include <string.h>
int main(void) {
string s = "chris";
printf("hello %s \n", s);
}
当我编译它时,我收到一条消息说 use of undeclared identifier 'string' - string s = "chris";
我尝试添加 using namespace std;
但是说using
未定义。我都试过了 #include <string>
和 #include <string.h>
如有任何想法,我们将不胜感激。
最佳答案
string
是一个标准的 C++ 库类。使用 const char *
代替:
#include <stdio.h>
int main(int argc, const char **argv) {
const char *s = "chris";
printf("hello %s \n", s);
return 0;
}
关于c - 在 Mac 终端中编写基本的 "C"程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21393322/