NXC: [resolved] fmt str "%s" doesn't work with printf

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: [resolved] fmt str "%s" doesn't work with printf

Post by HaWe »

with the latest release, format string "%s" doesn't work with printf() any longer (I suppose it's a FormatNum() issue):

Code: Select all

#define printfxy( _x, _y, _fmt, _val) {  \
  string sval = FormatNum(_fmt, _val); \
  TextOut(_x, _y, sval); \
}


//********************************************************************

task main()
{
  printf ("%s", "line 1 printf");
  printfxy(0,48, "%s", "line 2 printfxy");
  printfxy(0,40, "line 3 num :%3d", 33);


  while (1);
}
(with earlier/former releases it always worked fine)

edit:
as expected, when using _fmt="%s" with
sprintf(sval, _fmt, _val);
instead of
sval = FormatNum(_fmt, _val);
it doesn't work neither!
Last edited by HaWe on 10 Aug 2012, 18:58, edited 1 time in total.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: format string "%s" doesn't work with printf

Post by HaWe »

...any ideas?
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: format string "%s" doesn't work with printf

Post by HaWe »

so after there seems to be no solution for the current release, I restored my older BCC version -
but I kept my newer efw on the brick.

Now

Code: Select all

printf("%s", "hello, world! ");
works; it shows:
hello, world!
as expected.

Unfortunately,

Code: Select all

printf("hello, %s!", "world");
does NOT work. It shows just:
world
So it's probably more a BCC issue than a efw issue.

But as my whole output of almost all programs always uses the ANSI C compatible command printf() and almost never NumOut or TextOut I'll stay with the older version - it's better than nothing.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: format string "%s" doesn't work with printf

Post by afanofosc »

FormatNum should not work at all with strings so I am rather confused by this. Do I document anywhere that %s is a supported format specifier?

From your most recent post It sounds like you were simply getting lucky with a previously existing NXC bug that made it appear that %s was working when it was not actually working. Now that I have fixed that bug it looks even less like it works than it used to.

In any case, the fmtnum opcode definitely does not work with %s or a string argument as the value to replace in the format string. Since it is designed for formatting numbers I don't have any plans to change that.

John
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: format string "%s" doesn't work with printf

Post by HaWe »

so I'm lucky and happy that a "bug" made NXC printf() work correctly!^^
NXC printf() should actually work with all format strings supported by C: http://www.cplusplus.com/reference/clib ... io/printf/

That printf (and also print) is working both with numbers and with strings makes it ideal for variable output, and those format strings are supported by lots of other programming languages.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: format string "%s" doesn't work with printf

Post by HaWe »

ps:
maybe someone wants to try this with the newest BCC version....:

Code: Select all

#define printfxy( x, y, fmt, val) {      \
  if( Pos("%s", fmt)>=0)   {             \
    TextOut(x, y, FlattenVar(val)); }    \
  else {                                 \
    string sval = FormatNum(fmt, val);   \
    TextOut(x, y, sval); }               \
}


//********************************************************************

task main()
{
  printf ("%s", "line 1 printf");
  printfxy(0,48, "%s", "line 2 printfxy");
  printfxy(0,40, "line 3 num:%4d!", 33);


  while (1);
}
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: format string "%s" doesn't work with printf

Post by afanofosc »

I just need to do a better job of documenting the differences between many of the NXC API functions which are designed to be similar to but not necessarily identical to C stdlib functions.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: format string "%s" doesn't work with printf

Post by HaWe »

IMO, printf and a fmtstr operator for both numbers and strings would be fine.

BTW, my workaround I posted above works a little better with the latest BCC release. At least with printfxy there is a string output again with "%s", opposite to printf.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: format string "%s" doesn't work with printf

Post by afanofosc »

Doc,

Could you explain the need for FlattenVar in your workaround macro? It doesn't make any sense to me. Nothing sensible would happen if val is a numeric type and if val is a string there is no need to call FlattenVar since it is already a string.

John
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: format string "%s" doesn't work with printf

Post by HaWe »

It's a trick to outwit the compiler, respectively, the preprocessor. I tried it under different circumstances without, but simply, it doesn't work without. Instead, the compiler complains of any unknown constants (identified as identifiers) or wrong variable types using the macro both with numbers and also with strings and also with format strings containing additional text. My guess is that TextOut is expected to work with numbers but it doesn't work with numbers, or do you have another guess?
Did you try the sample code without FlattenVar with better results?

Code: Select all

// compiler errors with this version !!
#define printfxy( x, y, fmt, val) {      \
  if( Pos("%s", fmt)>=0)   {             \
    TextOut(x, y, val); }    \
  else {                                 \
    string sval = FormatNum(fmt, val);   \
    TextOut(x, y, sval); }               \
}


//********************************************************************

task main()
{
  printf ("%s", "line 1 printf");
  printfxy(0,48, "%s", "line 2 printfxy");
  printfxy(0,40, "line 3 num:%4d!", 33);


  while (1);
}
# Error: Undefined Identifier 33
File "c:\Temp\temp.nxc" ; line 19
# TextOut(0, 40, 33)
#----------------------------------------------------------
# Error: string constant or variable of type string expected
File "c:\Temp\temp.nxc" ; line 19
# TextOut(0, 40, 33)
#----------------------------------------------------------
ps, besides this test version there meanwhile is an extended, even better working version which you may wish to read here:
https://sourceforge.net/apps/phpbb/mind ... 480#p14480
This version also supports additional text output in string formatters with extra string variables.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest