Libraries of c ++ and c

10

Good morning. I would like to know some concepts about the c ++ and c libraries. My first question would be why you can use stdio.h in c ++ in addition to iostream, and which is better to use in the end, besides another question would be the difference between writing "stdlib.h" and "cstdlib" all that in c ++ . I've been studying c and now I'm starting with c ++ and I'm not clear about the differences.

    
asked by Raul 08.02.2017 в 01:29
source

2 answers

11
  

Why can you use stdio.h in C ++ in addition to iostream ?

The compiler.

On many occasions, the compiler used for C ++ and C is the same, just change the compiler configuration to make compilations in one way or another. For this reason it is not uncommon for the libraries of both languages to be mutually available, that is: you could also include <iostream> in a C code but it would not do you any good since C ++ has different keywords ( template or namespace do not exist in C), some keywords do not have the same meaning (for example auto 1 or register 2 ) and even C ++ has deprecated elements that in C are still valid (such as digraphs or trígrafos ).

Custom.

Many programmers whose main language has been for years C also work with C ++. Also, programmers of certain generations have been educated with C in universities and educational centers before with C ++ and for these programmers it is usually more convenient to use C utilities, syntaxes and constructs before C ++ approaches even if they are programming C ++; You could not do this if it were not for the ...

Compatibility with C.

C ++ is based on C and evolves from it. The C ++ standards committee makes a lot of efforts to maintain a compatibility with C (although this, with each passing year causes more controversy, may stop worrying about this compatibility in the future). Thanks to the efforts made in this compatibility, any code of C is compilable in C ++ using the appropriate libraries and with minimal changes (or none).

  

What is the difference between writing "stdlib.h" and "cstdlib" .

C ++ adapts many of the C libraries to its own idiosyncrasies, classifying functions in namespaces or transforming some functions into templates, when a C library has been adapted to C ++ its file receives a c as a prefix and you delete the extension, here is a list of C header files and its header adapted in C ++ :

|   C++    |     C     |
+----------+-----------+
| cassert  | assert.h  |
| cctype   | ctype.h   |
| cerrno   | errno.h   |
| cfloat   | float.h   |
| ciso646  | iso646.h  |
| climits  | limits.h  |
| clocale  | locale.h  |
| cmath    | math.h    |
| csetjmp  | setjmp.h  |
| csignal  | signal.h  |
| cstdarg  | stdarg.h  |
| cstdbool | stdbool.h |
| cstddef  | stddef.h  |
| cstdint  | stdint.h  |
| cstdio   | stdio.h   |
| cstdlib  | stdlib.h  |
| cstring  | string.h  |
| ctime    | time.h    |
| cuchar   | uchar.h   |
| cwchar   | wchar.h   |
| cwctype  | wctype.h  |
  

Which is better to use?

Use the headline collection specific to each language, that is: in C use stdio.h and if you need something from that library in C ++ use cstdio .

.

1 In C auto is a storage specifier (the same category as static , for example) while in C ++ is used for the static deduction of types .

2 In C register is used to indicate to the compiler that the qualified variable with this keyword will be very used and the programmer recommends that it be stored in a processor register, in C ++ this word key is deprecated (as of C ++ 17).

    
answered by 08.02.2017 в 09:16
3
  

Why can you use stdio.h in c ++ in addition to iostream ?

At the beginning, what we know today as C ++ were nothing but a compendium of macros designed for C to behave like an object-oriented language. Over time it became necessary to create a language of their own and there C ++ was born.

Because of that common history, C libraries have always been shared by C ++ and, for compatibility, they will continue to be.

In the case of cstdio , this library exists only to maintain consistency with the standard C ++ library, where no library includes the .h extension.

In the case of iostream we have our own C ++ library that can take advantage of all the benefits of this language. This makes available a number of input / output facilities with many utilities that we can also expand and configure at will (So, for example, we can prepare our classes so they can be printed on screen using the insertion operator std::cout << miClase , which in C is unthinkable).

On the other hand, the fact that there is stdio.h e iostream is not all bad:

  • People coming from C can take advantage of a smoother learning curve because they can reuse much of the knowledge they already have.
  • There are operations that can be easier to implement with the C libraries
  • Having the C libraries increases the compatibility between both languages.
  

What is the difference between writing stdlib.h and cstdlib ?

Absolutely none. You find the difference when compiling in C or compiling in C ++. The libraries available in C ++ (both stdio.h as cstdio ) add a version of the functions under the namespace std , which for obvious reasons can not be done in C.

  

Which is better to use?

Since there are no differences, feel free to use the version that you like.

    
answered by 08.02.2017 в 09:52