Basic c program not compiling in Ubuntu trusty
Respectful greetings,
This basic C program does not compile in Ubuntu trusty 14.04.1 LTS. The compile line is gcc array.c -std=c99 (the last option for loops). Should I be using ? Is there an iostream for c (and not c++)?
#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;
}
The error message I get is
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];
^
Thanks
This is not a C program, it is a C++ program. Rename your file to array.cpp
and/or use g++
instead of gcc
, and no, there is no such thing as iostream
in C.
The first non-C part of your program is
using namespace std;
there is no namespace
s in C.
the second part is
std:cin>>array[x];
there are two things wrong with it, first of all why do using namespace std;
and then std::cin
if you use using namespace
in C++ it means that it will lookup in that namespace when omited, secondly that is also C++ specific. There is no stream operator in C.
You included stdio.h
so you have to use fgets or similar functions. And for output the printf family
下一篇: 基本的c程序不在Ubuntu中编译可靠