0

I've been trying to learn C, and I'm stuck on including libraries. I need to use strcpy(), but that method is included in the iostream library, but whenever I try to include the library, the program gives me errors. I've tried using "iostream", "iostream.h", , , but it either gives me a "can't find iostream.h" error, or the program exceeds 100 errors and just crashes. Even if my code is empty, I still get the same thing. Here's the code:

#include "iostream"

int main(void)
{
}

Yup, just that much makes it crash already. And here's a part of the errors I'm getting (could never paste them all here):

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(37): error C2061: syntax error : identifier 'abs'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(37): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2061: syntax error : identifier 'acos'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2061: syntax error : identifier 'asin'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'atan'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'atan2'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'ceil'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(41): error C2061: syntax error : identifier 'cos'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(41): fatal error C1003: error count exceeds 100; stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

So yeah it even exceeds the 100 errors and the program just stops counting. I don't understand why, I'm just including a regular library. Is there an equivalent of strcpy()? I mainly wanted to use it like this (for practice):

#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
#include "iostream"

int main(void)
{
    struct person 
    { 
        int id; 
        char name[50];
        int age;
    }; 

    struct person p1;

    p1.id = 5595; 
    strcpy(p1.name, "Myname");
    p1.age = 18;

    printf("%d%s%d", p1.id, p1.name, p1.age);
}
ZimZim
  • 3,291
  • 10
  • 49
  • 67
  • note there's a difference between crashing and reporting errors :] ususally compilers report errors, seldom they crash – stijn Apr 07 '12 at 21:36

3 Answers3

4

<iostream> is a C++ header (it deals with input/ouput streams, as the name implies). If you want strcpy, you need <string.h>.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

If your source file is ".c", all you have to do is to rename it ".cpp".

Then it'll compile as C++, you'll have the C++ headers, and you'll be able to use C++ streams.

I don't see any need for iostreams, however.

Strcpy and friends are in "<string.h>". Just include it, and "stdio.h" (like you're doing); delete the "iostreams" #include ... and life should be good.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

iostreams are a C++-only feature. The iostreams header file is written in C++, not C. (Yes, they are different languages!) Presumably, you're invoking the compiler in C mode, so when the compiler looks at the header file it of course throws lots of errors since many of the constructs used in iostream makes sense only in C++ mode.

If you want to use iostreams, you have to compile in C++ mode (and code in proper, modern C++ accordingly), use a different library that's C-only or work around it by implementing your own code as necessary.

In this case, all you apparently want to do is to use strcpy(). That is declared in the string.h, not iostream. (string.h is a C header file.) Just #include <string.h> and it should compile.

In silico
  • 51,091
  • 10
  • 150
  • 143