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.
Re: NXC: format string "%s" doesn't work with printf
Posted: 08 Aug 2012, 15:34
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
Re: NXC: format string "%s" doesn't work with 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.
Re: NXC: format string "%s" doesn't work with printf
Posted: 08 Aug 2012, 17:09
by HaWe
ps:
maybe someone wants to try this with the newest BCC version....:
Re: NXC: format string "%s" doesn't work with printf
Posted: 08 Aug 2012, 22:26
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
Re: NXC: format string "%s" doesn't work with printf
Posted: 09 Aug 2012, 06:09
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.
Re: NXC: format string "%s" doesn't work with printf
Posted: 09 Aug 2012, 14:28
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
Re: NXC: format string "%s" doesn't work with printf
Posted: 09 Aug 2012, 15:18
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?
// 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.