Can functions from the C standard library be used in C++?
Asked Answered
F

3

32

Right now I'm getting familiar with C and the C standard library and I wonder if my knowledge in this area will be useful when I turn to working with C++ at a later time.

Therefore I'd like to know, whether I can use the functions provided by the C standard library in a C++ setting, and also whether and why it would make sense to actually do so.

Fitting answered 16/12, 2015 at 9:3 Comment(3)
@moooeeeep: Note that you have changed the meaning of OP. OP asked for standard libraries, what is a definite yes. While after your edit it asks for C-fucntions, where the answer is "It depends on they were implemented for doing so." I would suggest to reroll that part!Dewie
@Zaibis I thought it might be more interesting to include the more general case, but you are right, the OP asked specifically about the standard library. Please review my second edit.Cashier
@moooeeeep: I'm fine with it. Hopefully Nimit is aswell :PDewie
T
48

Yes, C++ was originally designed so that any C library can be easily used in C++. Of course this is slightly less true (in particular, if a C library happens to use some C++ keyword like try or dynamic_cast, it won't work; also, if a callback coded in C++ passed to a C library is raising some exception, you are likely to have a big mess).

The standard practice to use a C header file in C++ is

 extern "C" {
 #include <some_c_header_file.h>
 };

and most existing C header files are designed to cooperate with C++ by actually containing stuff like

 #ifdef __cplusplus
 extern "C" {
 #endif

 //// most of the header material goes here, C style

 #ifdef __cplusplus
 }; // end extern "C"
 #endif

In practice, many C standard headers have equivalent C++ headers wrapping things like above (and also in namespace std). Eg C <stdio.h> is C++ <cstdio> -but you often should prefer genuine C++ streams (<iostream>), however printf-like routines are usually more localization friendly mixed with gettext(3).

However C and C++ are very different languages. You should code in idiomatic C++11 (using standard C++ containers, auto, closures, RAII, smart pointers, rule of five, SFINAE, exceptions, anonymous functions, ...)

Some standard C functions are not very useful in idiomatic C++. For example, you are unlikely to use directly malloc in genuine C++ (at least prefer new -which is still very low level and no more in the C++ spirit-, more likely use a lot the containers and the smart pointers without dealing manually with heap allocation). But POSIX functions (notably syscalls(2) ....) are quite useful in C++. longjmp is likely to be incompatible with C++ exceptions.

BTW, C++ has evolved a lot in this century. Don't learn C++98 but at least C++11 (there are tremendous differences between them) and perhaps C++14. Use a recent compiler (GCC or Clang/LLVM); in december 2015, that means GCC 5 at least or Clang/LLVM 3.7 at least. Don't forget to enable all warnings & debug info in the compiler (e.g. g++ -Wall -Wextra -g -std=c++11)

C++ (that means C++11 at least) is a difficult programming language, considerably more complex than C is. You'll need weeks of reading to learn some of it, and good coding style and discipline is essential (you can easily write very crappy code in C++). Start with Programming: Principles & Practice Using C++

I believe that if you only know C, reading SICP (and studying a bit of Scheme) before learning C++ is worthwhile.

The notion of undefined behavior is very important, both in C and probably even more in C++. You absolutely need to understand it (see C.Lattner's blog on it) and avoid it.

You will also learn a big lot by studying (and perhaps contributing to) some existing free software and its source code. Hence I recommend using Linux.

Tepee answered 16/12, 2015 at 9:8 Comment(14)
And if you do want to use a C library which has e.g. a struct field named try, you can simply #define try try_ or similar before you #include it.Syrinx
@Claudio The C++ counterparts are primarily used to insert the symbols into the std:: namespace, not to add the `extern "C" {'. At least on my machine, it works perfectly fine to include all the C standard headers directly, inserting the symbols into the global namespace.Quinta
Unfortunately, sometimes there is no C++(11) equivalent in the standard library, e.g., arithmetics with dates (https://mcmap.net/q/57590/-arithmetics-on-calendar-dates-in-c-or-c-add-n-days-to-given-date)Cashier
I wouldn't be too religious about writing idiomatic C++: Do whatever is most simple. All other concerns are of secondary importance. For me, it means that I tend to use the printf() family of functions over the C++ iostreams, because doing correct formatting with cout is a PITA, while the C version is usually quite readable and straight-forward.Quinta
I was much more thinking of containers, smart pointers than of streamsTepee
My recommendation to read SICP before learning C++11 is still a strong one, in particular if you only know CTepee
Aren't closures and anonymous functions the same?Nyberg
@Nyberg Closures capture state, functions do not. I disagree with "prefer new". If you use new in modern C++ you are doing it wrong.Dodger
@Dodger First of all, I never mentioned new. Secondly, closures do not necessarily capture state and are therefore already including capture-less lambdas (which is presumably what "anonymous functions" is meant to designate), so enumerating those seperately seems superfluous.Nyberg
@Nyberg The comment about new was meant for Basille Starynkevitch, maybe I should have made 2 comments. I agree with your point that lambdas include anonymous functions and therefore anonymous functions didn't need to be mentioned separately, but I don't see anything wrong with answering your question the way I did.Dodger
@Dodger I didn't disagree with your comment. I wasn't aware that anonymous functions were stateless. Mea culpa :-)Nyberg
@BasileStarynkevitch: I am not sure recommending Free Software in general is very useful. Most free software code is actually rather atrocious, for various reasons (age, compatibility with multiple/old compilers, contributors' inexperience in C++ itself, ...)Evensong
I would not say that. Also, free software is IMHO a good example of real world software (in particular, I don't believe the proprietary software is significantly better). And most junior programmers start working on some existing legacy software base, not in a new project ex nihiloTepee
@BasileStarynkevitch Full ACK. The proprietary code I have to cope with at work is much, much worse than any code I've seen in free software, yet...Quinta
D
20

I'll just quote a paragraph out of the ISO/IEC N3690(c++ standard).

17.2 The C standard library

1 The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety.

So simply yes!

Dewie answered 16/12, 2015 at 9:29 Comment(0)
S
0

yes .you can use standard c library functions in C++ Examples

    stdio.h   => cstdio   (printf/scanf)
    math.h    => cmath     (sqrt)  
Sobriety answered 31/10, 2017 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.