Ivaylo Valchev
Ivaylo Valchev

Reputation: 10415

When using C headers in C++, should we use functions from std:: or the global namespace?

C is somewhat, not exactly, a subset of C++. So we can use most of the C functions/headers in C++ by changing the name a little bit (stdio.h to cstdio, stdlib.h to cstdlib).

My question is actually kind of semantic. In C++ code (using newest version of GCC compiler), I can call printf("Hello world!"); and std::printf("Hello world!"); and it works exactly the same. And in the reference I am using it also appears as std::printf("Hello world!");.

My question is, is it preferred to use std::printf(); in C++? Is there a difference?

Upvotes: 124

Views: 8964

Answers (9)

Charile C
Charile C

Reputation: 1

First of all, printf is a well-known function in the C library, and almost nobody would like to name a function as printf.

Second, in the header file <cstdio>, printf is both in the std namespace and the global namespace. So writing std:: prefix or not just work the same.

In my opinion, If your code and the library you use do not contain any printf in other namespaces. Just simply use printf, at least you can save your time from typing std::. Or better use std::printf to avoid any possible name conflicts. std::printf is safer. printf is more convenient.

Upvotes: -1

sergej
sergej

Reputation: 17979

From the C++11 Standard (emphasis mine):

D.5 C standard library headers [depr.c.headers]

  1. For compatibility with the C standard library ...
  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.

Using the «name.h» headers is deprecated, they have been identified as candidates for removal from future revisions.

So, I would suggest to include the «cname» headers and to use the declarations and definitions from the std namespace.

If you have to use the «name.h» headers for some reasons (it's deprecated, see above), I would suggest to use the declarations and definitions from the global namespace.

In other words: prefer

#include <cstdio>

int main() {
    std::printf("Hello world\n");
}

over

#include <stdio.h>

int main() {
    printf("Hello world\n");
}

Upvotes: 116

eiennohito
eiennohito

Reputation: 158

From my own practice: use std:: prefixes. Otherwise one day abs will bite you very painfully in case you using floating points.

Non-qualified abs refers to function defined on int on some platforms. On others it is overloaded. However std::abs is always overloaded for all types.

Upvotes: 3

anukul
anukul

Reputation: 1952

In stdio

This is the C++ version of the Standard C Library header @c stdio.h, and its contents are (mostly) the same as that header, but are all contained in the namespace @c std (except for names which are defined as macros in C).

So it should not make any difference.

Upvotes: 2

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247899

No, you're fine either way.

The original intent was that the <___.h> headers would be the C versions which put everything in the global namespace, and the <c___> headers would be the C++-ified versions, which place everything in the std namespace.

In practice, though, the C++ versions also put everything into the global namespace. And there's no clear consensus that using the std:: versions is "the right thing to do".

So basically, use whichever you prefer. The most common is probably to use the C standard library functions in the global namespace (printf instead of std::printf), but there's not much reason to consider one "better" than the other.

Upvotes: 10

NeonMercury
NeonMercury

Reputation: 51

From the C++11 standard:

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).

So, if you use <cstdio>, you can be sure, that printf will be in the namespace std, and hence not in global namespace.
Using a global namespace creates a conflict of names. This is not C++ way.

Therefore, I'm using <cstdio> headers and advise you to do so.

Upvotes: 3

syntagma
syntagma

Reputation: 24294

The only difference there is is that in std::printf() by adding std:: scope resolution you will secure yourself from someone writing a function with the same name in the future, which would lead to namespace conflict. Both usages will lead to exactly the same OS API calls (you can check it under Linux by running strace your_program).

I find it very unlikely that someone would name a function like that, as printf() is one of the most commonly used functions out there. Also, in C++, iostreams are preffered over calls to cstdio functions like printf.

Upvotes: 3

T.C.
T.C.

Reputation: 137301

<cmeow> always provides ::std::purr and may or may not provide ::purr.

<meow.h> always provides ::purr and may or may not provide ::std::purr.

Use the form that is guaranteed to be provided by the header you include.

Upvotes: 86

razvan
razvan

Reputation: 525

Using just printf without std:: could generate some name conflicts and is considered a bad practice by a lot of c++ devs. Google is your friend on this one, but here are some links, hope this helps

Why is "using namespace std" considered bad practice? http://www.cplusplus.com/forum/beginner/61121/

Upvotes: 2

Related Questions