Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is .h not needed to include a header file?

Tags:

c++

include

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_ */
like image 357
user198729 Avatar asked Feb 03 '26 18:02

user198729


1 Answers

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.

like image 122
Marcelo Cantos Avatar answered Feb 06 '26 07:02

Marcelo Cantos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!