[OT] How do you print % signs using "sprintf"?

Archive of the old Parsimony forum. Some messages couldn't be restored. Limitations: Search for authors does not work, Parsimony specific formats do not work, threaded view does not work properly. Posting is disabled.

[OT] How do you print % signs using "sprintf"?

Postby Steve Maughan » 20 Apr 2004, 19:55

Geschrieben von:/Posted by: Steve Maughan at 20 April 2004 20:55:01:

This is driving me nuts! How do you print a % sign using (s)printf? I'm using Visual Studio 2003 .NET and the following code just prints the number and misses the '%' off the end.

if (hash_probes){
d = (double)hash_hits;
d = (100 * d / hash_probes );
sprintf(s, "info string Hash Probe Effectiveness: %5.1f%%\n", d);
send_command(s);
}
Thanks!
Steve
Steve Maughan
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Dann Corbit » 20 Apr 2004, 20:31

Geschrieben von:/Posted by: Dann Corbit at 20 April 2004 21:31:47:
Als Antwort auf:/In reply to: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Steve Maughan at 20 April 2004 20:55:01:
This is driving me nuts! How do you print a % sign using (s)printf? I'm using Visual Studio 2003 .NET and the following code just prints the number and misses the '%' off the end.

if (hash_probes){
d = (double)hash_hits;
d = (100 * d / hash_probes );
sprintf(s, "info string Hash Probe Effectiveness: %5.1f%%\n", d);
send_command(s);
}
Thanks!
Steve
Looks correct to me. I'll take a closer look.
%% is how it is usually done.



my ftp site {remove http:// unless you like error messages}
Dann Corbit
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Dann Corbit » 20 Apr 2004, 20:35

Geschrieben von:/Posted by: Dann Corbit at 20 April 2004 21:35:34:
Als Antwort auf:/In reply to: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Steve Maughan at 20 April 2004 20:55:01:
This is driving me nuts! How do you print a % sign using (s)printf? I'm using Visual Studio 2003 .NET and the following code just prints the number and misses the '%' off the end.

if (hash_probes){
d = (double)hash_hits;
d = (100 * d / hash_probes );
sprintf(s, "info string Hash Probe Effectiveness: %5.1f%%\n", d);
send_command(s);
}
Thanks!
Steve
Something's strange:
C:\tmp>type t.c
#include
int main(void)
{
char s[2048];
double d = .372122345678978;
sprintf(s, "info string Hash Probe Effectiveness: %5.1f%%\n", d);
puts(s);
return 0;
}
C:\tmp>cl t.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.
t.c
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.
/out:t.exe
t.obj
C:\tmp>t
info string Hash Probe Effectiveness: 0.4%

can you please show your definitions for s and d?



my ftp site {remove http:// unless you like error messages}
Dann Corbit
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Dieter Bürßner » 20 Apr 2004, 20:51

Geschrieben von:/Posted by: Dieter Bürßner at 20 April 2004 21:51:04:
Als Antwort auf:/In reply to: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Steve Maughan at 20 April 2004 20:55:01:
if (hash_probes){
d = (double)hash_hits;
d = (100 * d / hash_probes );
sprintf(s, "info string Hash Probe Effectiveness: %5.1f%%\n", d);
send_command(s);
}
I have almost exactly the same source here, and it works (GCC, ICC, MSVC). %% is correct.
Are you using s in a further [s]printf call? Then expect problems, becuase now it will be a single "%" and this will not be valid.
From the draft of the C99 Standard:
7.19.6.1 The fprintf function
[...]
8 The conversion specifiers [following %] and their meanings are:
[...]
% A % character is written. No argument is converted. The complete
conversion specification shall be %%.
Regards,
Dieter
Dieter Bürßner
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Dieter Bürßner » 20 Apr 2004, 21:01

Geschrieben von:/Posted by: Dieter Bürßner at 20 April 2004 22:01:02:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Dieter Bürßner at 20 April 2004 21:51:04:
Are you using s in a further [s]printf call? Then expect problems, becuase now it will be a single "%" and this will not be valid.
Perhaps I should elaborate. printf(s) will not work (in general) when s contains a "%", however printf("%s", s) will work.
Regards,
Dieter
Dieter Bürßner
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Steve Maughan » 20 Apr 2004, 21:18

Geschrieben von:/Posted by: Steve Maughan at 20 April 2004 22:18:49:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Dann Corbit at 20 April 2004 21:35:34:

Dann & Dieter,
can you please show your definitions for s and d?
Thanks for your help with this. It puzzles me.

void uci_show_stats()
{
static char s[1024];
double d;
if (uci_show_strings){
if (hash_probes){
d = (double)hash_hits;
d = (100 * d / hash_probes );
sprintf(s, "info string Hash Probe Effectiveness: %5.1f%%\n", d);
send_command(s);
}
if (cutoffs){
d = (double)primary_cutoffs;
d = (100 * d / cutoffs );
sprintf(s, "info string Move Order Effectiveness: %5.1f%%\n", d);
send_command(s);
}
}
}
and for completeness...
void send_command(char *t)
{
static size_t i;
i = strlen(t);
if (i>0){
printf(t);
if (t[i-1]!='\n') printf("\n");
fflush(stdout);
}
}
Steve Maughan
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Dieter Bürßner » 20 Apr 2004, 21:22

Geschrieben von:/Posted by: Dieter Bürßner at 20 April 2004 22:22:24:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Steve Maughan at 20 April 2004 22:18:49:
and for completeness...
void send_command(char *t)
{
static size_t i;
i = strlen(t);
if (i>0){
printf(t);
This will be a problem, see my other post.
Better use
printf("%s", t);

Regards,
Dieter
Dieter Bürßner
 

Re: [OT] Solved!!

Postby Steve Maughan » 20 Apr 2004, 21:29

Geschrieben von:/Posted by: Steve Maughan at 20 April 2004 22:29:09:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Dieter Bürßner at 20 April 2004 22:22:24:

Dieter (& Dann)
This will be a problem, see my other post.
Better use
printf("%s", t);
This solves it!! Thanks a million!
Regards,
Steve
Steve Maughan
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Dieter Bürßner » 21 Apr 2004, 01:08

Geschrieben von:/Posted by: Dieter Bürßner at 21 April 2004 02:08:48:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Steve Maughan at 20 April 2004 22:18:49:
void uci_show_stats()
{
static char s[1024];
double d;
if (uci_show_strings){
if (hash_probes){
d = (double)hash_hits;
d = (100 * d / hash_probes );
sprintf(s, "info string Hash Probe Effectiveness: %5.1f%%\n", d);
send_command(s);
}
if (cutoffs){
d = (double)primary_cutoffs;
d = (100 * d / cutoffs );
sprintf(s, "info string Move Order Effectiveness: %5.1f%%\n", d);
send_command(s);
}
}
}
and for completeness...
void send_command(char *t)
{
static size_t i;
i = strlen(t);
if (i>0){
printf(t);
if (t[i-1]!='\n') printf("\n");
fflush(stdout);
}
}
BTW. These seem to be a lot of lines of code - too many IMHO. Also they include some unneeded casts (but one probably can discuss endlessly about this, I won't, I just wanted to mention it once). My suggestions would be:

void uci_show_stats()
{
  char s[64]&#59; /* why make it static? This won't be called in recursion, and
                  stack space will be no issue */
  if (uci_show_strings)
  {
    if (hash_probes)
    {
      sprintf(s, "info string Hash Probe Effectiveness: %5.1f%%\n", 
              100.*hash_hits/hash_probes)&#59;
      send_command(s)&#59;
    }
    if (cutoffs)
    {
      sprintf(s, "info string Move Order Effectiveness: %5.1f%%\n", 
              100.*primary_cutoffs/cutoffs)&#59;
      send_command(s)&#59;
    }
  }
}
void send_command(char *t) 
{
  size_t i = strlen(t)&#59; /* Here static really looked rather odd. */
  if (i>0)
  {
    if (t[i-1]=='\n') 
      t[i-1] = '\0'&#59;
    puts(t)&#59;
    fflush(stdout)&#59;
  }
}

To me, this looks slightly easier to follow and a bit more dense (less lines of codes, no unneeded casts). There is one main difference. This send_command function will possibly change t[]. Your one did not (however you did not indicate, that you care about it - for example by saying void send_command(const char *t)). I did not test it ...
By using puts, the problem with a "%" inside the first argument to printf is avoided from the start.
Cheers,
Dieter
Dieter Bürßner
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Steve Maughan » 21 Apr 2004, 01:28

Geschrieben von:/Posted by: Steve Maughan at 21 April 2004 02:28:25:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Dieter Bürßner at 21 April 2004 02:08:48:

Dieter,
Thanks for the comments - really appreciated! The casts are there as a relic from past debugging. The other comments just highlight my lack of experience with 'C', this Monarch rewrite being my first 'C' program. I'm starting to get to grips with it but with 15 years of Pascal / Delphi experience it's hard to undo that way of thinking. Can you, or others, recommend any good books intermediate 'C' book?
Thanks again,
Steve
void uci_show_stats()
{
static char s[1024];
double d;
if (uci_show_strings){
if (hash_probes){
d = (double)hash_hits;
d = (100 * d / hash_probes );
sprintf(s, "info string Hash Probe Effectiveness: %5.1f%%\n", d);
send_command(s);
}
if (cutoffs){
d = (double)primary_cutoffs;
d = (100 * d / cutoffs );
sprintf(s, "info string Move Order Effectiveness: %5.1f%%\n", d);
send_command(s);
}
}
}
and for completeness...
void send_command(char *t)
{
static size_t i;
i = strlen(t);
if (i>0){
printf(t);
if (t[i-1]!='\n') printf("\n");
fflush(stdout);
}
}
BTW. These seem to be a lot of lines of code - too many IMHO. Also they include some unneeded casts (but one probably can discuss endlessly about this, I won't, I just wanted to mention it once). My suggestions would be:

To me, this looks slightly easier to follow and a bit more dense (less lines of codes, no unneeded casts). There is one main difference. This send_command function will possibly change t[]. Your one did not (however you did not indicate, that you care about it - for example by saying void send_command(const char *t)). I did not test it ...
By using puts, the problem with a "%" inside the first argument to printf is avoided from the start.
Cheers,
Dieter
>void uci_show_stats()
>{
>  char s[64]&#59; /* why make it static? This won't be called in recursion, and
>                  stack space will be no issue */
>  if (uci_show_strings)
>  {
>    if (hash_probes)
>    {
>      sprintf(s, "info string Hash Probe Effectiveness: %5.1f%%\n", 
>              100.*hash_hits/hash_probes)&#59;
>      send_command(s)&#59;
>    }
>    if (cutoffs)
>    {
>      sprintf(s, "info string Move Order Effectiveness: %5.1f%%\n", 
>              100.*primary_cutoffs/cutoffs)&#59;
>      send_command(s)&#59;
>    }
>  }
>}
>void send_command(char *t) 
>{
>  size_t i = strlen(t)&#59; /* Here static really looked rather odd. */
>  if (i>0)
>  {
>    if (t[i-1]=='\n') 
>      t[i-1] = '\0'&#59;
>    puts(t)&#59;
>    fflush(stdout)&#59;
>  }
>}
>
Steve Maughan
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Dieter Bürßner » 21 Apr 2004, 01:42

Geschrieben von:/Posted by: Dieter Bürßner at 21 April 2004 02:42:03:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Steve Maughan at 21 April 2004 02:28:25:
Can you, or others, recommend any good books intermediate 'C' book?
My suggestion: http://cm.bell-labs.com/cm/cs/cbook/. I bought the first edition many years ago (it was stolen from my desk ... perhaps a small indication of the quality of this book). One of the best technical books I ever read. It is very compact, but still explains everything really needed. Almost free of errors (when I go to the local book shop and look through any random book about C or C++, I typically can detect many errors on the first view, not so, with this book). One of the authors (Dennis Ritchie) is the main "architect" of the C language (together with Ken Thompson, who is also very well known in the computer chess history).
Cheers,
Dieter
Dieter Bürßner
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Dann Corbit » 21 Apr 2004, 01:54

Geschrieben von:/Posted by: Dann Corbit at 21 April 2004 02:54:00:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Dieter Bürßner at 21 April 2004 02:42:03:
Can you, or others, recommend any good books intermediate 'C' book?
My suggestion: http://cm.bell-labs.com/cm/cs/cbook/. I bought the first edition many years ago (it was stolen from my desk ... perhaps a small indication of the quality of this book). One of the best technical books I ever read. It is very compact, but still explains everything really needed. Almost free of errors (when I go to the local book shop and look through any random book about C or C++, I typically can detect many errors on the first view, not so, with this book). One of the authors (Dennis Ritchie) is the main "architect" of the C language (together with Ken Thompson, who is also very well known in the computer chess history).
I must agree with Dieter.
Another thing worth learning is to download the C FAQ:
http://www.faqs.org/faqs/C-faq/faq/
Even experienced programmers may learn something new.



my ftp site {remove http:// unless you like error messages}
Dann Corbit
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Dieter Bürßner » 21 Apr 2004, 02:10

Geschrieben von:/Posted by: Dieter Bürßner at 21 April 2004 03:10:41:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Steve Maughan at 21 April 2004 02:28:25:
Can you, or others, recommend any good books intermediate 'C' book?
How could I forget, to mention http://users.powernet.co.uk/eton/unleashed/. I did not read this book (I never saw it in one of the local book stores I typically visit - if it were there, I certainly would buy it). You might see a very well known name (to the readers of the WB-Forum) in the list of the authors! I "know" other names of the author list, too (from news postings and/or mail). I think they all will know very well, about what they write (I would not say this about many other books about C, that I saw).
Cheers,
Dieter
Dieter Bürßner
 

Re: [OT] Both Books Ordered - Thanks! [NT]

Postby Steve Maughan » 21 Apr 2004, 12:47

Geschrieben von:/Posted by: Steve Maughan at 21 April 2004 13:47:03:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Dann Corbit at 21 April 2004 02:54:00:

...
Steve Maughan
 

Re: [OT] How do you print % signs using "sprintf"?

Postby Michael Byrne » 25 Apr 2004, 18:14

Geschrieben von:/Posted by: Michael Byrne at 25 April 2004 19:14:59:
Als Antwort auf:/In reply to: Re: [OT] How do you print % signs using "sprintf"? geschrieben von:/posted by: Dann Corbit at 21 April 2004 02:54:00:
Can you, or others, recommend any good books intermediate 'C' book?
My suggestion: http://cm.bell-labs.com/cm/cs/cbook/. I bought the first edition many years ago (it was stolen from my desk ... perhaps a small indication of the quality of this book). One of the best technical books I ever read. It is very compact, but still explains everything really needed. Almost free of errors (when I go to the local book shop and look through any random book about C or C++, I typically can detect many errors on the first view, not so, with this book). One of the authors (Dennis Ritchie) is the main "architect" of the C language (together with Ken Thompson, who is also very well known in the computer chess history).
I must agree with Dieter.
Another thing worth learning is to download the C FAQ:
http://www.faqs.org/faqs/C-faq/faq/
Even experienced programmers may learn something new.
For the Dennis Ritchie book, used paperbook copies start here from about $15 , new paperback go for about $32, used hardcover editions start @ $67
http://www.amazon.com/gp/product/offer- ... dition=all.
I just ordered the one for $15 ...not sure it will show up as being available...ao far it is, but it may not really be available if they just have one.
Michael Byrne
Michael Byrne
 


Return to Archive (Old Parsimony Forum)

Who is online

Users browsing this forum: No registered users and 25 guests