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