Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 370
Nimit Bhardwaj Avatar asked Dec 16 '15 09:12

Nimit Bhardwaj


People also ask

Is the C standard library written in C?

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.

Are library functions part of C language?

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.

Why we Cannot use C standard library function in kernel module?

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).

How are library functions accessed in C?

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.

What are standard library functions in C programming?

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.

How to use library functions to get pre-defined output in C?

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>.

How many functions does a C program have?

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

How do you declare a library function 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.


2 Answers

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.

like image 80
Basile Starynkevitch Avatar answered Oct 11 '22 20:10

Basile Starynkevitch


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!

like image 27
dhein Avatar answered Oct 11 '22 20:10

dhein