Page 2 of 2

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

Posted: 09 Aug 2012, 16:20
by afanofosc
Just to demonstrate what was going wrong with FormatNum before it was fixed, here's the underlying NBC code that used to be generated:

Code: Select all

	strcat __strbufmain, "%s"
	strcat __strbufmain, "line 1 printf"
	fmtnum __strretvalmain, __strbufmain, __constVal1
	strcat __strbufmain, __strretvalmain
As you can see here, the __strbufmain value, which was the format specifier, was being being incorrectly overwritten by the string. The compiler was failing to complain about this. Then the fmtnum opcode was using "line 1 printf" as the format string and the numeric value (__constVal1) was ignored. This bug has been fixed. The new code looks like this:

Code: Select all

	strcat __strbufmain, "%s"
	mov __ArrHelper__main_244_0, __strbufmain
	strcat __strbufmain, "line 1 printf"
	fmtnum __strretvalmain, __ArrHelper__main_244_0, __constVal1
	strcat __strbufmain, __strretvalmain
As you can see, it no longer incorrectly overwrites the format string.

I am investigating an enhancement to the fmtnum opcode which would allow it to take a string for its third argument.

John Hansen

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

Posted: 09 Aug 2012, 16:40
by HaWe
yes, now I understand.
I'm happy to hear that there once will be an enhancement for "%s" (although deploring that my - you must admit - smart workarounds then won't be of any use any more^^).

But I guess then there will be enough room for even wider enhancements like, e.g., "-%10s" ... :)

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

Posted: 10 Aug 2012, 06:05
by afanofosc
I have uploaded a new test release of BricxCC and the enhanced NBC/NXC firmware which adds support for a string value as the third parameter to the fmtnum opcode. That means you can now use "%-10s" format specifiers with this opcode.

The new compiler includes FLT_MAX sorts of constants plus a new TypeOf function that returns a number indicating the type of the variable along with several VT_* constants in NBCCommon.h that work with that function. Also a new FormatVal API function which is identical to FormatNum except its documentation and name suggest that it can handle values of any numeric or string type whereas th documentation for FormatNum says it expects/supports only numeric format strings and numeric values.

You'll get updated syntax highlighting if you click the Default button on the API tab of the Preferences dialog after extracting all the test release files to their proper locations. Make sure to "unblock" your .chm and .exe files via the file properties dialog.

John Hansen

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

Posted: 10 Aug 2012, 07:54
by HaWe
wow, awesome! :o
thank you very much for this job!
then there remains nothing to do for me at all any more.... :shock:

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

Posted: 10 Aug 2012, 18:40
by HaWe
tested: FormatVal works fine!

even left-text-justify works!
GREAT WORK !
:)