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.

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
Nimit Bhardwaj
  • 827
  • 9
  • 19
  • 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! – dhein Dec 16 '15 at 09:33
  • 1
    @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. – moooeeeep Dec 16 '15 at 09:41
  • @moooeeeep: I'm fine with it. Hopefully Nimit is aswell :P – dhein Dec 16 '15 at 09:47

3 Answers3

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    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. – John Zwinck Dec 16 '15 at 09:09
  • 3
    @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. – cmaster - reinstate monica Dec 16 '15 at 09:13
  • Unfortunately, sometimes there is no C++(11) equivalent in the standard library, e.g., arithmetics with dates (http://stackoverflow.com/a/15669430/1025391) – moooeeeep Dec 16 '15 at 09:16
  • 10
    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. – cmaster - reinstate monica Dec 16 '15 at 09:18
  • 2
    I was much more thinking of containers, smart pointers than of streams – Basile Starynkevitch Dec 16 '15 at 09:21
  • My recommendation to read SICP before learning C++11 is still a strong one, in particular if you only know C – Basile Starynkevitch Dec 16 '15 at 09:28
  • Aren't closures and anonymous functions the same? – Columbo Dec 16 '15 at 11:51
  • @Columbo Closures capture state, functions do not. I disagree with "prefer `new`". If you use `new` in modern C++ you are doing it wrong. – nwp Dec 16 '15 at 13:37
  • @nwp 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. – Columbo Dec 16 '15 at 13:40
  • @Columbo 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. – nwp Dec 16 '15 at 13:49
  • @nwp I didn't disagree with your comment. I wasn't aware that anonymous functions were stateless. Mea culpa :-) – Columbo Dec 16 '15 at 13:50
  • 3
    @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, ...) – Matthieu M. Dec 16 '15 at 15:40
  • 1
    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 nihilo – Basile Starynkevitch Dec 16 '15 at 15:50
  • 1
    @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... – cmaster - reinstate monica Dec 16 '15 at 17:39
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!

Community
  • 1
  • 1
dhein
  • 6,431
  • 4
  • 42
  • 74
0

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

    stdio.h   => cstdio   (printf/scanf)
    math.h    => cmath     (sqrt)  
slfan
  • 8,950
  • 115
  • 65
  • 78