printf question

B

Ben Pfaff

Jamesm said:
I am upgrading a large code base that I did not write and can not speak
with the original developer. I have encountered the following sprintf
format many times.

sprintf(buf, "%10.10d", somevar);

Where somevar is an int. Now to me the .10 part makes no sense since an
int is never going to have a fractional portion, so you would never use
that part of the format. I just want to make sure I am not missing some
sprintf functionality that I am unaware of before I remove them.

For %d, sprintf always outputs at least as many digits as
specified by the precision. Thus, %10.10d will output the number
1 as 0000000001.
 
J

Jens Thoms Toerring

Jamesm said:
I am upgrading a large code base that I did not write and can not speak
with the original developer. I have encountered the following sprintf
format many times.
sprintf(buf, "%10.10d", somevar);
Where somevar is an int. Now to me the .10 part makes no sense since an
int is never going to have a fractional portion, so you would never use
that part of the format. I just want to make sure I am not missing some
sprintf functionality that I am unaware of before I remove them.

For '%d' (and other format specifiers for integers) the part after
the dot specifies the minimum number of digits to output, so if
'somevar' is e.g. 123 then you will get

0000000123

while with '%10d" you would just get

123

i.e. with spaces instead of zeros in front. So you probably better
don't remove it without further checks what the leading zeros are
good for;-)
Regards, Jens
 
P

Peter Nilsson

Jamesm said:
I am upgrading a large code base that I did not write and can
not speak with the original developer.

But you can still RTFM.
I have encountered the following sprintf format many times.

sprintf(buf, "%10.10d", somevar);

It could be also be written as...

sprintf(buf, "%010d", somevar);
Where somevar is an int. Now to me the .10 part makes no sense

So look it up.
since an int is never going to have a fractional portion, so
you would never use that part of the format. I just want to
make sure I am not missing some sprintf functionality that I
am unaware of

And you have _no_ references for the standard library?!
before I remove them.

Tip: If it ain't broken, don't fix it.

The following will give you a guide for what the precision is
for with %d...

#include <stdio.h>

int main(void)
{
printf(">%08d<\n", 123);
printf(">%8.8d<\n", 123);
printf(">%8.5d<\n", 123);
printf(">%-8.5d<\n", 123);
return 0;
}
 
J

J. J. Farrell

I am upgrading a large code base that I did not write and can not speak
with the original developer. I have encountered the following sprintf
format many times.

sprintf(buf, "%10.10d", somevar);

Where somevar is an int. Now to me the .10 part makes no sense since an
int is never going to have a fractional portion, so you would never use
that part of the format. I just want to make sure I am not missing some
sprintf functionality that I am unaware of before I remove them.

You could always try looking at documentation for printf. You could
use Google to find some if you don't have any to hand.

If you don't know what it does and have no requirement to change it,
why would you even consider removing it?
 
J

Jamesm

I am upgrading a large code base that I did not write and can not speak
with the original developer. I have encountered the following sprintf
format many times.

sprintf(buf, "%10.10d", somevar);

Where somevar is an int. Now to me the .10 part makes no sense since an
int is never going to have a fractional portion, so you would never use
that part of the format. I just want to make sure I am not missing some
sprintf functionality that I am unaware of before I remove them.
 
O

Old Wolf

I am upgrading a large code base that I did not write and can not speak
with the original developer. I have encountered the following sprintf
format many times.

sprintf(buf, "%10.10d", somevar);

I'm mentioning this as it's often overlooked by casual coders -
this code does not limit the size of the output. If the int has
15 decimal digits, then 16 bytes will be written to buf. When
using sprintf with %d, you must take other measures to ensure
that there is no buffer overflow.
 
¬

¬a\\/b

But you can still RTFM.


It could be also be written as...

sprintf(buf, "%010d", somevar);

and what does it print in buf "sprintf(buf, "%03d", 123456);"?

if "int somevar=1;"
and how you write
#########1
?
sprintf(buf, "%#10d", somevar);?

and how you write
%%%%%%%%%1
?

i change the "printf standard" and should use something like
spr_m(buf, sizebuf, "%10:%d", somevar);
for above
 
H

Haider

I am upgrading a large code base that I did not write and can not speak
with the original developer. I have encountered the following sprintf
format many times.

sprintf(buf, "%10.10d", somevar);

Where somevar is an int. Now to me the .10 part makes no sense since an
int is never going to have a fractional portion, so you would never use
that part of the format. I just want to make sure I am not missing some
sprintf functionality that I am unaware of before I remove them.

it means 10 byte of buffer will be used to write that integer it will
get accomodated in 10 bytes.
if it will sprintf(buf, "10.8d", somvar); and somevar=15
then first two byte of buf wud be occupied by space character i.e 0x20
and followed by six ascii zeros i.e 0x30 and then two byte will become
ascii 15 the whole buffer wud be somthing like that
20203030303030303135..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top