1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

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

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.

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

Postby doc-helmut » 05 Aug 2012, 09:49

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 doc-helmut on 10 Aug 2012, 18:58, edited 1 time in total.
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2427
Joined: 29 Sep 2010, 14:25

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

Postby doc-helmut » 07 Aug 2012, 10:36

...any ideas?
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2427
Joined: 29 Sep 2010, 14:25

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

Postby doc-helmut » 08 Aug 2012, 13:15

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.
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2427
Joined: 29 Sep 2010, 14:25

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

Postby afanofosc » 08 Aug 2012, 15:34

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/
User avatar
afanofosc
Site Admin
 
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN

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

Postby doc-helmut » 08 Aug 2012, 16:34

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/clibrary/cstdio/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.
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2427
Joined: 29 Sep 2010, 14:25

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

Postby doc-helmut » 08 Aug 2012, 17:09

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);
}
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2427
Joined: 29 Sep 2010, 14:25

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

Postby afanofosc » 08 Aug 2012, 22:26

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/
User avatar
afanofosc
Site Admin
 
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN

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

Postby doc-helmut » 09 Aug 2012, 06:09

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.
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2427
Joined: 29 Sep 2010, 14:25

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

Postby afanofosc » 09 Aug 2012, 14:28

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/
User avatar
afanofosc
Site Admin
 
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN

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

Postby doc-helmut » 09 Aug 2012, 15:18

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/mindboards/viewtopic.php?f=3&t=975&p=14480#p14480
This version also supports additional text output in string formatters with extra string variables.
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2427
Joined: 29 Sep 2010, 14:25

Next

Return to Mindstorms Software

Who is online

Users browsing this forum: No registered users and 2 guests