# Bug in ruby printf/sprintf

**URL:** <https://www.ruby-forum.com/t/bug-in-ruby-printf-sprintf/100419>\
**Category:** Ruby\
**Created:** [July 28, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-printf-sprintf/100419 "2006-07-28T21:32:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 28, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-printf-sprintf/100419/1 "2006-07-28T21:32:31Z")

</div>

I have encountered what appears to be a bug in ruby’s printf/sprintf

This code:

str = “test”  
printf “|%010s|”, str

Generates this output:  
| test|

However in C and perl, the same code generates this output:  
|000000test|

The latter output is correct according to the man pages, according to  
the ruby doc for printf/sprintf the behavior should be the same.

Has anyone else encountered this? Is there something I’m missing here  
or is there a workaround for it if it is a bug?

Thanks,  
Paul

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 28, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-printf-sprintf/100419/2 "2006-07-28T21:42:15Z")

</div>

The equivalent C program under RedHat Linux 3 update 6 x86\_64 produces  
| test|

Also, I ran your ruby code in ruby 1.8.4 (2006-12-24) and it also  
produces the same results as the C code does.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 28, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-printf-sprintf/100419/3 "2006-07-28T21:49:18Z")

</div>

Tiberius wrote:

> The equivalent C program under RedHat Linux 3 update 6 x86\_64 produces  
> | test|
> 
> Also, I ran your ruby code in ruby 1.8.4 (2006-12-24) and it also  
> produces the same results as the C code does.

That’s interesting. I just tried the same on linux (Gentoo) and you’re  
correct, printf in C behaves the same way there. In Mac OS X (10.4) and  
FreeBSD (6.1) it does zero pad it as does perl on all platforms. I  
wonder why I haven’t hit this before and what the reason for the  
difference is. Time to take a look at the doc’s for the linux version.

–Paul

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 29, 2006, 11:16am UTC](https://www.ruby-forum.com/t/bug-in-ruby-printf-sprintf/100419/4 "2006-07-29T11:16:14Z")

</div>

Hi,]

In message “Re: Bug in ruby printf/sprintf”  
on Sat, 29 Jul 2006 04:45:13 +0900, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|That’s interesting. I just tried the same on linux (Gentoo) and you’re  
|correct, printf in C behaves the same way there. In Mac OS X (10.4) and  
|FreeBSD (6.1) it does zero pad it as does perl on all platforms. I  
|wonder why I haven’t hit this before and what the reason for the  
|difference is. Time to take a look at the doc’s for the linux version.

From Linux man page printf(3):

```
   0 The value should be zero padded. For d, i, o, u, x,
          X, a, A, e, E, f, F, g, and G conversions, the converted
          value is padded on the left with zeros rather than
          blanks. If the 0 and - flags both appear, the 0 flag is
          ignored. If a precision is given with a numeric
          conversion (d, i, o, u, x, and X), the 0 flag is
          ignored. For other conversions, the behavior is
          undefined.

```

It’s behavior is undefined, so that some fills with zeros and others  
just ignore. Don’t use zero with %s specifier if you want portable  
behavior.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 29, 2006, 11:40am UTC](https://www.ruby-forum.com/t/bug-in-ruby-printf-sprintf/100419/5 "2006-07-29T11:40:50Z")

</div>

On 7/29/06, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It’s behavior is undefined, so that some fills with zeros and others  
> just ignore. Don’t use zero with %s specifier if you want portable  
> behavior.

So here’s portable:  
print “|#{str.rjust(10, “0”)}|”

Sorry to insult everyone’s intelligence!
