48

I see this line from the c++ reference for cstdio:

Every element of the library is defined within the std namespace. but I tried the code:

std::printf("hello world"); 

printf("hello world");

is it true that C++ headers puts the names in both the std and the global namespace?

Flexo
  • 87,323
  • 22
  • 191
  • 272
danny
  • 1,095
  • 2
  • 12
  • 27

2 Answers2

60

Including cstdio imports the symbol names in std namespace and possibly in Global namespace.
Including stdio.h imports the symbol names in Global namespace and possibly in std namespace.

The same applies for all c-styled headers.


Reference:
C++11 standard

Annex D (normative) Compatibility features [depr] states:

D.6 C standard library headers

1 For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, as shown in Table 151.

Which include:

<assert.h> <float.h> <math.h> <stddef.h> <tgmath.h> <complex.h> <inttypes.h> <setjmp.h> <stdio.h> <time.h> <ctype.h> <iso646.h> <signal.h> <stdint.h> <uchar.h> <errno.h> <limits.h> <stdarg.h> <stdlib.h> <wchar.h> <fenv.h> <locale.h> <stdbool.h> <string.h> <wctype.h>

Further on,

2 Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).

3 [ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • So this is implementation dependent? Interesting, I wasn't aware. – Mr Lister May 05 '12 at 08:56
  • @MrLister: Yes, and there is no confusion over this the standard clearly states so. – Alok Save May 05 '12 at 09:04
  • 1
    am I correct that I should prefer to include and that "stdio.h" is "deprecated"? – Oleg Vazhnev Jan 08 '14 at 16:09
  • @javapowered Officially, yes. Since C++11, the C compatibility headers have been specified in the "Compatibility features" annex of the standard, which describes *"deprecated features, where deprecated is defined as: Normative for the current edition of this International Standard, but having been identified as a candidate for removal from future revisions"*. Labelling them as "identified as a candidate for removal" is arguably perverse, though. http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0619r0.html#3.5 argues that the headers will be retained forever, and should be undeprecated. – Mark Amery Jan 12 '19 at 16:42
  • @AlokSave So, which one is preferred, `#include` or `#include `? – John May 25 '22 at 06:27
11

According to libstdc++ docs:

The standard specifies that if one includes the C-style header (<math.h> in this case), the symbols will be available in the global namespace and perhaps in namespace std:: (but this is no longer a firm requirement.) One the other hand, including the C++-style header (<cmath>) guarantees that the entities will be found in namespace std and perhaps in the global namespace.

http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt01ch03s02.html

rubenvb
  • 74,642
  • 33
  • 187
  • 332
CodeClown42
  • 11,194
  • 1
  • 32
  • 67
  • I think if we use `` then it will guarantee that the entities will be available under `namespace std` and hence not in global namespace. – Krishna Oza Apr 14 '15 at 10:17
  • 2
    @KrishnaOza Not true. See [here](https://stackoverflow.com/questions/71604454/is-it-undefined-behavior-to-use-sqrt-when-including-cmath-as-opposed-to-m). – Jason Mar 24 '22 at 14:42