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

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
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 »

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
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 »

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" ... :)
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 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
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 »

wow, awesome! :o
thank you very much for this job!
then there remains nothing to do for me at all any more.... :shock:
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post by HaWe »

tested: FormatVal works fine!

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

Who is online

Users browsing this forum: No registered users and 0 guests