Hi
Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");
Thanks,
Leon 10 9919
On 27 Nov 2003 19:18:11 -0800, io****@yahoo.com (ios) wrote: Hi
Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP");
Were you expecting any?
The contents of eventname (if an array) or the contents of the memory
it points to (if a pointer) will be the same for either statement in
this example. This would not be true if the second argument contained
anything sprintf would consider a conversion specification.
strcpy returns a pointer while sprintf returns an int but, since
either will be discarded, I don't think this is a relevant difference.
I would expect strcpy to be significantly faster but this is my
intuitive judgement and not part of the standard.
The only real difference I can see is that you need to include a
different header file depending on which you use.
<<Remove the del for email>>
On Thu, 27 Nov 2003, ios wrote:
[Incidentally, there's no need to tag your question with [Q];
that *is* the default around here, despite recent tendencies.
The only recognized subject-line tag I can think of for c.l.c
is [OT], marking off-topic posts.]
Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP");
In this case, absolutely nothing. In general, though,
sprintf() has the whole 'format specifiers' baggage, like
printf() does; and of course sprintf() returns a character
count where strcpy() returns a pointer to the destination
array.
Note particularly that while
strcpy(d,s);
and
sprintf(d,"%s",s);
are exactly identical under all circumstances,
strcpy(d,s);
and
sprintf(d,s);
are NOT identical; consider the case where
(0 == strcmp(s,"%%")).
HTH,
-Arthur
ios wrote: Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP");
I believe that strcpy will copy the final '\0', and sprintf will
not. Thus you almost certainly want to capture the return value
of sprintf.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
"CBFalconer" <cb********@yahoo.com> wrote: ios wrote: strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP");
I believe that strcpy will copy the final '\0', and sprintf will not. Thus you almost certainly want to capture the return value of sprintf.
sprintf will write a '\0' to terminate the output string.
There is no difference between the two given statements,
assuming that both <stdio.h> and <string.h> are #included.
--
Simon.
Simon Biber wrote: "CBFalconer" <cb********@yahoo.com> wrote: ios wrote: > strcpy(eventname, "MDCX_RSP"); > and > sprintf(eventname, "MDCX_RSP");
I believe that strcpy will copy the final '\0', and sprintf will not. Thus you almost certainly want to capture the return value of sprintf.
sprintf will write a '\0' to terminate the output string.
There is no difference between the two given statements, assuming that both <stdio.h> and <string.h> are #included.
You are correct (and I think Chuck was just hallucinating or something), but
it bears repeating (assuming someone already pointed it out) that the
strcpy version is likely to have superior performance, and that the sprintf
could break if a different string literal is used. For example,
sprintf(eventname, "%saved"); /* !!! */
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
In article <3F***************@yahoo.com>,
CBFalconer <cb********@yahoo.com> wrote: ios wrote: Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP");
I believe that strcpy will copy the final '\0', and sprintf will not. Thus you almost certainly want to capture the return value of sprintf.
They both will copy the final '\0'. The difference is what happens when
a maintenance programmer has a reason to change "MDCX_RSP" to something
else, for example something that contains % characters, and doesn't
notice that sprintf has been used...
So the second form is a bug waiting to come out and byte you where it
hurts. (The exception would be if this is within a series of sprintf
statements, where some don't have any additional arguments except the
formatting string).
there is no difference between the two cases. However if you want to
include some integer in between you can't use strcpy io****@yahoo.com (ios) wrote in message news:<79*************************@posting.google.c om>... Hi
Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP");
Thanks, Leon
ios <io****@yahoo.com> wrote: Hi
Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP");
No difference.
--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
In <3F***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes: ios wrote: Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP");
I believe that strcpy will copy the final '\0', and sprintf will not. Thus you almost certainly want to capture the return value of sprintf.
Don't be idiot! What do you think the 's' in sprintf stands for?
2 The sprintf function is equivalent to fprintf, except that the
output is written into an array (specified by the argument s)
rather than to a stream. A null character is written at the end
of the characters written; it is not counted as part of the
returned value.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <79*************************@posting.google.com> io****@yahoo.com (ios) writes:
First, there is no point in using the [Q] tag in the subject line when
posting a question. A question mark at the end is a better choice. Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP");
They have the same effect, only the return value (which you ignore,
anyway) is different.
However, the execution of the strcpy call is likely to be faster than the
execution of the sprintf call. The former has to compare each copied
character to 0, while the latter has to compare it to both 0 and % and
also check each character whether it is a single byte character of the
first byte of a multibyte character.
To be perfectly safe, regardless of the contents of the copied string,
the sprintf call should be written like this:
sprintf(eventname, "%s", "MDCX_RSP");
but why bother, since strcpy() is the right tool for the job?
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: William Payne |
last post by:
Hello, I have two structs:
struct FileEntry
{
FileEntry(const char* name,
const char* type,
std::size_t file_size,
HICON icon)
:
m_file_size(file_size),
|
by: Paul Sheer |
last post by:
I need to automatically search and replace all fixed size
buffer strcpy's with strncpy's (or better yet, strlcpy's)
as a security and stability audit. The code base is large
and it is not feasable...
|
by: Matt |
last post by:
I have 2 questions:
1. strlen returns an unsigned (size_t) quantity. Why is an unsigned
value more approprate than a signed value? Why is unsighned value less
appropriate?
2. Would there...
|
by: shya |
last post by:
hi there!
I just wrote a function that reverse a string. It seems all ok to me,
but the program goes on segmentation fault on *s = *t_str.
Here's the code:
#include <stdio.h>
#include...
|
by: ulyses |
last post by:
Hi i have got quite strange problem. I wrote programme which shows all
runnign processes. This info is get from /proc dir. When a dir that is
process dir is found stat file is read and pid, name...
| |
by: Shark |
last post by:
What could be the error??
i have a dll for converting vox files to Dll.
the structure of the header looks like :
...
|
by: ben.carbery |
last post by:
Hi,
I have just written a simple program to get me started in C that
calculates the number of days since your birthdate.
One thing that confuses me about the program (even though it works) is...
|
by: semut |
last post by:
Given that the string is of null terminated type. What could be the
possible causes (by experience) the string to have no null terminated
and cause buffer overflow later. I know it is quite broad,...
|
by: google |
last post by:
Consider the following code:
char str;
char str2;
strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%");
printf("printf: ");
printf("1%s2", str);
printf("\nsprintf: ");
sprintf(str2, "1%s2",...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |