【发布时间】:2014-01-27 22:47:05
【问题描述】:
我很好奇是否可以在 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> 我都试过了
任何想法将不胜感激。
【问题讨论】:
-
C 中没有“
string”;听起来你在混合 C 和 C++ 概念。 -
而在 C++ 中,您可以使用
#include <string>来使用std::string。但最重要的是,不要尝试随意写 C 或 C++,先给自己准备一本书。