By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,435 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,435 IT Pros & Developers. It's quick & easy.

Need an explanation

P: n/a
Hi

I am writing this tutorial stuff again in the holidays and I came across
this problem:

The "width" field in printf is a minimum width. Printf will not truncate
a field. for instance:
#include <stdio.h>
int main(void)
{
printf("%5s %03d\n","1234567890",12345);
}

will print
1234567890 12345
and NOT
12345 123
as expected.

-------------------

OK, but how can I EXPLAIN this stuff? Is there any reason?
And no, "compatibility with previous bugs" is NOT an explanation
at least for me, and not for a beginner.

What could I say here?

Thanks
Dec 26 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
>I am writing this tutorial stuff again in the holidays and I came across
>this problem:

The "width" field in printf is a minimum width. Printf will not truncate
a field. for instance:
#include <stdio.h>
int main(void)
{
printf("%5s %03d\n","1234567890",12345);
}

will print
1234567890 12345
and NOT
12345 123
as expected.
I suggest serious repair of your expecter is needed.
>-------------------

OK, but how can I EXPLAIN this stuff? Is there any reason?
Pretty, lined-up output is not as important as getting the value
right. It's not like the output is going on punch cards where
everything has to start in the right column. If one of those values
was on your paycheck, which would YOU want?
>And no, "compatibility with previous bugs" is NOT an explanation
at least for me, and not for a beginner.
Would you like to mention a deliberate INcompatability with
FORTRAN, which filled up fields that didn't fit with a row of *'s?
>What could I say here?
If you want to print a different number, calculate a different
number. For example, n/100 or n%1000 might be the number you want
to print.

Dec 26 '06 #2

P: n/a
Gordon Burditt a écrit :
>>I am writing this tutorial stuff again in the holidays and I came across
this problem:

The "width" field in printf is a minimum width. Printf will not truncate
a field. for instance:
#include <stdio.h>
int main(void)
{
printf("%5s %03d\n","1234567890",12345);
}

will print
1234567890 12345
and NOT
12345 123
as expected.


I suggest serious repair of your expecter is needed.

>>-------------------

OK, but how can I EXPLAIN this stuff? Is there any reason?


Pretty, lined-up output is not as important as getting the value
right. It's not like the output is going on punch cards where
everything has to start in the right column. If one of those values
was on your paycheck, which would YOU want?
The problem is that it is impossible to cut the output of a %s
to avoid a buffer oevrflow. This has led to serious problems in software
where people forgot this misfeature.

>
>>And no, "compatibility with previous bugs" is NOT an explanation
at least for me, and not for a beginner.


Would you like to mention a deliberate INcompatability with
FORTRAN, which filled up fields that didn't fit with a row of *'s?
That would have been a much better approach.

OK, no explanation then?

Because all you say is actually:

"It is like that, Swallow it or use Fortran".

I mean I would like to *explain* stuff, offer some
*reason* for language decisions like this.

Thanks for your answer.
Dec 26 '06 #3

P: n/a
In article <45***********************@news.orange.fr>,
jacob navia <ja***@jacob.remcomp.frwrote:
>The "width" field in printf is a minimum width. Printf will not truncate
a field. for instance:
#include <stdio.h>
int main(void)
{
printf("%5s %03d\n","1234567890",12345);
}

will print
1234567890 12345
and NOT
12345 123
as expected.

-------------------

OK, but how can I EXPLAIN this stuff? Is there any reason?
And no, "compatibility with previous bugs" is NOT an explanation
at least for me, and not for a beginner.
The field width is intended to be a width that will display the data
correctly and neatly. If the data doesn't fit in the field, it's
probably neatness you want to sacrifice rather than correctness.

The precision is slightly harder to explain, because it does different
things for each type, but the general idea is how much of the data to
display.

So the precision determines how the value is formatted, and the field
width specifies how it should be laid out, but doesn't override the
precision.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 26 '06 #4

P: n/a
jacob navia wrote:
Gordon Burditt a écrit :
>I am writing this tutorial stuff again in the holidays and I came across
this problem:

The "width" field in printf is a minimum width. Printf will not truncate
a field. for instance:
#include <stdio.h>
int main(void)
{
printf("%5s %03d\n","1234567890",12345);
}

will print
1234567890 12345
and NOT
12345 123
as expected.

I suggest serious repair of your expecter is needed.

>-------------------

OK, but how can I EXPLAIN this stuff? Is there any reason?

Pretty, lined-up output is not as important as getting the value
right. It's not like the output is going on punch cards where
everything has to start in the right column. If one of those values
was on your paycheck, which would YOU want?

The problem is that it is impossible to cut the output of a %s
to avoid a buffer oevrflow. This has led to serious problems in software
where people forgot this misfeature.
How will a printf statement cause a buffer overflow ?
OK, no explanation then?

Because all you say is actually:

"It is like that, Swallow it or use Fortran".
No , he said:

Gordon Burditt wrote:
Pretty, lined-up output is not as important as getting the value
right. It's not like the output is going on punch cards where
everything has to start in the right column. If one of those values
was on your paycheck, which would YOU want?
Sounds like a fine explanation to me. If it's not obvious
try answering his question:

*** If one of those values
*** was on your paycheck,
*** which would YOU want?

Dec 26 '06 #5

P: n/a
jacob navia said:

<snip>
The problem is that it is impossible to cut the output of a %s
to avoid a buffer oevrflow.
No, it isn't. I suggest you read the Standard's documentation on *printf
precision, or K&R2 page 244, more closely.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 26 '06 #6

P: n/a
In article <45***********************@news.orange.fr>,
jacob navia <ja***@jacob.remcomp.frwrote:
>The problem is that it is impossible to cut the output of a %s
to avoid a buffer oevrflow. This has led to serious problems in software
where people forgot this misfeature.
You mean where people forgot the precision part of the format?

Which is also useful for printing non-nul-terminated strings, as I may
have mentioned before :-)

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 26 '06 #7

P: n/a
In article <45***********************@news.orange.frjacob navia <ja***@jacob.remcomp.frwrites:
The "width" field in printf is a minimum width. Printf will not truncate
a field. for instance:
#include <stdio.h>
int main(void)
{
printf("%5s %03d\n","1234567890",12345);
}

will print
1234567890 12345
and NOT
12345 123
as expected.
I would expect (if I had not read the standard) either:
67890 345
or
***** ***
Why do you think your expectations are better? Why do you think that
printing 12345 in three digits as an integer should give 123? It just
does not fit. Apparently you have problems with the concept of
"minimal field width". For the string you can also specify whether
the width is also maximal. I do not think you can do so for the
integer, as any number printed with fewer digits than five is not
really represenative of the number. But you should look at Fortran
when you wish to have exact widths. In that case the output would
be: 12345 ***
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Dec 26 '06 #8

P: n/a
In article <45***********************@news.orange.frjacob navia <ja***@jacob.remcomp.frwrites:
....
The problem is that it is impossible to cut the output of a %s
to avoid a buffer oevrflow.
Did you look at the standard? Try: "%.5s".
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Dec 26 '06 #9

P: n/a
jacob navia a écrit :
Hi

I am writing this tutorial stuff again in the holidays and I came across
this problem:

The "width" field in printf is a minimum width. Printf will not truncate
a field. for instance:
#include <stdio.h>
int main(void)
{
printf("%5s %03d\n","1234567890",12345);
}

will print
1234567890 12345
and NOT
12345 123
as expected.

-------------------

OK, but how can I EXPLAIN this stuff? Is there any reason?
And no, "compatibility with previous bugs" is NOT an explanation
at least for me, and not for a beginner.

What could I say here?

Thanks
OK Thanks to all that answered. It was my fault. Actually the width is
not significative but the precision part. With the precision part
you *can* specify a maximum number of chars to be shown/printed.
Have a nice holidays.

Dec 26 '06 #10

P: n/a
Richard Heathfield a écrit :
jacob navia said:

<snip>

>>The problem is that it is impossible to cut the output of a %s
to avoid a buffer oevrflow.


No, it isn't. I suggest you read the Standard's documentation on *printf
precision, or K&R2 page 244, more closely.

<snip>
That's right. The problem is that the "width" field has a
wrong name. You would think it gives the width of the field
but it doesn't.
Dec 26 '06 #11

P: n/a
jacob navia wrote:
[...]
The problem is that it is impossible to cut the output of a %s
to avoid a buffer oevrflow. This has led to serious problems in software
where people forgot this misfeature.
Consider using the precision specifier instead of (or
in addition to) the width specifier. Subtle point: When you
use a precision with "%s", the corresponding argument must
still be a char* but need not point to a string. This comes
in handy when dealing with "counted strings" that are not
"strings" in C's sense.
I mean I would like to *explain* stuff, offer some
*reason* for language decisions like this.
Unless you can track down the people who thought up the
original ancestral printf() and ask them directly, this will
necessarily involve a certain amount of guesswork.

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 26 '06 #12

P: n/a
jacob navia said:
Richard Heathfield a écrit :
>jacob navia said:
<snip>
>>>The problem is that it is impossible to cut the output of a %s
to avoid a buffer oevrflow.

No, it isn't. I suggest you read the Standard's documentation on *printf
precision, or K&R2 page 244, more closely.

<snip>

That's right. The problem is that the "width" field has a
wrong name. You would think it gives the width of the field
but it doesn't.
The Standard describes the relevant value as specifying a "minimum field
width". This seems to me to be a correct description. If you think it's
wrong, take it up with ISO.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 26 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.