sprintf on MVS

P

portergrouptx

I am trying to pad a string with leading character zeros. There seems
to be a difference between the behavior of sprintf on Windows
(Microsoft Visual C++ .NET) and on MVS. Can anyone explain the reason
for this? Or am I doing something incorrectly? Thanks in advance!

---
On Windows:
---

string s = "BB";
char buf[5];
sprintf(buf, "%04s", s.c_str());

// after sprintf: buf = "00BB" <========!!!

---
On MVS:
---

string s = "BB";
char buf[5];
sprintf(buf, "%04s", s.c_str());

// after sprintf: buf = " BB" <========!!!
 
W

Walter Roberson

I am trying to pad a string with leading character zeros. There seems
to be a difference between the behavior of sprintf on Windows
(Microsoft Visual C++ .NET) and on MVS. Can anyone explain the reason
for this? Or am I doing something incorrectly? Thanks in advance!

Well, the first thing you are doing wrong is asking the question
on a C newsgroup instead of a C++ newsgroup ;-)
string s = "BB";

C has no 'string'.
char buf[5];
sprintf(buf, "%04s", s.c_str());

In C, that would imply that s is a structure (or union) with a member
named c_str which is a function with no parameters. But you can't
store functions in C, only function pointers, so you certainly
wouldn't be getting anything useful there. And if s *were*
a structure or union, you wouldn't be able to initialize it with
a character array...
// after sprintf: buf = "00BB" <========!!!
string s = "BB";
char buf[5];
sprintf(buf, "%04s", s.c_str());
// after sprintf: buf = " BB" <========!!!

In C, the meaning of a 0 modifier on an s parameter is undefined.
So in C, either output would be within the bounds of the standard.
Along with lots of other possibilities such as putting in umaults
intead of spaces or 0's.


Try this:

char s[] = "BB";
char buf[5];
sprintf(buf, "%s%s", "0000" + min(4,strlen(s)), s);
 
P

portergrouptx

Thanks for the reply!

I did not realize that a 0 modifier on an s parameter is undefined - i
will not use it.

Sorry for including a c++ construct (string) on this newsgroup. My
intention was not to imply that s is a struct or union, etc., etc.

Thanks again.
 
K

Keith Thompson

string s = "BB";

C has no 'string'.
char buf[5];
sprintf(buf, "%04s", s.c_str());

In C, that would imply that s is a structure (or union) with a member
named c_str which is a function with no parameters. But you can't
store functions in C, only function pointers, so you certainly
wouldn't be getting anything useful there.

No, it implies that s is a structure or union with a member named
c_str which is a *pointer* to a function with no parameters.
Remember, the C function call operator takes a pointer-to-function as
it first operand. In the common case of using a function name, such
as foo(10, 20), the function name foo is implicitly converted to a
pointer-to-function value before it becomes the operand of the call
operator.

<OT>
The difference between this and a call to a C++ member function is
that there's no implicit parameter referring to the object containing
the pointer. s.c_str() is perfectly legal in C, assuming s is
declared properly, but the function pointed to by c_str has no
knowledge of the value of s -- which is why you don't see this kind of
thing as often in C as in C++. Something like s.c_str(s) would be
closer to the C++ semantics.
</OT>

This is, of course, a tangent of no particular relevance to what the
OP was actually asking about.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top