473,748 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Q]What is different between strcpy and sprintf in this case

ios
Hi

Can someone tell me what is different between below case?

strcpy(eventnam e, "MDCX_RSP") ;
and
sprintf(eventna me, "MDCX_RSP") ;

Thanks,
Leon
Nov 13 '05 #1
10 9951
On 27 Nov 2003 19:18:11 -0800, io****@yahoo.co m (ios) wrote:
Hi

Can someone tell me what is different between below case?

strcpy(eventna me, "MDCX_RSP") ;
and
sprintf(eventn ame, "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>>
Nov 13 '05 #2

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(eventnam e, "MDCX_RSP") ;
and
sprintf(eventna me, "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

Nov 13 '05 #3
ios wrote:

Can someone tell me what is different between below case?

strcpy(eventnam e, "MDCX_RSP") ;
and
sprintf(eventna me, "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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #4
"CBFalconer " <cb********@yah oo.com> wrote:
ios wrote:
strcpy(eventnam e, "MDCX_RSP") ;
and
sprintf(eventna me, "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.
Nov 13 '05 #5
Simon Biber wrote:
"CBFalconer " <cb********@yah oo.com> wrote:
ios wrote:
> strcpy(eventnam e, "MDCX_RSP") ;
> and
> sprintf(eventna me, "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(eventna me, "%saved"); /* !!! */

--
Richard Heathfield : bi****@eton.pow ernet.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
Nov 13 '05 #6
In article <3F************ ***@yahoo.com>,
CBFalconer <cb********@yah oo.com> wrote:
ios wrote:

Can someone tell me what is different between below case?

strcpy(eventnam e, "MDCX_RSP") ;
and
sprintf(eventna me, "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).
Nov 13 '05 #7
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.co m (ios) wrote in message news:<79******* *************** ***@posting.goo gle.com>...
Hi

Can someone tell me what is different between below case?

strcpy(eventnam e, "MDCX_RSP") ;
and
sprintf(eventna me, "MDCX_RSP") ;

Thanks,
Leon

Nov 13 '05 #8
ios <io****@yahoo.c om> wrote:
Hi

Can someone tell me what is different between below case?

strcpy(eventnam e, "MDCX_RSP") ;
and
sprintf(eventna me, "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... ===
Nov 13 '05 #9
In <3F************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
ios wrote:

Can someone tell me what is different between below case?

strcpy(eventnam e, "MDCX_RSP") ;
and
sprintf(eventna me, "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
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2304
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),
7
3347
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 to manually perform these changes. I would like perhaps a C++ parser that can automatically detect use of a strcpy to a buffer of fixed size. For instance, struct x { char member;
81
7329
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 be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
12
2381
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 <stdlib.h> void reverse(char *s) {
5
1763
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 and state are get from it. The thing is that on my machine every time I run it, it gives different results. Sometimes it shows nothing - most of the times. Sometimes it shows info about 4 processes. Sometimes it works correctly. I don't now whats...
1
528
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 : -----------------------------------------code------------------------------------------- typedef struct tFILECONVERT {
18
3133
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 how global variables and function returns work... For example, I have a global array "char datestring;" which is defined in the function speakdate. speakdate just converts a set of integers (date variables) to a string.
12
1982
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, just like to find out the causes as much as possible so that I could impose stricter checking toward my codes. note: I could not use std::string cause it will require a total rewrite. thanks.
3
2620
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", str); //Interesting stuff happens here printf(str2);
0
8996
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6799
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.