基本的c程序不是在Ubuntu中编译的可信的

Basic c program not compiling in Ubuntu trusty

本文关键字:Ubuntu 编译 程序      更新时间:2023-10-16

恭敬的问候,

这个基本的C程序不能在Ubuntu trusty 14.04.1 LTS中编译。编译行是 gcc array.c -std=c99(循环的最后一个选项(。我应该使用 ?是否有适用于 c(而不是 c++(的 iostream?

#include <stdio.h>
#include <stdlib.h>
  using namespace std;
  int main(void)
{
  int array[8];
  for(int x=0;x<8;x++)
  {
    std::cin>>array[x];
  }
  for(int x=0;x<8;x++)
  {
    std::cout<<array[x];
  }
  return 0;
}

我收到的错误消息是

array.c:3:3: error: unknown type name ‘using’
   using namespace std;
   ^
array.c:3:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘std’
   using namespace std;
                   ^
array.c: In function ‘main’:
array.c:9:9: error: expected expression before ‘:’ token
     std::cin>>array[x];
         ^
array.c:13:5: error: duplicate label ‘std’
     std::cout<<array[x];
     ^
array.c:9:5: note: previous definition of ‘std’ was here
     std::cin>>array[x];
     ^
array.c:13:9: error: expected expression before ‘:’ token
     std::cout<<array[x];
         ^

谢谢

这不是一个C程序,它是一个C++程序。将您的文件重命名为 array.cpp 和/或使用 g++ 而不是 gcc ,不,C 中没有 iostream 这样的东西。

程序的第一个非 C 部分是

using namespace std;

C 中没有namespace s。

第二部分是

std:cin>>array[x];

它有两点问题,首先为什么要using namespace std;,然后std::cin如果你在C++中使用using namespace,这意味着它在省略时会在该命名空间中查找,其次这也是C++具体的。C 中没有流运算符。

您包含了stdio.h因此您必须使用 fgets 或类似的函数。对于输出 printf 系列