Help with scanf

D

Dan Pop

In said:
Yes, if you want to guarantee the output appears before
the input request.


Probably from C++, where cin/cout are 'tied', causing
the output to appear first automatically.

Why not from the C standard itself?

Dan
 
D

Dave Thompson

Hello

If you want some string to be has leading zero's

use
printf("%04d",1); => "0001"
printf("%04d",10001); => "10001"
Yes.

printf("%04.4d",10001); => "1000"

No. dot-precision for *printf %d (or i o u x X) specifies minimum
digits; it does not truncate. Only for %s can it truncate.
printf ("%4.3d", 1) -> " 001"
printf ("%4.3d", 1001) -> "1001"
printf ("%4.3d", 10001) -> "10001"
Basically, %N.Nd and %0Nd are identical.
printf("%4d",1); => " 1"


for a complete set of pssiblelities look in a good C manual.
Agree there.

- David.Thompson1 at worldnet.att.net
 
A

Arthur J. O'Dwyer

If you want some string to [have] leading zeros
use
printf("%04d",1); => "0001"
printf("%04d",10001); => "10001" Yes.
printf("%04.4d",10001); => "1000"

No. dot-precision for *printf %d (or i o u x X) specifies minimum
digits; it does not truncate. Only for %s can it truncate.
printf ("%4.3d", 1) -> " 001"
printf ("%4.3d", 1001) -> "1001"
printf ("%4.3d", 10001) -> "10001"
Basically, %N.Nd and %0Nd are identical.

Nitpick:

printf("Basically, %4.4d and %04d are identical.\n", -1, -1);

I don't completely understand this aspect of *printf format strings,
so I can't claim to guarantee that my library's correct, but at least
on my machine this prints

Basically, -0001 and -001 are identical.

which isn't true. :)

I think it's true for all positive values, though.

-Arthur
 
D

Dan Pop

Nitpick:

printf("Basically, %4.4d and %04d are identical.\n", -1, -1);

I don't completely understand this aspect of *printf format strings,
so I can't claim to guarantee that my library's correct, but at least
on my machine this prints

Basically, -0001 and -001 are identical.

%4.4d asks for a minimum output width of 4 characters and a minimum of 4
digits. It is implied that no more characters than strictly needed by
these minimum limits and the actual value to be converted are printed,
therefore -1 gets converted to -0001.

%04d asks for a minimum output width of 4 characters and zero padding
(instead of the default space padding), therefore -1 gets converted to
-001.

Your implementation is correct.
which isn't true. :)

I think it's true for all positive values, though.

For zero, too.

Dan
 

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

Similar Threads

Scanf Problem 5
Help with code plsss 0
scanf internals 11
avoid newline scanf 6
Function is not worked in C 2
about scanf() 2
strange scanf 1
question about scanf 11

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top