Re: int main(void) is better than int main()
James Kanze wrote:
On Jan 6, 1:02 am, Tim H <thoc...@gmail.com> wrote:
On Jan 4, 11:59 am, James Kanze <james.ka...@gmail.com> wrote:
It's generally a lot easier with cout. About the only
printf formatting I know that you can't easily achieve with
ostream is the ' ' modifier, i.e.: "% d".
"can't achieve" or "can't easily achieve" ?
Can't achieve directly. You can achieve anything if you format
into an std::ostringstream, and then do padding, etc. manually.
Isn't this, or can't this be, assuming int x;
std::cout << (x >= 0 ? std::string(' ') : std::string());
std::cout << x;
Or have I missed something?
You *can* simulate printf formatting with ostream today,
although why you would want to? Boost::format supports almost
all of the printf formatters (I think the ' ' flag is one of the
It also lacks the * formatter, which hurts me.
What do you mean by "the * formatter"? The only place you can
use a '*' in a format specification of printf is for width and
precision. From what I understand of boost::format (but
admittedly, I've never used it), it allows all of the normal
manipulators as well, so you can use setw() and setprecision()
to achieve the same effect as the '*'. (setprecision() will
affect all of the following variables, where as ".*" won't, so
they effect isn't exactly the same.)
And even if it is lacking, there's always code like this, no matter how
annoying and bad it might be,
std::string df(const unsigned int width) {
std::ostringstream ox;
ox << "%" << width << "d\n";
return ox.str();
}
....
printf(df(5).c_str(),x);
....
Another thing which I think is lacking, however, is precision of
an int or a string, e.g. printf( "%-10.8s|%5.3d",
"abcdefghijklmnopqrstuvwxyz", 2 ) will output:
"abcdefgh | 002"
From what I've seen, however, most people don't actually know
the embedded language used by printf very well, and aren't aware
of such features to begin with.
common things I do:
printf("%3d: %#08x\n", x, y);
std::cout << boost::format("%3d: %#08x\n") %x %y;
How do you do this easily with plain old cout?
std::cout << std::setw( 3 ) << x
<< Gabi::HexFmt( 8, ios::showbase ) << y << '\n' ;
Except that I'd usually write:
std::cout << std::setw( 3 ) << x
<< "0x" << Gabi::HexFmt( 8, ios::uppercase ) << y <<
'\n' ;
I'm not familiar with 'Gabi'. Is that part of the standard?
Adding to the snippet above,
const int x = 123;
const int y = 0xabcd;
my compiler produces,
123: 0x00abcd
for the OP's printf statement. I can't tell from reading the only draft
copy of a C standard I have, but I'm going to guess that the '%#08x'
format width calculations include the '0x'. Assuming that in the OPs
snippet x and y were meant to be int and sizeof(int) is 4 and the number
of bits per char is 8, I wonder if the OP meant '%#010x'?
So using plain old cout, what's wrong with,
std::cout
<< std::dec
<< std::setw(3)
<< x
<< ": "
<< (y == 0 ? std::string() : std::string("0x")) << std::setfill('0')
<< std::setw(6) // or std::setw(8) if you wanted 8 hex digits
<< std::hex
<< y
<< std::endl;
since I prefer the format "0x0000ABCD" for hex.
I suppose it would be superfluous to mention std::uppercase.
I'm not sure I understood the OP's question or your response. What am I
missing, please?
LR