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.
In a typical case, the C standard library is written primarily in C, and the C++ standard library primarily in C++. To give some concrete numbers, Microsoft's standard library has ~1050 C and C++ files, and 37 assembly language files.
Library functions are built-in functions that are grouped together and placed in a common location called library. Each function here performs a specific operation. We can use this library functions to get the pre-defined output. All C standard library functions are declared by using many header files.
Because the GNU C Library which you are familiar with is implemented for user mode, not kernel mode. The kernel cannot access a userspace API (which might invoke a syscall to the Linux kernel).
C has many libraries with pre-defined functions in the form of header files. To use these functions in our code, we need to include these header files. Header files contain definitions of functions, macros, and datatypes which we could use directly in our program by including the respective header file.
Standard library functions or simply C Library functions are inbuilt functions in the C compiler which make the code shorter and easy to read. There are many inbuilt functions are defined in each header file. For example, <stdio.h> header includes the functions like printf (), scanf (), etc.
We can use this library functions to get the pre-defined output. All C standard library functions are declared by using many header files. These library functions are created at the time of designing the compilers. We include the header files in our C program by using #include<filename.h>.
As we have already discussed, every C program has at least one function, that is, the main () function. The main () function is also a standard library function in C since it is inbuilt and conveys a specific meaning to the C compiler. 2. Significance of Standard Library Functions in C
All C standard library functions are declared by using many header files. These library functions are created at the time of designing the compilers. We include the header files in our C program by using #include<filename.h>. Whenever the program is run and executed, the related files are included in the C program.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With