[Rd] a little bug for the function 'sprintf' (PR#14161)

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Dec 21 18:36:12 CET 2009


baoliqin at gmail.com wrote:
> Dear R-ers,
> 
> I am a gratuate student from South China University of Technology. I fond
> the function 'sprintf' in R2.10.1 have a little bug(?):
> 
> When you type in the example codes:
> 
>> sprintf("%s is %f feet tall\n", "Sven", 7.1)
> 
> and R returns:
> 
> [1] "Sven is 7.100000 feet tall\n"
> 
> this is very different from the 'sprintf' function in C/C++, for in C/C++,
> the format string "\n" usually represents a new line, but here, just
> the plain text "\n"!


No, this is exactly the same as in C/C++. If you compare the result of 
sprintf to "Sven is 7.100000 feet tall\n" with strcmp() in C, they will 
compare equal.

 > s <- sprintf("%s is %f feet tall\n", "Sven", 7.1)
 > s
[1] "Sven is 7.100000 feet tall\n"
 > nchar(s)
[1] 27
 > substr(s,27,27)
[1] "\n"

The thing that is confusing you is that strings are DISPLAYED using the 
same escape-character mechanisms as used for input. Compare

 > cat(s)
Sven is 7.100000 feet tall
 >



> 
> Is it a bug, or a deliberate design?

Design, not bug (and please don't file as bug when you are in doubt.)

> Thanks for your attention!
> 
> Best wishes!
> 
> Yours sincerely,
> Baoli Qin
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-devel mailing list