sprintf() - is it OS specific

H

hari

Hi,

is the sprintf() implemetation OS specific or ANSI standard?
for eg. in a code like sprintf(variable,"%s%s",str1,str2);

in windows OS using VC++, if in case i pass NULL in str1 no problem,
the
first %s would be "". is there possibility that passing NULL would
result in core dump.

awaiting your replies.

thanks,
hari
 
J

Joona I Palaste

hari said:
is the sprintf() implemetation OS specific or ANSI standard?

The behaviour is ANSI standard. Any specific implementation is required
to match the behaviour but otherwise it's OS specific.
for eg. in a code like sprintf(variable,"%s%s",str1,str2);
in windows OS using VC++, if in case i pass NULL in str1 no problem,
the
first %s would be "". is there possibility that passing NULL would
result in core dump.

Yes there is a possibility. The behaviour of sprintf()'s "%s"
specifier is undefined with a NULL parameter. Windows VC++ is simply
pampering you.
awaiting your replies.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Insanity is to be shared."
- Tailgunner
 
M

Malcolm

hari said:
is the sprintf() implemetation OS specific or ANSI standard?
for eg. in a code like sprintf(variable,"%s%s",str1,str2);
The %p format is OS-specific. Beyond that, sprintf() can be written
portably. However it is such a commonly used function that most vendors
would think it worth their while optimising it, so the internals will
probably vary from platform to platform.
 
K

Kevin Easton

Malcolm said:
The %p format is OS-specific. Beyond that, sprintf() can be written
portably. However it is such a commonly used function that most vendors
would think it worth their while optimising it, so the internals will
probably vary from platform to platform.

It is certainly not trivial to write the floating point conversion
routines in portable standard C.

- Kevin.
 
H

hari

Thanks for the info.


Emmanuel Delahaye said:
Like any standard function, the specification of sprintf() is standard, but
obviously, it's implementation is not. What is not defined by the standard
can be defined by an implementation, but don't rely on it because it's not
portable, and will bite you the day you change your implementation (such a
thing occurs more often than you think).


Don't do that. The behaviour is undefined (aka UB). It's a bug. Always.


Yes. Anything can happen with a UB.
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top