[comp.lang.c] Borland C++ bug?

del@percheron.ecn.purdue.edu (David A Whittemore) (06/19/91)

i often use sprintf() to append an existing char[] with another:

        char    line[BIG ARRAY], word[SMALLER ARRAY];

        until EOF
                fscanf(fp, "%s", word);
                sprintf(line, %s %s", line, word);

under Microsoft 5.1 and all other (BSD/SYSV) compilers i have used
this would logically append "line" with "word".

under borland C++ 2.0, however, the line:
        sprintf(line, "%s %s", line, word);

acts like:
        strcpy(line, word) !

which means it wipes out the existing contents of "line".

example:
        char    jnk[100];
        strcpy(jnk, "hello");
        sprintf(jnk, "%s %s", jnk, "world");

        yields (under borland): " world"

is this a borland bug, or have i been making some bad
assumptions as to how sprintf() works for years?

ug.

-david

s64421@zeus.usq.EDU.AU (house ron) (06/20/91)

del@percheron.ecn.purdue.edu (David A Whittemore) writes:

>under borland C++ 2.0, however, the line:
>        sprintf(line, "%s %s", line, word);

>acts like:
>        strcpy(line, word) !

>which means it wipes out the existing contents of "line".

>is this a borland bug, or have i been making some bad
>assumptions as to how sprintf() works for years?

To quote the ANSI C standard, "If copying takes place between objects
that overlap, the behaviour is undefined."

--
Regards,

Ron House.   (s64421@zeus.usq.edu.au)
(By post: Info Tech, U.C.S.Q. Toowoomba. Australia. 4350)

gram@uctcs.cs.uct.ac.za (Graham Wheeler) (06/21/91)

In <1991Jun19.003026.21026@noose.ecn.purdue.edu> del@percheron.ecn.purdue.edu (David A Whittemore) writes:

>i often use sprintf() to append an existing char[] with another:

Why on earth don't you just use strcat? Its a much cheaper operation than a
sprintf.

--
Graham Wheeler <gram@cs.uct.ac.za> | "That which is weak conquers the strong,
Data Network Architectures Lab     | that which is soft conquers the hard.
Dept. of Computer Science          | All men know this; none practise it"
University of Cape Town            |		Lao Tzu - Tao Te Ching Ch.78