6

To my knowledge, headers of the form cxyz are identical to xyz.h with the only difference being that cxyz places all of the contents of xyz.h under the namespace std. Why is it that the following programs both compile on GCC 4.9 and clang 6.0?

#include <cstdio>

int main() {
    printf("Testing...");
    return 0;
}

and the second program:

#include <cstdio>

int main() {
    std::printf("Testing...");
    return 0;
}

The same goes for the FILE struct:

FILE* test = fopen("test.txt", "w");

and

std::FILE* test = std::fopen("test.txt", "w");

both work.

Up until now, I always thought that it was better to use cstdio, cstring, etc, rather than their non-namespaced counterparts. However, which of the following two programs above are better practice?

The same goes for other C functions such as memset (from cstring), scanf (also from cstdio), etc.

(I know some people will ask why I am using C IO in a C++ program; the issue here is not specifically C IO, but whether or not this code should compile without specifically specifying std:: before calling a namespaced C function.)

ra1nmaster
  • 662
  • 1
  • 8
  • 21
  • I see. So in my code, which style should I use if I wish to use, for example, memset from the cstring header? Is it better to write 'std::memset' or 'memset'' – ra1nmaster Jan 30 '15 at 00:06

1 Answers1

8

The standard permits the compiler to also inject the names into the global namespace.

One reason for this is that it permits the implementation of <cstdio> to be:

#include <stdio.h>

namespace std
{
    using ::printf;
    using ::fopen;
    // etc.
}

so the compiler/library vendor does not have to write and maintain so much code.

In your own code, always use std:: or using namespace std; etc. so that your code is portable to compilers which do not inject the names into global namespace.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Thanks for the explanation! It was a little confusing since I haven't yet seen a compiler that doesn't inject the names into the global namespace. – ra1nmaster Jan 30 '15 at 00:10
  • @Ra1nMaster they probably do it to avoid bug reports / support issues from people who want to know why `printf` isn't working – M.M Jan 30 '15 at 00:11
  • @ra1nmaster: strictly speaking, it's the standard library and not the compiler which is responsible for injecting (or not) the functions into the global namespace. – rici Jan 30 '15 at 00:42
  • @rici the existance of a standard library is an implementation detail for the compiler – Yakk - Adam Nevraumont Jan 30 '15 at 02:44