3

This works:

#include <iostream>
using namespace std;

but this fails:

#include <stdio>

When is .h not needed?

About the namespace issue,I didn't find such logic in cstdio:

#pragma once
#ifndef _CSTDIO_
#define _CSTDIO_
#include <yvals.h>

#ifdef _STD_USING
 #undef _STD_USING
  #include <stdio.h>
 #define _STD_USING

#else /* _STD_USING */
 #include <stdio.h>
#endif /* _STD_USING */

// undef common macro overrides
 #undef clearerr
 #undef feof
 #undef ferror
 #undef getc
 #undef getchar
 #undef putc
 #undef putchar

 #define _HAS_CONVENTIONAL_CLIB 1
 #define _IOBASE    _base
 #define _IOPTR _ptr
 #define _IOCNT _cnt

#ifndef _FPOSOFF
  #define _FPOSOFF(fp)  ((long)(fp))
#endif /* _FPOSOFF */

typedef FILE _Filet;

#ifndef RC_INVOKED
 #if _GLOBAL_USING
_STD_BEGIN
using ::_Filet;

using ::size_t; using ::fpos_t; using ::FILE;
using ::clearerr; using ::fclose; using ::feof;
using ::ferror; using ::fflush; using ::fgetc;
using ::fgetpos; using ::fgets; using ::fopen;
using ::fprintf; using ::fputc; using ::fputs;
using ::fread; using ::freopen; using ::fscanf;
using ::fseek; using ::fsetpos; using ::ftell;
using ::fwrite; using ::getc; using ::getchar;
using ::gets; using ::perror;
using ::putc; using ::putchar;
using ::printf; using ::puts; using ::remove;
using ::rename; using ::rewind; using ::scanf;
using ::setbuf; using ::setvbuf; using ::sprintf;
using ::sscanf; using ::tmpfile; using ::tmpnam;
using ::ungetc; using ::vfprintf; using ::vprintf;
using ::vsprintf;


_STD_END
 #endif /* _GLOBAL_USING */
#endif /* RC_INVOKED */

#endif /* _CSTDIO_ */
user198729
  • 61,774
  • 108
  • 250
  • 348
  • 1
    Maybe take a look the macros `_STD_BEGIN` and `_STD_END` for the namespace declaration. – Björn Pollex Mar 28 '10 at 09:47
  • I don't know how that macro exactly works,but seems it doesn't wrap `stdio.h`? – user198729 Mar 28 '10 at 09:50
  • Those `using` statements between `_STD_BEGIN` and `_STD_END` import those names into the std namespace. They would now be available both as `size_t` and `std::size_t` (don't ask me why they should be kept visible globally). - The C++ versions have other differences. For example, it appears that some things are macros in C libraries, which C++ redefines as normal functions. Also, `` adds various overloads to functions (no overloading in C) etc. – UncleBens Mar 28 '10 at 10:22

3 Answers3

6

Standard C++ headers don't use the .h. Everything else does (or, more accurately, everything else uses whatever extension it wants, .h, .hxx, .hpp, .hh and more).

Standard C headers can be included in one of two ways:

#include <stdio.h>
#include <cstdio>

The second form wraps its symbols in the std namespace.

The original intent was that headers could, in principle, be stored in a database in some highly optimised pre-compiled state, in which case the idea of a file extension wouldn't make sense. I don't know that this ever happened in practice.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
5

It's not needed for the header files defined by the C++ Standard, none of which have a .h extension. The C++ version of stdio.h is:

#include <cstdio>

which wraps stdio.h, placing the names in it in the C++ std namespace, but you can still use all the C Standard header files in C++ code, if you wish.

Edit: The macro that places the names in the std namespace in the GCC version of cstdio is:

_GLIBCXX_BEGIN_NAMESPACE(std)

You can check that your own header does what it should do by trying to use something like:

std::printf( "hello" );

in your code.

  • I took a look at the `cstdio`,but didn't find the logic to placing the names in `std` namespace – user198729 Mar 28 '10 at 09:39
  • 1
    @user198729 I don't know which compiler you are using, but that's what the standard says cstdio must do. –  Mar 28 '10 at 09:43
  • The compiler shouldn't matter,it's the header file itself that doesn't mention anywhere about `std`.You can have a look into it,also I've pasted it in my post above:) – user198729 Mar 28 '10 at 09:45
  • @user198729 The compiler matters a lot - the headers are part of the specific compiler implementation. –  Mar 28 '10 at 09:54
  • I'm using visual c++ 2008 express,seems the magic is done by `using ::_Filet;` and alike. – user198729 Mar 28 '10 at 10:20
1

the .h is not needed, simply when the .h is omitted from the file's name in the filesystem.

Chris Becke
  • 34,244
  • 12
  • 79
  • 148