473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

namespace std and standard headers

If I include both of these headers in a c++ source file:

#include <stdio.h>
#include <cstdio>

would I then be able to do both of:

std::printf("Hello, world!\n");
printf("Hello, world!\n");

--
Simon Elliott http://www.ctsn.co.uk
Oct 11 '05 #1
16 1664

Simon Elliott wrote:
If I include both of these headers in a c++ source file:

#include <stdio.h>
#include <cstdio>

would I then be able to do both of:

std::printf("Hello, world!\n");
printf("Hello, world!\n");

--
Simon Elliott http://www.ctsn.co.uk


It seems that you can do both but this is not a recommended as a good
programming practice because you can get into obscure errors or worse
you can mix them and compile but execute erroneous.
If you know what you are doing you may save yourself from some hard
work in C(sometimes)!

Oct 11 '05 #2
On 11/10/2005, re**********@gmail.com wrote:
It seems that you can do both but this is not a recommended as a good
programming practice because you can get into obscure errors or worse
you can mix them and compile but execute erroneous.
If you know what you are doing you may save yourself from some hard
work in C(sometimes)!


I'm trying to get some legacy code to compile. I wouldn't write new
code like this!

--
Simon Elliott http://www.ctsn.co.uk
Oct 11 '05 #3
On Tue, 11 Oct 2005 11:59:12 +0000, Simon Elliott wrote:
If I include both of these headers in a c++ source file:

#include <stdio.h>
#include <cstdio>

would I then be able to do both of:

std::printf("Hello, world!\n");
printf("Hello, world!\n");


Keep in mind that these will be separate streams, flushed at different
times. So your output may not come out sequentially as you would expect
without explicit stream flushing.

- Jay
Oct 11 '05 #4
In article <43***********************@news.gradwell.net>,
"Simon Elliott" <Simon at ctsn.co.uk> wrote:
If I include both of these headers in a c++ source file:

#include <stdio.h>
#include <cstdio>

would I then be able to do both of:

std::printf("Hello, world!\n");
printf("Hello, world!\n");


Yes, this should work. Those two print statements should also work if
you only #include <stdio.h>, but your odds are better of it working with
both includes.

-Howard
Oct 11 '05 #5
Jay Nabonne wrote:
On Tue, 11 Oct 2005 11:59:12 +0000, Simon Elliott wrote:

If I include both of these headers in a c++ source file:

#include <stdio.h>
#include <cstdio>

would I then be able to do both of:

std::printf("Hello, world!\n");
printf("Hello, world!\n");

Keep in mind that these will be separate streams


They're the same function, fiddling with the same stream.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Oct 11 '05 #6
In article <pa****************************@sonicNOSPAM.com> ,
Jay Nabonne <ja*********@sonicNOSPAM.com> wrote:
On Tue, 11 Oct 2005 11:59:12 +0000, Simon Elliott wrote:
If I include both of these headers in a c++ source file:

#include <stdio.h>
#include <cstdio>

would I then be able to do both of:

std::printf("Hello, world!\n");
printf("Hello, world!\n");


Keep in mind that these will be separate streams, flushed at different
times. So your output may not come out sequentially as you would expect
without explicit stream flushing.


Mmmm, I seem to recall there is some issues about whether
the C lib incorp'd herein is extern "C" or "C++"'d but not
whether there are things such as buffering issues. IMO the
above is fine AFAIK.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 11 '05 #7
On Tue, 11 Oct 2005 14:47:56 -0500, Pete Becker wrote:
Keep in mind that these will be separate streams


They're the same function, fiddling with the same stream.


Oops. My bad. I was "seeing" std::cout where there wasn't one... :(

Oct 11 '05 #8

Jay Nabonne wrote:
On Tue, 11 Oct 2005 11:59:12 +0000, Simon Elliott wrote:
If I include both of these headers in a c++ source file:

#include <stdio.h>
#include <cstdio>

would I then be able to do both of:

std::printf("Hello, world!\n");
printf("Hello, world!\n");


Keep in mind that these will be separate streams, flushed at different
times. So your output may not come out sequentially as you would expect
without explicit stream flushing.


I would not be surprised if cstdio looks like this:
namespace std{
#include <stdio.h>
}
or:
#include <stdio.h>
namespace std{
using printf;
// ......
}
Streams should be synchronized with stdio by default.
std::iostream::sync_with_stdio(false); // unsynchronize

Greetings, Bane.

Oct 11 '05 #9
"Branimir Maksimovic" <bm***@volomp.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I would not be surprised if cstdio looks like this:
namespace std{
#include <stdio.h>
}
I would. It's dead wrong.
or:
#include <stdio.h>
namespace std{
using printf;
// ......
}
That also doesn't conform exactly to the C++ Standard, but
it's a widely adopted practice. It'll probably be made
valid in the next release of the C++ Standard.
Streams should be synchronized with stdio by default.
std::iostream::sync_with_stdio(false); // unsynchronize


Right, though the "unsynchronize" operation may do
nothing (because there's nothing to be gained).

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Oct 11 '05 #10

P.J. Plauger wrote:
"Branimir Maksimovic" <bm***@volomp.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I would not be surprised if cstdio looks like this:
<snip unlikely contents of cstdio>
or:
#include <stdio.h>
namespace std{
using printf;
// ......
}


That also doesn't conform exactly to the C++ Standard, but
it's a widely adopted practice. It'll probably be made
valid in the next release of the C++ Standard.


Have I misunderstood something here - is the following a correct
program?

#include <cstdio>

int main()
{
printf("Hello world\n"); // or should that be std::printf?
}

I always thought that <cstdio> should put its names in the std
namespace only - not in the global namespace as well. 17.4.1.2/4 would
seem to confirm that. But Comeau online, for example, compiles my
program.

Because the

#include <stdio.h>
namespace std{
using printf;
// ......
}

approach is so widespread I have never been keen to use the <cname>
headers instead of <name.h>. If the compiler is likely to let me get
away with including <cstdio> but leaving the std:: off the front of
printf I might as well use <stdio.h> and have a correct program. But if
<cstdio> is allowed to put its names in the global namespace as well as
the std namespace, I'm worrying over nothing.

Gavin Deane

Oct 12 '05 #11
<de*********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
P.J. Plauger wrote:
"Branimir Maksimovic" <bm***@volomp.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
> I would not be surprised if cstdio looks like this:
<snip unlikely contents of cstdio>
> or:
> #include <stdio.h>
> namespace std{
> using printf;
> // ......
> }
That also doesn't conform exactly to the C++ Standard, but
it's a widely adopted practice. It'll probably be made
valid in the next release of the C++ Standard.


Have I misunderstood something here - is the following a correct
program?

#include <cstdio>

int main()
{
printf("Hello world\n"); // or should that be std::printf?
}


Should be std::printf, but it sometimes works anyway.
I always thought that <cstdio> should put its names in the std
namespace only - not in the global namespace as well. 17.4.1.2/4 would
seem to confirm that. But Comeau online, for example, compiles my
program.
You're correct about what the C++ Standard *says*. What most
implementations *do* is another matter. And that's because
the C++ committee repeatedly refused to listen to compiler
vendors while drafting the C++ Standard. (Don't forget export
either.)
Because the

#include <stdio.h>
namespace std{
using printf;
// ......
}

approach is so widespread I have never been keen to use the <cname>
headers instead of <name.h>. If the compiler is likely to let me get
away with including <cstdio> but leaving the std:: off the front of
printf I might as well use <stdio.h> and have a correct program. But if
<cstdio> is allowed to put its names in the global namespace as well as
the std namespace, I'm worrying over nothing.


You need to distinguish between what might happen and what you can
rely on. The safe rules are:

-- If you want assuredly to be able to write std::printf, include
<cstdio>.

-- If you want assuredly to be able to write ::printf, include
<stdio.h>.

The C++ Standard says that <cstdio> will not declare ::printf
and that <stdio.h> will also declare std::printf, but neither
of these rules is consistently followed in real life.

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Oct 12 '05 #12

P.J. Plauger wrote:
<de*********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...


<snip>
Because the

#include <stdio.h>
namespace std{
using printf;
// ......
}

approach is so widespread I have never been keen to use the <cname>
headers instead of <name.h>. If the compiler is likely to let me get
away with including <cstdio> but leaving the std:: off the front of
printf I might as well use <stdio.h> and have a correct program. But if
<cstdio> is allowed to put its names in the global namespace as well as
the std namespace, I'm worrying over nothing.


You need to distinguish between what might happen and what you can
rely on. The safe rules are:

-- If you want assuredly to be able to write std::printf, include
<cstdio>.

-- If you want assuredly to be able to write ::printf, include
<stdio.h>.

The C++ Standard says that <cstdio> will not declare ::printf
and that <stdio.h> will also declare std::printf, but neither
of these rules is consistently followed in real life.


Very helpful, thank you.

What I actually want is to assuredly avoid incorrect code compiling
successfully. It sounds like I will often get away with

#include <cstdio>

int main()
{
std::printf("stuff\n"); // Remembered the std::
std::printf("more stuff\n"); // Remembered again. Well done me.

// Get distracted for a bit ...

printf("Oops! Forgot std::\n"); // But it probably compiles OK
}

If the rule is likely not to be enforced by the compiler, and I am
likely to forget at some point, I might as well just use <stdio.h> and
avoid the whole problem.

Gavin Deane

Oct 12 '05 #13
<de*********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
What I actually want is to assuredly avoid incorrect code compiling
successfully. It sounds like I will often get away with

#include <cstdio>

int main()
{
std::printf("stuff\n"); // Remembered the std::
std::printf("more stuff\n"); // Remembered again. Well done me.

// Get distracted for a bit ...

printf("Oops! Forgot std::\n"); // But it probably compiles OK
}

If the rule is likely not to be enforced by the compiler, and I am
likely to forget at some point, I might as well just use <stdio.h> and
avoid the whole problem.


Yep. The use of namespaces to "contain" the Standard C library
was essentially a (predicted) failure.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Oct 12 '05 #14
On 12/10/2005, P.J. Plauger wrote:
You need to distinguish between what might happen and what you can
rely on. The safe rules are:

-- If you want assuredly to be able to write std::printf, include
<cstdio>.

-- If you want assuredly to be able to write ::printf, include
<stdio.h>.
And if I need to do both? (This is for legacy code; I can't think of
any reason for using both in new code.)
The C++ Standard says that <cstdio> will not declare ::printf
and that <stdio.h> will also declare std::printf, but neither
of these rules is consistently followed in real life.


Yes. I still use Borland C++ Builder a lot, and even the different
versions don't seem to be all that consistent in this respect. You can
even compile the same source file twice and get different results.

--
Simon Elliott http://www.ctsn.co.uk
Oct 12 '05 #15
"Simon Elliott" <Simon at ctsn.co.uk> wrote in message
news:43***********************@news.gradwell.net.. .
On 12/10/2005, P.J. Plauger wrote:
You need to distinguish between what might happen and what you can
rely on. The safe rules are:

-- If you want assuredly to be able to write std::printf, include
<cstdio>.

-- If you want assuredly to be able to write ::printf, include
<stdio.h>.


And if I need to do both? (This is for legacy code; I can't think of
any reason for using both in new code.)


Include both headers.
The C++ Standard says that <cstdio> will not declare ::printf
and that <stdio.h> will also declare std::printf, but neither
of these rules is consistently followed in real life.


Yes. I still use Borland C++ Builder a lot, and even the different
versions don't seem to be all that consistent in this respect. You can
even compile the same source file twice and get different results.


That's probably because Borland started out with the RogueWave
library, which tries to do what the C++ Standard requires
with C header namespaces, IIRC. Then they tried STLport instead,
which doesn't. Now they're switching to our library, which can
go either way.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Oct 13 '05 #16
On 13/10/2005, P.J. Plauger wrote:
And if I need to do both? (This is for legacy code; I can't think of
any reason for using both in new code.)
Include both headers.


That's what I hoped you'd say. Thanks for the info.
I still use Borland C++ Builder a lot, and even the different
versions don't seem to be all that consistent in this respect. You
can even compile the same source file twice and get different
results.

That's probably because Borland started out with the RogueWave
library, which tries to do what the C++ Standard requires
with C header namespaces, IIRC. Then they tried STLport instead,
which doesn't. Now they're switching to our library, which can
go either way.


Which versions use your library?

--
Simon Elliott http://www.ctsn.co.uk
Oct 13 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Leon | last post by:
Hi all. For my application I'm using a third-party library which is supplied as a library file (.lib) and an associated header file to prototype the functions defined in the library. Important...
10
by: Erwin Burgstaller | last post by:
When I have several header files with "namespace x {}" in each, the resulting doxygen documentation lists the Namespace exactly as often as there are header files with that namespace in. Thats only...
7
by: zbyszek | last post by:
I am working with a large C++ program which, for reasons of backward compatibility, uses C's printf and fprintf rather than iostreams. For a certain type of build I want to provide new functions...
8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
6
by: Steffen Hampel | last post by:
I got an rather large project which i recently startet to split up into namespaces. At a certain point the compiler (MSVC 6.0 sp5) began to give me C2871 errors ( 'name' : does not exist or is...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
12
by: bgeneto | last post by:
I know that it's a very basic question, but I can't figure out or find an answer to why do we have to specify a namespace, like this #include<string> using namespace std; when using the...
5
by: Michael | last post by:
Hi, If I use using namespace std; It will include all the standard C++ libaries? I mean iostream, string, etc. ? Thanks, Michael
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.