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

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Mon, 21 Dec 2009 18:36:12 +0100

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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Mon 21 Dec 2009 - 17:40:24 GMT

This archive was generated by hypermail 2.2.0 : Tue 22 Dec 2009 - 11:51:10 GMT