When does .h not need to include a header file?

It works:

#include <iostream> using namespace std; 

but this fails:

 #include <stdio> 

When is .h not required?

About the problem with the namespace, I did not 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_ */ 
+3
source share
3 answers

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

 #include <cstdio> 

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

Edit: A macro that puts names in the std namespace in the gcc version of cstdio is:

 _GLIBCXX_BEGIN_NAMESPACE(std) 

You can verify that your own header is doing what it should do by trying to use something like:

 std::printf( "hello" ); 

in your code.

+5
source

C ++ standard headers do not use .h. Everything else does (or, more precisely, everything else uses any extension that he wants, .h, .hxx, .hpp, .hh, etc.).

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

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

The second form wraps its characters in the std .

The initial intention was that the headers could, in principle, be stored in the database in some highly optimized pre-compiled state, in which case the idea of โ€‹โ€‹file extension does not make sense. I do not know that this has ever happened in practice.

+6
source

.h is not required, simply when .h is omitted from the file name in the file system.

+1
source

Source: https://habr.com/ru/post/1305356/


All Articles