Reputation: 71
My code is written in c. However, I converted the code via changing the header files to c++ and the program works fine. Now my question is: I want the cin and cout to work in the c program. I have included the following libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/types.h>
#include <asm/types.h>
#include <linux/socket.h>
#include <asm/socket.h>
#include <netinet/in.h>
#include <errno.h>
and for the cin and cout to work I used the iostream library in c++ which does not work in c. Kindly advise me of any library I can use or probably if I can convert the code to work in c programming.
Upvotes: 2
Views: 32131
Reputation: 5566
any library i can use or probable if i can convert the code to work in c programming
I think no such library or code conversion tools would exist. Also, I believe you probably do not want such a thing.
IMHO, C and C++ are two languages, so you will be required to keep the compilation units separate based on code type:
1a) It is (mostly true) that C functions can only be compiled in a C compilation unit.
1b) A C compilation unit can not compile or understand C++.
2a) C++ functions or methods can only be compiled in a C++ compilation unit.
2b) A C++ compilation unit CAN produce C link-able function, but as said in other answers, C and C++ do not necessarily generate identical code for the same 'c-style' function.
The issue (of 1 and 2 above) is sometimes called (or used to be called?) name mangling, specifically what the linker uses to connect the code of different compilation units. C++ uses name mangling, C does not.
3) It is trivial to call C functions (defined and compiled in a C compilation unit) from C++ code (in a C++ compilation unit) ... use 'extern "C" '. The extern "C" causes the C++ compiler to generate the function call with an unmangled link name, a name that C code can link to and invoke.
4) It is fairly easy (though possibly not trivial) to call C++ functions (defined in a C++ compilation unit) from code in a C compilation unit.
But one 'direct' approach is to provide in the C++ compilation unit a C style function interface declared with the request to not mangle the name.
4a) within the C++ code, declare and implement a C function (i.e. foo()) with the extern "C" declaration. This function, though c-style, is able to access or perform the desired C++ function.
4b) The extern "C" foo() can be invoked from the code of a C compilation unit (note: where there is no extern "C").
4c) the Linker will connect the unmangled name of foo() ... thus supporting the C code invocation of the C++ function foo().
5) It is somewhat more challenging to invoke a class method from a C function, because the C++ class method will typically require a specific instance through which to address the object. And C code knows little about class instances.
With enough pieces, it can be done. For instance, I have used a static class methods to connect interrupts (always C or assy code) to a C++ object instance. But without compelling reasons (like interrupt handling), I choose to write using C++.
Upvotes: 1
Reputation: 180415
cin
and cout
are streams and do not exist in C. You can use printf()
and scanf()
in C.
In general running C code in C++ will work as C++ is based on C but going the other way can be problematic as there are a lot of features in C++ that don't exist in C.
Upvotes: 5