--- In [email protected], "int21h_d" <[EMAIL PROTECTED]> wrote:
>
> --- In [email protected], "peternilsson42" <peternilsson42@>
> wrote:
> >
> > "int21h_d" <int21h_d@> wrote:
> > >
> > > can someone explain why the "thequickbrown..." doesnt
> > > concatenates after the "hello world", that is, "hello
> > > worldthequickbrownfox...." ???
> > 
> > You _need_ to include certain headers before you can
> > expect the code to work at all...
> 
> it was supposedly to be a code snippet anyway...so headers
> are not the case here... 

That does in no way deny that you should do this.

> >   #include <stdio.h>
> >   #include <string.h>
> > 
> > If you don't then the signatures for the functions
> > you use default to the wrong type.
> > 
> > > int main(int argc, char *argv[])
> > 
> > Not an error, but you only need...
> > 
> >   int main(void)
> > 
> > > {
> > > 
> > >     char buff[80];
> > >     sprintf(buff, "hello world");
> > >     printf("%s\n", buff);
> > 
> > More simply...
> > 
> >   char buff[80] = "hello world";
> >   puts(buff);
> > 
> > >     buff[strlen(buff)]=strcat
> > > (buff,"thequickbrownfoxjumpsoverthelazydog");
> > 
> > What do you expect this to do? You are attempting to
> > assign a pointer to a character!
> 
> well, what had happened(output) here is "hello world hequick...."

As indicated by enough people, this is pure coincidence. You provoke
undefined behaviour, and this means EVERYTHING can happen.

> > You probably didn't realise that because you didn't
> > include the relevant header, so strcat defaults to...
> > 
> >   int strcat();
> > 
> > ...instead of what it should be...
> > 
> >   char *strcat(char *, const char *);
> > 
> > Had you included the header, the compiler is
> > obliged to issue a diagnostic for the constraint
> > violation of assigning a pointer to an integer.
> > 
> > Note that the 1999 C standard requires named
> > functions to be declared before use.
> > 
> > >     printf("%s\n", buff);
> > >     getch();
> > 
> > Non standard function that has a perfectly useable
> > standard alternative, namely getchar(). That said,
> > learn to run your programs from a DOS emulator,
> > rather than from IDE that closes the window when
> > the program finishes.
> 
> use of standard functions are not the case also....

Maybe, but you (resp. your friend) should use functions correctly!

> > >     return 0;
> > > }
> > 
> > It makes no sense to talk about why a broken program
> > didn't work. Undefined behaviour means _anything_
> > can happen.
> > 
> > Curiosity is a useful trait, but I'd rather spend two
> > hours writing useful code that works, than two hours
> > studying why a broken piece of code happens to behave
> > a certain way, on a specific machine, on a specific
> > compiler, at a specific time of day, and phase of the
> > moon... ;-)
> > 
> > -- 
> > Peter
> 
> well, Peter, i agree w/ you in spending your time, however,
> it will not be right either if someone had asked you such
> stuffs and answer him that the code wont work because it
> wont work...

And why not? Wrong code remains wrong, no matter what you think about it.

> in this case, i was just lost of words.. it was not my
> problem in the first place... somebody had just asked me...
> in any case, thanks for the answers =)

Sorry, but you did not miss any words, you missed the point: wrong
code remains wrong, no matter how you look at it. Provoking undefined
behaviour and using standard functions in a non-standard way IS wrong.
Period. There's nothing more to say to it.

Regards,
Nico

Reply via email to