-7

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

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
magd
  • 99
  • 7
  • 12
    This is not a c program. – Iharob Al Asimi Dec 19 '14 at 21:32
  • 2
    Use `g++` to compile instead of `gcc`. – Frxstrem Dec 19 '14 at 21:33
  • 2
    There is neither an iostream nor a `using` statement in C. – Scott Hunter Dec 19 '14 at 21:33
  • This question is similar to this one: http://stackoverflow.com/questions/5508110/why-is-this-program-erroneously-rejected-by-three-c-compilers?rq=1 – indiv Dec 19 '14 at 21:38
  • I am attempting to program in C from a base of C++ so for C++ concepts to leak in to this program is normal. – magd Dec 20 '14 at 17:45
  • Possible duplicate of [how to fix error: unknown type name ‘namespace’](https://stackoverflow.com/questions/13602249/how-to-fix-error-unknown-type-name-namespace) – ivan_pozdeev Aug 18 '18 at 17:13
  • 1
    "I am attempting to program in C from a base of C++ so for C++ concepts to leak in to this program is normal." - This does not make sense. Porting to human languages: you can't speak in German using English words and grammar. That's just English, even if some words are similar (learn about "false friends"). C and C++ are different languages. – too honest for this site Aug 18 '18 at 17:16

1 Answers1

3

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 namespaces 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

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97