Version franaise
Home About Download Resources Contact us

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
precision not working properly for strings in Printf?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-06-27 (04:11)
From: Jonathan Bryant <jtbryant@v...>
Subject: Re: [Caml-list] precision not working properly for strings in Printf?

On Jun 26, 2007, at 11:48 PM, Quc Peyrot wrote:

> On Jun 27, 2007, at 5:38 AM, Jonathan Bryant wrote:
>> On Jun 26, 2007, at 11:18 PM, Quc Peyrot wrote:
>>> It seems that the precision field doesn't work properly with the  
>>> "s" type in Printf.printf:
>> It does work properly.  It's just not working the way you're  
>> expecting it to work.
> I was just expecting it to work like the printf from the glibc:
> s      If  no  l  modifier  is  present:  The  const char *  
> argument is
> 	      expected to be a pointer to an array of character type   
> (pointer
> 	      to  a string).  Characters from the array are written up to  
> (but
> 	      not including) a terminating NUL character; if  a   
> precision  is
> 	      specified,  no more than the number specified are written.   
> If a
> 	      precision is given, no null character need be  present;  if   
> the
> 	      precision  is  not specified, or is greater than the size of  
> the
> 	      array, the array must contain a terminating NUL character.

The OCaml standard library isn't glibc.  The implementation of the  
Printf/Scanf modules is custom, OCaml specific and tied into the  
compiler, so one should not assume the behave the same way.  They are  
not simply wrappers.  As a matter of fact, they have several other  
differences from the glibc printf family of functions (look at the  
conversion specifiers in the docs).

>>> # Printf.printf "%.2s" "qwerty";;
>>> qwerty- : unit = ()
>>> This should print "qw"
>> No it shouldn't.  The width/precision specifier guarantees a  
>> _minimum_ length for strings, not an absolute length.  From the  
>> docs for Printf:
> As said above, this is not how printf is working in the glibc (at  
> least on linux and Mac OS X). Any clue why the same convention has  
> not been followed?

See above.

> -- 
> Best Regards,
> Quc
> _______________________________________________
> Caml-list mailing list. Subscription management:
> Archives:
> Beginner's list:
> Bug reports: