Is Sscanf() OK?

May 20, 2008
12,176
133
Syracuse, NY, USA
I have a plugin that begins like this.
Code:
INT WINAPI TEST ( LPWSTR psz )
{
    WCHAR c;
    INT n = Sscanf(psz, L" %c", &c);
    Printf(L"n = %d ... **%c** ... %d\r\n", n, c, c);

If I call that plugin with no arguments, Sscanf returns 1. I imagine the value of c is whatever garbage was there in the first place. But should Sscanf() return 1?
Code:
v:\> test
n = 1 ... **䝨** ... 18280

v:\> test
n = 1 ... **睸** ... 30584

v:\> test
n = 1 ... **枈** ... 26504
 

rconn

Administrator
Staff member
May 14, 2008
12,557
167
First, I don't recommend that you use Sscanf, Sprintf, or Printf. They were necessary a few years ago when the RTL was missing some critical things (like support for 64-bit ints), and they were faster than the MS implementations. They are no longer necessary, and I'll be eliminating them in another (major) version or two.

Second, TCC never passes empty or invalid arguments to Sscanf, so it doesn't check for null / empty string arguments. Adding that would mean unnecessary overhead.
 
May 20, 2008
12,176
133
Syracuse, NY, USA
First, I don't recommend that you use Sscanf, Sprintf, or Printf. They were necessary a few years ago when the RTL was missing some critical things (like support for 64-bit ints), and they were faster than the MS implementations. They are no longer necessary, and I'll be eliminating them in another (major) version or two.

Second, TCC never passes empty or invalid arguments to Sscanf, so it doesn't check for null / empty string arguments. Adding that would mean unnecessary overhead.
Thanks for the reply. I build my plugins with /MT so all the C stuff gets built in. Avoiding wprintf and swscanf keeps them small. Does the same go for Qprintf (vs. fwprintf)?
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,689
106
Albuquerque, NM
prospero.unm.edu
First, I don't recommend that you use Sscanf, Sprintf, or Printf. They were necessary a few years ago when the RTL was missing some critical things (like support for 64-bit ints), and they were faster than the MS implementations. They are no longer necessary, and I'll be eliminating them in another (major) version or two.

Oh, please don't eliminate Printf. I use it all the time, as it handles redirection correctly, unlike the RTL functions. (The other two I don't care about.)
 
May 20, 2008
12,176
133
Syracuse, NY, USA
Oh, please don't eliminate Printf. I use it all the time, as it handles redirection correctly, unlike the RTL functions. (The other two I don't care about.)
I'll second that! With Printf (probably Qprintf also) redirection and piping of plugin output happen seamlessly with no effort on the part of the plugin author. And they don't happen at all with wprintf (and probably fwprintf). If there's some monkeying with streams and standard handles that will let the C library output functions work as the SDK ones, please tell me how to do that.