C++ - include unistd.h: why not cunistd? - c++

It's said that when including C header files in C++, the ".h" suffix should be removed and then add "c" at the beginning. For example, #include <cstdio> instead of #include <stdio.h>. But when I use sleep() in my code, #include <cunistd> does not work, but #include <unistd.h> works. Why not <cunistd>?

Your algorithm is correct for most (all?) standard C headers, but unistd.h is not part of standard C so standard C++ in turn doesn't include it with the other c... headers.

Because unistd.h never was part of the C language. It is part of the Operating System.

<unistd.h> , stands for unix standard header ,the name says it all.

unistd.h is not part of standard C.
Standard C++ lib doesn't include it with the other c headers.

Related

Which parts of the C standard library are not covered by (the rest of) the C++ standard library?

The C++ library includes the same definitions as the C language library
But the C++ library seems to duplicate (/extend) some of the functionality of the C library in non-C-library headers. For example, the C library has <string.h>, and the C++ library has both <cstring> and <string>; the C library has <time.h>, and the C+ library has both <ctime> and <chrono>.
If I need a string class, I assume that I am better off using <string> instead of <cstring>, because <string> can benefit from all the non-C functionality in C++ (e.g. exceptions). But there is functionality in the C library that doesn't exist in any other form in the C++ library. For example, I couldn't find anything like memcpy and memcmp outside <cstring>.
Which parts of the C library have no analogue in the non-C-library headers?
(If the version of the C++ standard matters for this, I am interested in C++11.)
There aren't that many headers, so lets just list them. Some could be replaced by language facilities, rather than libraries. (I haven't enumerated every function in each header, so I might have missed the odd function that doesn't have a C++ alternative when most of its colleagues do.)
C library C++ alternatives
assert.h Exceptions
complex.h <complex>
ctype.h None (or maybe <locale>, if you want to jump down that rabbit-hole)
errno.h None (but only applies to C functions)
fenv.h None
float.h <limits>
inttypes.h (See breakdown)
formatting <iostream>
strto... <string> (C++11), <sstream>
imaxabs std::abs overloads
imaxdiv std::div overloads
iso646.h Language
locale.h <locale>
math.h None (extended with C++ overloads)
setjmp.h Exceptions
signal.h None
stdarg.h Variadic templates (C++11)
stdbool.h Language
stddef.h None
stdint.h None
stdio.h <iostream> etc.
stdlib.h (See breakdown)
atof etc. <sstream>, <string> (C++11)
rand etc. <random> (C++11)
malloc etc. new, containers
abort etc. None
bsearch etc. <algorithm>
abs etc. None (extended with C++ overloads)
mb strings None
string.h <string>, <algorithm>
tgmath.h <cmath> (C++ overloads)
time.h <chrono> (C++11)
wchar.h <iostream> etc.
wctype.h None
To summarise:
Which parts of the C library have no analogue in the non-C-library headers?
[w]ctype.h, errno.h, fenv.h, fenv.h, math.h, signal.h, stddef.h, stdint.h, some of stdlib.h. Before C++11, also stdarg.h, time.h and more of stdlib.h

Pre processor directive conio.h c++

As in C++ header files are used without .h extension like <iostream> instead of <iostream.h> but its not same in case of <conio.h>. Why we can't use <conio>
The C++ standard specifies which headers are part of the C++ standard library. In addition to C++-specific headers, it includes the headers specified by the C standard. You can use them with their C names (e.g., #include <stdio.h>), and they put their symbols into the global namespace. You can use them without the .h extension and a c on the front (e.g., #include <cstdio>), and they put their symbols into the namespace std.
But that's only for the headers from the C standard. conio.h is not part of the C standard, so the C++ standard doesn't say anything about it.
conio.h is a C header, thus (traditionally) C headers had the .h extension for the system headers. C++ standard headers are mainly without this .h extension. As you may know, many C headers (those from the standard library) have C++ counterparts (like in C++ is )
Because conio.h is a C header, not C++ specific.
conio isn't part of the c++ standard, so you can't count on the compiler to know what it is. :(
In fact, i think it's usually only supported under windows.

What is the différence between #include <iostream.h> and #include <iostream>? [duplicate]

This question already has answers here:
Difference between iostream and iostream.h
(3 answers)
Closed 9 years ago.
What is the difference between
#include <iostream.h>
and
#include <iostream>
?
Before C++ was even standardised, the I/O library was developed as <iostream.h>. However, that header has never been a standard C++ header. Some older compilers continued to distribute the <iostream> header also as <iostream.h>. Use <iostream> because it is guaranteed by the standard to exist.
It's worth noting that the only standard headers that end with .h are the C standard library headers. All C++ standard library headers do not end with .h.
<iostream> is the usual header
<iostream.h> is the old header, not longer supported by some compilers
It just depends on the name of the file provided by your toolchain.
Some (old) compilers use .h files.
Modern compilers usually use <iostream> (without the .h extension).

#include <file> #include <file.h> - what's the difference? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between <string> and <string.h>?
My specific example uses following clause:
#include <string>
If I use following clause instead
#include <string.h>
compiler ends with error
[BCC32 Error] utils.cpp(173): E2316 'getline' is not a member of 'std'
Line 173 in utils.cpp file is as follows:
while(std::getline(in, line, '\n'))
I thought that there is no difference between these two clauses. Now I am confused. What files are in fact included by these two clauses? Lets say, my C++ Builder installation has program directory C:\Program Files\RAD Studio\9.0 and include files are located in subdirectory C:\Program Files\RAD Studio\9.0\include.
#include <string>
This includes the C++ string header.
#include <string.h>
This includes the C string header, with all identifiers in the global namespace. (Deprecated.)
#include <cstring>
This includes the C string header, with all identifiers placed in the std:: namespace.
Edit: Rule of thumb - C++ headers never end on ".h". Prefix the traditional C header name with "c" and drop the ".h" to keep the global namespace clean. Use ".h" for your project's C headers only. Use ".hpp" for C++-only headers.
They are two different headers. The convention in the C standard library is to have the headers ending with .h, whereas in the C++ standard library the convention is to miss out the file extension altogether. Some more detail from wikipedia:
Each header from the C Standard Library is included in the C++
Standard Library under a different name, generated by removing the .h,
and adding a 'c' at the start; for example, 'time.h' becomes 'ctime'.
The only difference between these headers and the traditional C
Standard Library headers is that where possible the functions should
be placed into the std:: namespace (although few compilers actually do
this). In ISO C, functions in the standard library are allowed to be
implemented by macros, which is not allowed by ISO C++.
Other libraries follow different conventions. Boost, for instance, chooses .hpp as their C++ header extension of choice.
By convention, C (Procedural) headers ends by '.h' "string.h", "stdio.h"... and C++ (Object oriented mostly) don't include any extension: "iostream", "string" ...
Not sure if all headers follow this convention, but I think that the standards ones all do.

Including C headers inside a C++ program

I have a C++ program (.cpp) inside which I wish to use some of the functions which are present inside the C header files such as stdio.h, conio.h, stdlib.h, graphics.h, devices.h etc.
I could include the stdio.h library inside my cpp file as : #include <cstdio>.
How do I include the other library files?
How do I add the graphics.h library?
I'm using Microsoft Visual Studio 6.0 Enterprise Edition and also Turbo C++ 3.0.
For a list of C standard C headers (stdio, stdlib, assert, ...), prepend a c and remove the .h.
For example stdio.h becomes cstdio.
For other headers, use
extern "C"
{
#include "other_header.h"
}
If you put this inside your headers:
#ifdef __cplusplus
extern "C"
{
#endif
// your normal definitions here
#ifdef __cplusplus
}
#endif
Then it will work for both C and C++ without any problem ...
Hope this helps...:)
I'm not sure what you need exactly, but if you want to use old fashioned C functions inside you C++ program, you can easy include them by removing the .h and add a "c" prefix.
for example if you want to include math.h use
#include <cmath>
Just include them inside a extern "C" block an they should work like expected.
You can #include them using their original names. #include <stdio.h> works just fine in C++.