??? 10/15/08 12:06 Modified: 10/15/08 12:08 Read: times |
#159079 - Wrong! Responding to: ???'s previous message |
By definition, ':', '\t', '\r' and '\n' are always one character large (but may be multiple bytes on a compiler running wide character sets). That is a known.
But your assumptions about the produced number of characters when printing numbers is based on an incorrect assumption. %4d is four characters large if and only if the value to print can fit in four characters. The value -1000 requires five characters. The value 12345 also requires five characters. printf() will not throw away extra characters to produce a lie. It will extend the field width and emit every required character! That is why you should never (N.E.V.E.R.) manually "count" the number of produced characters. In this case, you may write a broken record to a flash file system, or send a broken packet on a communication board. But this world is full of programs with dangerous buffer-overflow vulnerabilities, costing many millions of dollars every hour, just because people have assumed that they knew the size of the output from the *printf() family of functions. |
Topic | Author | Date |
How many bytes will be written ?? | 01/01/70 00:00 | |
return value? | 01/01/70 00:00 | |
OT: That's why you should never use TABs! | 01/01/70 00:00 | |
go ahead and use tabs | 01/01/70 00:00 | |
No, don't! | 01/01/70 00:00 | |
Any programmers editor | 01/01/70 00:00 | |
insert spaces for TABs | 01/01/70 00:00 | |
Oh Yes ... the sprintf()... | 01/01/70 00:00 | |
So here we go | 01/01/70 00:00 | |
Wrong! | 01/01/70 00:00 | |
Valid warnings. But... | 01/01/70 00:00 | |
No buts! | 01/01/70 00:00 | |
maintability and engineering | 01/01/70 00:00 | |
Not entirely true? | 01/01/70 00:00 | |
You are correct | 01/01/70 00:00 | |
Good example of counting dangers | 01/01/70 00:00 | |
Good interaction.![]() | 01/01/70 00:00 |