wamp
wamp

Reputation: 5969

Is it true that all c libraries can be used in c++?

Quite a newbie question as is.

Upvotes: 10

Views: 800

Answers (5)

Allbite
Allbite

Reputation: 2487

Yes, and no.

The questionables are...

  • Compund literals

  • Native complex number datatypes

  • "restrict" keyword

  • Variadic macros

  • "long long int" datatype

Some of those features from C are included in C++0x and some are available as library extensions in many newer compilers for "normal" C++.

So it depends on what level of C you are talking about, what level of the C++ standard, and what platform of what compiler since compiler implementations always have varied support for the standards and bugs of course.

And then there are keywords used in C++ which weren't defined in C, and are therefore available to be used as variable names in C but make a C++ compiler throw up. In C it is perfectly legal to use the following words as variables or function names, but they will obviously make C++ throw a hissy fit...

  • template
  • new
  • class

Oh and "goto" behaves differently in C++ and C. In C++ "goto" cannot be used to jump over a variable's initialization, but that's ok for C. Same goes for switch statements. In C you can write a switch statement or a set of goto's which will not compile in C++.

What else? "strchr" works differently in C vs C++. In C it returns a char pointer. In C++ it returns a const char pointer. If you use that output from strchr a certain way in C, it might blow chunks in C++ because of C++'s const correctness.

Inline functions are handled differently. In C they are scoped to the file, but in C++ they have external linkage by default.

C++ code needs function prototypes defined with extern "C" to call in to a C function.

C++ mangles symbols of function names but C does not.

"In theory there is no difference between theory and practice. In practice there is." - Yogi Berra

Upvotes: 1

Niki Yoshiuchi
Niki Yoshiuchi

Reputation: 17589

I think my contrived example will show you why it isn't always possible:

#ifndef HEADER_H
#define HEADER_H
int class(int a, int b);
int private(int a);
#endif

Perfectly valid C but it won't compile in C++, even with an extern "C" block. As far as I know the only way to use a C library like that is to create another C library which calls those functions and then use that wrapper library in your C++ code.

That said, I think stumbling upon something like this in the "real world" is pretty rare.

Upvotes: 0

Stephen Canon
Stephen Canon

Reputation: 106317

If the headers are properly protected with extern "C" { ... }, then yes.

Upvotes: 2

Matt Joiner
Matt Joiner

Reputation: 118710

Yes. There is no reason you cannot use C libraries in C++. Things change if you want to compile C in a C++ compiler. The C ABI is fully supported from C++, however things are not necessarily so neat from an API perspective. Certain C additions such as restrict are not in the C++ standard, and must be dealt with carefully.

Upvotes: 6

Louis Rhys
Louis Rhys

Reputation: 35637

The answer is yes. Take a look at this:

http://www.cplusplus.com/reference/clibrary/

Upvotes: 0

Related Questions