Sandeep wrote:
Consider what happened when the compiler compiled the file (ie.
"compilation unit") with your code in it. It saw it needed a printf,
and that it had a printf already.
This is compiler dependent. I Tried compiling it with g++ and cl (MS
Compiler). While "g++" gave the output as "hello world" and "cl" gave
no output
Clearly the outcome of compiling any C++ program will always be
compiler-dependent. The question really is whether the observed
compiler behavior conforms to the C++ standard.
Since the sample program includes the header file <stdio.h>, it is
reasonable to expect that a printf routine will be declared in the
global namespace. Were the program to include <cstdio> instead, then
printf would be declared in the std namespace only. Unfortunately gcc
has never had very strong support for the std namespace. And although
gcc does add printf and the other std:: names to the std namespace when
<cstdio> is included, gcc also continues to declare those names in the
global namespace. Doing so is a departure from the C++ standard
(§17.4.1.2/4) which is clear on the point that the standard library
names are declared in the std namespace instead of - and not in
addition to - global namespace declarations.
As a consequence, the std:: namespace qualifier is never really
necessary in a program compiled with gcc as it often is when compiling
with a more standards observant C++ compiler.
Greg