sprintf Vs strcpy Vs strcat

Aug 25, 2009 at 12:42am
Hi Guys,

Can someone please explain me how sprintf is different from strcat or strcpy? Actually, I wrote one simple program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<string.h>
#include<stdlib.h> 

int main() { 

    char concatenate_str[100];
    char sprint_str[100];

    strcpy(concatenate_str,"ps");
    strcpy(sprint_str,"ps");

    strcat(concatenate_str," > tmp00");
    sprintf(sprint_str,"%s > tmp01", sprint_str);

    system(concatenate_str);
    system(sprint_str);

    printf("Concatenated Str: %s\n", concatenate_str);
    printf("SpintF Str: %s\n", sprint_str);


    return 0;
}


Now they both return the same result. So how is sprintf different than strcat or for that matter we can also use strcpy in the above code as strcpy(cmd,"ps > tmp00");

Thanks in advance
Aug 25, 2009 at 9:53am
strcpy and strcat copy bytes upto the terminating null character.

sprintf belongs to the printf family of string formatting functions. sprintf doesn't just copy a string it takes a format specification that decribes how the arguments can be formatted.

http://www.freebsd.org/cgi/man.cgi?query=sprintf&apropos=0&sektion=0&manpath=FreeBSD+7.2-RELEASE&format=html

It should be said these are C functions. In C++ you'd us a std::ostringstream to produce formatted output to a buffer.
Aug 25, 2009 at 4:11pm
This may repeat some of what kbw has said - but I started it this
morning before leaving for the day job.

Well, if you analyse these two lines - you can see that strcat and sprintf are different:
1
2
3
    
strcat(concatenate_str," > tmp00");
sprintf(sprint_str,"%s > tmp01", sprint_str);


Also you will notice that the result of the sprintf statement did not go to the standard output (screen) where as the reslt of a printf does.

printf, sprintf and fprintf all BUILD a result string in the same way - you can have embbeded controls like %s, etc.
printf output goes to standard output,
sprintf output goes to a buffer, and fprintf goes to an output stream (file).

strcpy takes two pointers to a buffer and simply copies the chars (including the terminating 0) from the source buffer to the destination buffer. There is no building up of the string if you see what I mean - just a straight copy (overwriting anything that is already present in the destination buffer).

strcat.
takes a pointer to a destination buffer and a pointer to a source buffer. It finds the terminationg 0 of the destination buffer and starting from there tags on the data from the source buffer and adds a terminating 0.
there is no building up of the string like printf

sprintf, strcpy, strcat (and also strlen function) are all considered dangerous - the all use pointer to buffers -
there are no checks to see if the destination buffer is large enough to hold the resulting string -
so can easily lead to buffer overflow.

Note you can use c++ sytle strings instead of c style strings - but the same problems can happen.
A fair few of the c++ style string operators take char *:

for example:
1
2
3
4
5
6
#include <string>

std::string s;
char *pc;

s += pc; //can be a problem if pc is uninitialized 
Aug 27, 2009 at 1:13pm
Thanks a lot for the reply kbw and guestgulkan. It cleared up my doubt. :)
Topic archived. No new replies allowed.