Thread: to C or C++
View Single Post
Old 04-30-2004, 03:32 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Ah, perhaps it isn't part of the ansi C but it is now for ansi C++.

C (without the ++) was never my terrain though . I skipped that part when moving from Pascal to C++. But I knew the "c-part" in front of the headers were related to ANSI and C-something .

Anyway, here is a bit of reference I used. I remembered I read something similar before.
Quote:
ANSI C++
Standard header files


In ANSI-C++ the way to include header files from the standard library has changed.

The standard specifies the following modification from the C way of including standard header files:

Header file names no longer maintain the .h extension typical of the C language and of pre-standard C++ compilers, as in the case of stdio.h, stdlib.h, iostream.h, etc. This extension h simply disappears and files previously known as iostream.h become iostream (without .h).
Header files that come from the C language now have to be preceded by a c character in order to distinguish them from the new C++ exclusive header files that have the same name. For example stdio.h becomes cstdio .
All classes and functions defined in standard libraries are under the std namespace instead of being global. This not applies to C macros that remain as C macros.
Here you have a list of the standard C++ header files:

<algorithm> <bitset> <deque> <exception> <fstream> <functional> <iomanip> <ios> <iosfwd> <iostream> <istream> <iterator> <limits> <list> <locale> <map> <memory> <new> <numeric> <ostream> <queue> <set> <sstream> <stack> <stdexcept> <streambuf> <string> <typeinfo> <utility> <valarray> <vector>
And here is a list of the C header files included in ANSI-C++ with their new name and their equivalents in ANSI-C:
ANSI-C++ ANSI-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>
<cstddef> <stddef.h>
<cstdio> <stdio.h>
<cstdlib> <stdlib.h>
<cstring> <string.h>
<ctime> <time.h>
<cwchar> <wchar.h>
<cwtype> <wtype.h>

Since now classes and functions of the standard libraries are located within the std namespace we must use the C++ using directive for that these become usable in the same way they were in pre-standard code. For example, to be able to use all the standard classes of iostream we would have to include something similar to this:

#include <iostream>
using namespace std;
that would be equivalent to the old expression
#include <iostream.h>
previous to the standard.
Nevertheless for compatibility with ANSI-C, the use of name.h way to include C header files is allowed. Therefore, both following examples are valid and equivalent in a compiler which fulfills ANSI-C++ specifications:

// ANSI C++ example

#include <cstdio>
using namespace std;

int main ()
{
printf ("Hello World!");
return 0;
}

// pre ANSI C++ example
// also valid under ANSI C++, but deprecated

#include <stdio.h>

int main ()
{
printf ("Hello World!");
return 0;
}




In all the examples of the current version of The C++ tutorial it has been chosen to include the old way because to date is the most compatible format (and also shorter), although if you have a compiler that supports ANSI-C++ standard I recommended you the use of the new format in your programs.



ŠThe C++ Resources Network, 2000 - All rights reserved
__________________
Valmont is offline   Reply With Quote