c - 在 Mac 终端中编写基本的 "C"程序

标签 c macos terminal

我很好奇我是否可以在 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/

相关文章:

linux - 在 bash 中用另一个词替换一个词

C中的构造函数和析构函数

调用 printf 更改地址 C 传递的数组

ios - 多个对象上的选择器

c# - 我在哪里可以下载 MonoMac 的编译版本?

bash - 冒号在 PATH 中的作用

python - 如何在终端中切换 Python 版本?

C - 使 msgrcv 与信号共存

c - C语言读写大文件的有效方法

ios - 如何使用 XCode 5 和 iOS7 在 Mountain Lion 上安装 Boost 库